<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    class="ant-modal2"
    @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-input
          disabled
          v-decorator="['no', {rules: [{required: false, message: '备件编码不能为空'}]}]" />
      </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-tree-select
          style="width: 100%"
          :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
          :treeData="spareTypeTreeData"
          :treeNodeFilterProp="'title'"
          :showSearch="true"
          v-decorator="['typeId', {rules: [{required: true, message: '设备类别不能为空'}]}]"
          placeholder="请选择"
        >
        </a-tree-select>
      </a-form-item>

      <a-form-item
        label="备件等级"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select v-decorator="['level', {rules: [{required: true, message: '设备等级不能为空'}]}]" placeholder="请选择">
          <a-select-option
            v-for="(label,value) in levelMap"
            :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-input-number
          style="width: 100%"
          :min="0"
          :step="0.01"
          :formatter="BaseTool.Amount.formatter"
          :parser="BaseTool.Amount.parser"
          v-decorator="['initialValue', {rules: [{required: true, message: '备件价值不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="生产商"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select v-decorator="['producerId', {rules: [{required: true, message: '生产商不能为空'}]}]" placeholder="请选择">
          <a-select-option
            v-for="({value, label}) of producers"
            :key="value"
            :label="label"
            :value="value">{{ label }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item
        label="计量单位"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select v-decorator="['unit', {rules: [{required: true, message: '计量单位不能为空'}]}]" placeholder="请选择">
          <a-select-option
            v-for="(label,value) in unitMap"
            :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-input-number
          style="width: 100%"
          :min="1"
          v-decorator="['maxStock', {rules: [{required: true, message: '最高库存不能为空'}]}]" />
      </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="['minStock', {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 { addSparePartInfo, updateSparePartInfo } from '@/api/sqarepartmanage/sparepartinfo'
import { fetchSpareTypeTree } from '@/api/sqarepartmanage/sparetype'
import { fetchProducerOptions } from '@/api/firm/producer'

export default {
  name: 'BaseSparePartInfo',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      // 下拉框map
      levelMap: {},
      unitMap: {},
      spareTypeTreeData: [],
      producers: []
    }
  },
  props: {
  },
  created () {
    // 下拉框map
    this.levelMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SPARE_PART_INFO_LEVEL)
    this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_UNIT)
  },
  methods: {
    base (record) {
      this.setTree(record)
      this.setOptions()
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        return
      }
      this.modalTitle = '编辑'
      if (this.BaseTool.Object.isBlank(record.id)) {
        this.modalTitle = '复制'
      }
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'no',
          'name',
          'typeId',
          'level',
          'initialValue',
          'producerId',
          'unit',
          'currentStock',
          'maxStock',
          'minStock',
          '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)) {
          addSparePartInfo(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateSparePartInfo(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.spareTypeTreeData = res.data
      })
    },
    setOptions () {
      fetchProducerOptions().then(res => {
        this.producers = res.data
      })
    }

  }
}
</script>
