<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-form :form="form">
      <row-list :col="1" v-show="false">
        <row-item>
          <a-form-item>
            <a-input v-decorator="['id']" type="hidden"/>
          </a-form-item>
        </row-item>
      </row-list>
      <row-list :col="1">
        <row-item>
          <a-form-item
            label="名称"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['name', {rules: [{required: true, message: '名称不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="字段名称"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['fieldKey', {rules: [{required: true, message: '字段名称不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="表头显示"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              v-decorator="['useType', {rules: [{required: true, message: '表头显示不能为空'}]}]"
              placeholder="请选择">
              <a-select-option
                v-for="(label,value) in useTypeMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="类型"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              v-decorator="['type', {rules: [{required: true, message: '类型不能为空'}]}]"
              @change="typeChange"
              placeholder="请选择">
              <a-select-option
                v-for="(label,value) in typeMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>

        <row-item v-show="isDictType">
          <a-form-item
            label="数据词典类型"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['dictType', {rules: [{required: isDictType, message: '数据词典类型不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="标识"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              v-decorator="['codeValue', {rules: [{required: true, message: '标识不能为空'}]}]"
              placeholder="请选择">
              <a-select-option
                v-for="(label,value) in codeValueMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="性质"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              v-decorator="['nature', {rules: [{required: true, message: '性质不能为空'}]}]"
              placeholder="请选择">
              <a-select-option
                v-for="(label,value) in natureMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="排序"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input-number
              style="width: 100%"
              :min="1"
              v-decorator="['sort', {rules: [{required: true, message: '排序不能为空'}]}]"/>
          </a-form-item>
        </row-item>
      </row-list>
      <row-list :col="1">
        <row-item>
          <a-form-item
            label="备注"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-textarea
              :rows="4"
              v-decorator="['remark']"/>
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addFillInfo, updateFillInfo } from '@/api/fill/info'

export default {
  name: 'BaseFillInfo',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      isDictType: false,
      visible: false,
      // 下拉框map
      useTypeMap: {},
      typeMap: {},
      codeValueMap: {},
      natureMap: {}
    }
  },
  props: {},
  created () {
    // 下拉框map
    this.useTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.FILL_INFO_USE_TYPE)
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.FILL_INFO_TYPE)
    this.natureMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.FILL_INFO_NATURE)
    this.codeValueMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.FILL_INFO_CODE_VALUE)
  },
  methods: {
    base (record) {
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        this.isDictType = false
        return
      }
      this.modalTitle = '编辑'
      const { form: { setFieldsValue } } = this
      this.isDictTypeShow(record.type)
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'name',
          'fieldKey',
          'useType',
          'type',
          'codeValue',
          'dictType',
          'nature',
          'remark',
          'sort'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        if (this.BaseTool.String.isBlank(values.id)) {
          addFillInfo(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateFillInfo(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    isDictTypeShow (type) {
      console.log(222, this.DictCache.VALUE.FILL_INFO_TYPE.FILL_INFO_TYPE_XIA_LA === type ||
        this.DictCache.VALUE.FILL_INFO_TYPE.FILL_INFO_TYPE_DAN_XUAN === type)
      this.isDictType = this.DictCache.VALUE.FILL_INFO_TYPE.FILL_INFO_TYPE_XIA_LA === type ||
      this.DictCache.VALUE.FILL_INFO_TYPE.FILL_INFO_TYPE_DAN_XUAN === type
    },
    typeChange (val) {
      console.log(111, val)
      this.isDictTypeShow(val)
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    }

  }
}
</script>
