<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="联系人姓名">
          <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/JTreeSelect'
  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: '请输入联系人手机!'
            },{
              validator:this.mobileChecked
            }
            ]
          },
          email: {
            rules: [
              {
                type: 'email',
                message: '邮箱格式不正确，请重新输入',
              },
              {
                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
      },
      mobileChecked(rule, value, callback){
        // console.log(rule, value, callback)
            var strRegex = /^1[0-9]{10}$/
            var re = new RegExp(strRegex)
            // console.log(re.test(res))
            if (re.test(value)) {
                callback()
            } else if(value && !re.test(value)){
                callback('手机号码不正确，请正确输入')
            }else{
              callback()
            }
      }
    }
  }
</script>

<style lang="less" scoped>

</style>