<template>
  <a-drawer
      :title="title"
      :width="800"
      placement="right"
      :closable="false"
      @close="close"
      :visible="visible"
  >

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="日期">
          <a-date-picker v-decorator="[ 'date', validatorRules.date ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="账户ID">
          <a-input placeholder="请输入账户ID" v-decorator="['accountId', validatorRules.accountId ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="快手ID">
          <a-input placeholder="请输入快手ID" v-decorator="['kid', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="广告主名称">
          <a-input placeholder="请输入广告主名称" v-decorator="['advertiserName', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="广告主创建时间">
          <a-date-picker v-decorator="[ 'advertiserCreateTime', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="有消费计划数">
          <a-input-number v-decorator="[ 'costCampaignCount', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="总余额">
          <a-input-number v-decorator="[ 'balance', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="总消耗">
          <a-input-number v-decorator="[ 'cost', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="现金消耗">
          <a-input-number v-decorator="[ 'xianjinCost', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="后返消耗">
          <a-input-number v-decorator="[ 'fandianCost', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="框返消耗">
          <a-input-number v-decorator="[ 'kuangfanCost', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="激励账户消耗">
          <a-input-number v-decorator="[ 'jiliCost', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="信用账户消耗">
          <a-input-number v-decorator="[ 'xinyongCost', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="封面曝光数">
          <a-input-number v-decorator="[ 'fengmianShowCount', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="封面点击数">
          <a-input-number v-decorator="[ 'fengmianClickCount', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="素材曝光数">
          <a-input-number v-decorator="[ 'sucaiShowCount', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="行为数">
          <a-input-number v-decorator="[ 'convertCount', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="封面点击率">
          <a-input-number v-decorator="[ 'fengmianClickRate', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="转化点击率">
          <a-input-number v-decorator="[ 'convertLickRate', {}]" />
        </a-form-item>
		
      </a-form>
    </a-spin>
    <a-button type="primary" @click="handleOk">确定</a-button>
    <a-button type="primary" @click="handleCancel">取消</a-button>
  </a-drawer>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "KuaishouReportDailyAgentModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        date:{rules: [{ required: true, message: '请输入日期!' }]},
        accountId:{rules: [{ required: true, message: '请输入账户ID!' }]},
        },
        url: {
          add: "/report/kuaishouReportDailyAgent/add",
          edit: "/report/kuaishouReportDailyAgent/edit",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'accountId','kid','advertiserName','costCampaignCount','balance','cost','xianjinCost','fandianCost','kuangfanCost','jiliCost','xinyongCost','fengmianShowCount','fengmianClickCount','sucaiShowCount','convertCount','fengmianClickRate','convertLickRate'))
		  //时间格式化
          this.form.setFieldsValue({date:this.model.date?moment(this.model.date):null})
          this.form.setFieldsValue({advertiserCreateTime:this.model.advertiserCreateTime?moment(this.model.advertiserCreateTime):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.date = formData.date?formData.date.format():null;
            formData.advertiserCreateTime = formData.advertiserCreateTime?formData.advertiserCreateTime.format():null;
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>