<template>
  <a-modal
    :title="modalTitle"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    ok-text="保存"
    cancel-text="关闭"
    @ok="handleSubmit"
    @cancel="handleCancel"
    :maskClosable="false"
  >
    <a-spin :spinning="formLoading">
      <a-form :form="form">
        <a-form-item
          :required="true"
          label="所属类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select
            style="width: 100%"
            placeholder="请选择所属类型"
            v-decorator="['systemDictType', {rules: [{ required: true, message: '请选择所属类型！' }]}]"
          >
            <a-select-option
              v-for="(item,index) in typeList"
              :key="index"
              :value="item.code"
            >{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="分类名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入分类名称"
            v-decorator="['systemDictClassify', {rules: [{required: true, message: '请输入分类名称！'}]}]"
          />
        </a-form-item>
        <a-form-item label="子分类名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入子分类名称"
            v-decorator="['systemDictSubclassify', {rules: [{required: true, message: '请输入子分类名称！'}]}]"
          />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="排序"
          :hasFeedback="true"
          :required="true"
        >
          <a-input
            placeholder="请输入排序号"
            v-decorator="['systemDictSort', {rules: [{validator: checkSortIndex}]}]"
          />
        </a-form-item>
        <a-form-item label="是否上线" :labelCol="labelCol" :wrapperCol="wrapperCol" :required="true">
          <a-radio-group name="systemDictIsLine" v-decorator="['systemDictIsLine',{ initialValue: 1 }]">
            <a-radio :value="1">上线</a-radio>
            <a-radio :value="0">下线</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import homeApi from '@/api/homeApi'

export default {
  data () {
    return {
      modalTitle: '新增类型字典',
      typeList: homeApi.systemTypeList,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      visible: false,
      confirmLoading: false,
      formLoading: false,
      form: this.$form.createForm(this)
    }
  },
  mounted () {
  },
  methods: {
    // 初始化方法
    add (record) {
      this.visible = true
      // this.formLoading = true
      this.$nextTick(() => {
        this.form.setFieldsValue({ 'systemDictType': record.systemDictType })
      })
    },
    handleSubmit () {
      const {
        form: { validateFields }
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          values.systemDictSort = parseInt(values.systemDictSort)
          homeApi.addSystemDict(values)
            .then(res => {
              if (res.status === 200) {
                this.$message.success('新增成功')
                this.visible = false
                this.confirmLoading = false
                this.$emit('ok', values)
                this.form.resetFields()
              } else {
                this.$message.error('新增失败：' + res.msg)
              }
            })
            .finally(res => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    },
    checkSortIndex (rule, value, callback) {
      var text = '请输入排序号！'
      if (!value) {
        callback(text)
      } else if (/(^[1-9]\d*$)/.test(value)) {
        callback()
      } else {
        text = '请输入数字'
        callback(text)
      }
    }
  }
}
</script>
