<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :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="treeData"
          :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-input
          v-decorator="['name', {rules: [{required: true, message: '名称不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="编码"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['no']" />
      </a-form-item>
      <a-form-item
        label="是否配件"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select v-decorator="['type', {initialValue: DictCache.VALUE.SPARE_USE_TYPE.QITA,rules: [{required: true, message: '备件类型不能为空'}]}]" 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>
      <a-form-item
        label="设备类型"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-tree-select
          style="width: 100%"
          :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
          :treeData="sbTypeTreeData"
          :treeNodeFilterProp="'title'"
          :showSearch="true"
          @change="modelTypeChange"
          v-decorator="['modelType']"
          placeholder="请选择"
        >
        </a-tree-select>
      </a-form-item>
      <a-form-item
        label="所属型号"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select
          v-decorator="['modelId']"
          placeholder="请选择">
          <a-select-option
            v-for="({id,name,model}) in modelList"
            :key="id"
            :label="model"
            :value="id">{{ name + model }}
          </a-select-option>
        </a-select>
      </a-form-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>
      <a-form-item
        label="备注"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['remark', {rules: [{required: false, message: '备注不能为空'}]}]" />
      </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 { addSpareType, updateSpareType, fetchSpareTypeTree } from '@/api/sqarepartmanage/sparetype'
import { fetchSbTypeTree } from '@/api/sb/type'
import { querySbInfo } from '@/api/sb/model'

export default {
  name: 'BaseSpareType',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      sbTypeTreeData: [],
      // 下拉框map
      typeMap: {},
      delFlagMap: {},
      treeData: [],
      modelList: []
    }
  },
  props: {
  },
  created () {
    // 下拉框map
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SPARE_USE_TYPE)
    this.delFlagMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.DELFLAG)
  },
  methods: {
    modelTypeChange (value) {
      console.log(value)
      querySbInfo({ typeId: value }).then(res => {
        this.modelList = res.data
      })
    },
    base (record) {
      this.setTree(record)
      this.visible = true
      // 如果是空标识添加
      const { form: { setFieldsValue } } = this
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        return
      }
      this.modalTitle = '编辑'
      if (this.BaseTool.Object.isBlank(record.id)) {
        this.modalTitle = '复制'
      }
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'no',
          'name',
          'modelType',
          'modelId',
          'type',
          'parentId',
          'sort',
          'remark'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        if (this.BaseTool.String.isBlank(values.id)) {
          addSpareType(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateSpareType(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)
      }
    },
    /**
     * 设置备件类别树
     */
    setTree (record = {}) {
      fetchSpareTypeTree({}).then(res => {
        this.treeData = res.data
      })
      fetchSbTypeTree().then(res => {
        this.sbTypeTreeData = res.data
      })
      if (record.modelType != null) {
        querySbInfo({ typeId: record.modelType }).then(res => {
          this.modelList = res.data.rows
        })
      }
    }

  }
}
</script>
