<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="广告主开户主体名称">
          <a-input placeholder="请输入广告主开户主体名称" v-decorator="['name', validatorRules.name ]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="行业">
          <j-tree-select ref="treeSelect" placeholder="请选择行业" v-decorator="['industryId', validatorRules.pid]"
            dict="sys_category,name,id" pidField="pid" pidValue="f5cb2d2d28417b3b65a6dd744bcb9a76">
          </j-tree-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客户类型">
          <a-select v-decorator="['advertiserType',{ rules: [{ required: true, message: '请选择客户类型' }] }]" 
          @change="changeData">
            <a-select-option :value="1">自拓客户</a-select-option>
            <a-select-option :value="2">公司客户</a-select-option>
            <a-select-option :value="3">代理商</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="销售"
          v-if="model.advertiserType==1||model.advertiserType==2">
          <a-select showSearch optionFilterProp="children" style="width: 100%" :filterOption="filterOption"
            v-decorator="['saleId', { rules: [{  required: true,message: '请选择销售' }] }]">
            <a-select-option v-for="appModel in options" :key="appModel.userId + new Date().getTime()"
              :value="appModel.userId">{{ appModel.realName }}&#12288;&#12288;{{ appModel.roleName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="代理商"
          v-if="model.advertiserType==3">
          <a-select showSearch optionFilterProp="children" style="width: 100%" :filterOption="filterOption"
            v-decorator="['agentId', { rules: [{  required: true,message: '请选择代理商' }] }]">
            <a-select-option v-for="appModel in optionsTwo" :key="appModel.id + new Date().getTime()"
              :value="appModel.id">{{ appModel.name }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系人姓名">
          <a-input placeholder="请输入联系人姓名" v-decorator="['contact', validatorRules.contact ]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系人手机">
          <a-input placeholder="请输入联系人手机" v-decorator="['mobile', validatorRules.mobile ]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系人邮箱">
          <a-input placeholder="请输入联系人邮箱" v-decorator="['email', validatorRules.email ]" />
        </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/noParentTreeSelect'
  import moment from "moment"
  import {
    mapGetters
  } from 'vuex'
  export default {
    name: "AdvertiserModal",
    components: {
      JTreeSelect
    },
    data() {
      return {
        title: "操作",
        visible: false,
        model: {},
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 5
          },
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          },
        },
        options: [],
        optionsTwo: [],
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          name: {
            rules: [{
              required: true,
              message: '请输入广告主名称!'
            }]
          },
          industryId: {
            rules: [{
              required: true,
              message: '请输入行业ID!'
            }]
          },
          contact: {
            rules: [{
              required: true,
              message: '请输入联系人姓名!'
            }]
          },
          mobile: {
            rules: [{
              required: true,
              message: '请输入联系人手机!'
            }]
          },
          email: {
            rules: [{
              required: true,
              message: '请输入联系人邮箱!'
            }]
          },
          pid: {
            rules: [{
              required: true,
              message: '请选择行业!'
            }]
          },
        },
        url: {
          add: "/ctop/advertiser/add",
          edit: "/ctop/advertiser/edit",
          getList: '/ctop/advertiser/list',
          getTwoList: '/ctop/agentInfo/list'
        },
        disableOne:true
      }
    },
    created() {
      // /ctop/agentInfo/list
    },
    methods: {
      ...mapGetters(['nickname', 'avatar', 'userInfo']),
      changeData(data){
          this.model.advertiserType = data
      },
      add() {
        this.edit({});
      },
      edit(record) {
        if(record.advertiserType){
            this.disableOne = true
        }else{
            this.disableOne = false
        }
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'name', 'industryId', 'contact', 'mobile', 'email',
            'advertiserType', 'saleId', 'agentId'))
          //时间格式化
        });
        getAction('/sys/user/getAllSaleList', '').then(res => {
          console.log(res)
          if (res.success) {
            this.options = res.result
          }
        })
        getAction('/ctop/agentInfo/list', '').then(res => {
          console.log(res)
          if (res.success) {
            this.optionsTwo = res.result.records
          }
        })
      },
      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.userId = this.userInfo().id
            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()
      },

      getFormFieldValue(field) {
        return this.form.getFieldValue(field)
      },
      filterOption(input, option) {
        return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      },
    }
  }
</script>

<style lang="less" scoped>

</style>