<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-input v-decorator="['sbId']" type="hidden"/>
      </a-form-item>

      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <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-col>
        <a-col :lg="12" :md="24" :sm="24">
          <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-col>
      </a-row>
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <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"
              v-decorator="['typeId', {rules: [{required: true, message: '设备类型不能为空'}]}]"
              placeholder="请选择"
            >
            </a-tree-select>
          </a-form-item>
        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <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-col>
      </a-row>
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <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-col>
      </a-row>
    </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 { addPartInfo, updatePartInfo } from '@/api/part/info'
import { fetchPartTypeTree } from '@/api/part/type'
import BaseTool from '../../../../utils/tool'

export default {
  name: 'BasePartInfo',
  components: {
  },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      type: 1,
      sbTypeTreeData: [],
      producers: [],
      statusMap: {},
      unitMap: {},
      levelMap: {},
      sourceTypeMap: {},
      guiges: [],
      sbId: null
    }
  },
  props: {
  },
  created () {
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PART_STATUS)
    this.levelMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PART_LEVEL)
    this.setOptions()
    this.setTree()
  },
  methods: {
    base (record, type) {
      this.visible = true
      // 1新增，2复制
      this.type = type == null ? 1 : 2
      const { form: { setFieldsValue } } = this
      this.sbId = record.sbId
      // 如果是空标识添加
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'sbId'
        ])))
      })
      if (this.BaseTool.Object.isBlank(record.id)) {
        if (type === 1) {
          this.modalTitle = '添加'
          return
        } else {
          this.modalTitle = '复制'
        }
      }
      if (this.BaseTool.Object.isNotBlank(record.id)) {
        this.modalTitle = '修改'
      }
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'no',
          'name',
          'typeId',
          'level',
          'sbId',
          'unit',
          'status',
          '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)) {
          addPartInfo(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updatePartInfo(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 () {
      fetchPartTypeTree().then(res => {
        this.sbTypeTreeData = res.data
      })
    },
    setOptions () {
    }
  }
}
</script>
