<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="快手ID">
          <a-input placeholder="请输入快手ID" disabled v-decorator="['ksid', validatorRules.ksid ]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="快手广告主ID">
          <a-input placeholder="请输入快手广告主ID" v-decorator="['kaid', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="快手昵称">
          <a-input placeholder="请输入快手昵称" v-decorator="['name', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="行业">
          <j-tree-select
            ref="treeSelect"
            placeholder="请选择行业"
            v-decorator="['industry', validatorRules.pid]"
            dict="sys_category,name,id"
            pidField="pid"
            pidValue="67d69117968be1eecb1e705b399b9d93">
          </j-tree-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="广告主企业名称">
          <a-input placeholder="请输入广告主企业名称" v-decorator="['company', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="推广产品">
          <a-input placeholder="请输入推广产品" v-decorator="['product', {}]"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {httpAction,getAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import JTreeSelect from '@/components/jeecg/JTreeSelect'
  import moment from "moment"

  export default {
    name: "KuaishouAdAccountModal",
    components: {
      JTreeSelect
    },
    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: {
          ksid: {rules: [{required: true, message: '请输入ksid!'}]},
        },
        url: {
          add: "/ctop/kuaishou/advertiser/add",
          edit: "/ctop/kuaishou/advertiser/edit",
        },
      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        let param = {
          field:'name',
          val:record.secondIndustry
        }
        this.visible = true;
        getAction('/sys/category/loadOne',param).then((res) => {
          if (res.success) {
            record.industry=res.result.id

          }
        }).finally(() => {
          this.model = Object.assign({}, record);
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.model, 'ksid', 'kaid', 'name', 'industry', 'company', 'product', 'status'))
            //时间格式化
          });
          console.log(this.model)
        })


      },
      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.ksid) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化

            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>

</style>