<template>
  <a-modal :title="title" :width="800" :ok=false :visible="visible" :confirmLoading="confirmLoading"
    :okButtonProps="{ props: {disabled: disableSubmit} }" @ok="handleOk" @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="标签分类" :hidden="false" hasFeedback>
          <a-select v-decorator="['tagCategoryId', {rules: [{ required: true, message: '请选择标签分类'}]} ]"
            placeholder="请选择">
            <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-option :value="4">场景</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="标签名称" :hidden="false" hasFeedback>
          <a-input id="tagName" placeholder="请输入标签名称" v-decorator="['tagName', validatorRules.departName ]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="标签编码">
          <a-input placeholder="请输入标签编码,以字母加数字拼接,如A001" v-decorator="['tagCode',validatorRules.orgCode]" :maxLength="4" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="标签描述">
          <a-input placeholder="请输入标签描述" v-decorator="['description']" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="标签示例">
          <a-input placeholder="请输入标签示例" v-decorator="['model']" />
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    httpAction
  } from '@/api/manage'
  import {
    queryIdTree
  } from '@/api/api'
  import pick from 'lodash.pick'
  import ATextarea from 'ant-design-vue/es/input/TextArea'
  export default {
    name: "SysDepartModal",
    components: {
      ATextarea
    },
    data() {
      return {
        departTree: [],
        orgTypeData: [],
        phoneWarning: '',
        departName: "",
        title: "操作",
        seen: false,
        visible: false,
        condition: true,
        disableSubmit: false,
        model: {},
        menuhidden: false,
        menuusing: true,
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 5
          },
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          departName: {
            rules: [{
              required: true,
              message: '请输入标签名称!'
            }]
          },
          orgCode: {
            rules: [{
              required: true,
              message: '请输入标签编码!'
            }], validator: this.validate
          },
          mobile: {
            rules: [{
              validator: this.validateMobile
            }]
          }
        },
        url: {
          add: "/ctop/tagInfo/add",
        },
        dictDisabled: true,
      }
    },
    created() {},
    methods: {
      validate(rule, value, callback) {
        const regex = /^http(.*)$/
        if (!regex.test(value)) {
          callback('需要以 http 开头')
        }
        callback()
      },
      loadTreeData() {
        var that = this;
        queryIdTree().then((res) => {
          if (res.success) {
            that.departTree = [];
            for (let i = 0; i < res.result.length; i++) {
              let temp = res.result[i];
              that.departTree.push(temp);
            }
          }

        })
      },
      add(depart) {
        this.edit(depart);
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, {});
        this.visible = true;
        // this.loadTreeData();
        this.model = record
        console.log(this.model)
        //   if(this.seen){
        //     this.model.orgCategory = '1';
        //   }else{
        //     this.model.orgCategory = '2';
        //   }
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'tagCategoryId'))
        });
      },
      close() {
        this.$emit('close');
        this.disableSubmit = false;
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            //时间格式化
            console.log(formData)
            httpAction(this.url.add, formData, "post").then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.loadTreeData();
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })

          }
        })
      },
      handleCancel() {
        this.close()
      },
      validateMobile(rule, value, callback) {
        if (!value || new RegExp(/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/).test(value)) {
          callback();
        } else {
          callback("您的手机号码格式不正确!");
        }

      }
    }
  }
</script>

<style scoped>

</style>