<template>
  <a-modal
    :title="modalTitle"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-form :form="form">

      <a-form-item v-show="false" >
        <a-input v-decorator="['id']" type="hidden"/>
      </a-form-item>
      <a-form-item
        label="所属父类型"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-tree-select
          style="width: 100%"
          :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
          :treeData="parentTree"
          :treeNodeFilterProp="'title'"
          :showSearch="true"
          v-decorator="['parentId']"
          placeholder="请选择"
        >
        </a-tree-select>
      </a-form-item>
      <a-form-item
        label="是否父类"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select v-decorator="['childFlag',{initialValue: '0'}]" placeholder="请选择">
           <a-select-option value="1">
             是
           </a-select-option>
           <a-select-option value="0">
             否
           </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item
        label="类型"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['type', {rules: [{required: true, message: '类型不能为空'}]}]" />
      </a-form-item>

      <a-form-item
        label="类型名称"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input v-decorator="['typeName', {rules: [{required: true, message: '类型名称不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="标识"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input v-decorator="['code', {rules: [{required: true, message: '标识不能为空'}]}]"/>
      </a-form-item>
      <a-form-item
        label="名称"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input v-decorator="['label', {rules: [{required: true, message: '名称不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="值类型"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select v-decorator="['valueType', {rules: [{required: true, message: '值类型不能为空'}]}]" placeholder="请选择">
          <a-select-option
            v-for="(value,key) in valueTypeMap"
            :key="key"
            :label="key"
            :value="key">{{ value }}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item
        label="值"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input v-decorator="['value', {rules: [{required: true, message: '值不能为空'}]}]" />
      </a-form-item>


      <a-form-item
        label="排序"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input v-decorator="['sort', {initialValue:1,rules: [{required: true, message: '排序不能为空'}]}]" type="number" />
      </a-form-item>

      <a-form-item
        label="描述"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input v-decorator="['description']"/>
      </a-form-item>
    </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 {getDictTree, getDictPage, addDict, updateDict } from '@/api/upms/dict'

export default {
  name: 'BaseDict',
  data () {
    return {
      checked: true,
      confirmLoading: false,
      currentStep: 0,
      mdl: {},
      parentTree:[],
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false
    }
  },
  props: {
    valueTypeMap: {
      type: Object,
      default: () => ({})
    }
  },
  methods: {
    base (record) {
      this.visible = true
      getDictTree()
      .then(res => {
        this.parentTree = res.data
      })
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        return
      }
      this.modalTitle = '编辑'
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        // setFieldsValue(pick(record, ['id', 'type', 'typeName', 'label', 'value', 'valueType', 'sort', 'code']))
        setFieldsValue(Object.assign(pick(record, ['id', 'type','childFlag', 'typeName','parentId', 'label', 'value', 'sort', 'code', 'description']),
          { 'valueType': record.valueType + '' }))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        if (this.BaseTool.String.isBlank(values.id)) {
          addDict(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateDict(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    }

  }
}
</script>
