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

      <a-form-item
        label="设备名称"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
        disable
      >
        <a-input
          disabled
                  v-decorator="['sbName', {rules: [{required: false, message: '设备不能为空'}]}]" />
      </a-form-item>
      <!--<a-form-item
        label="部件名称"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select v-decorator="['partId']" placeholder="请选择">
          <a-select-option
            v-for="({value, label}) of partInfoOptions"
            :key="value"
            :label="label"
            :value="value">{{ label }}
          </a-select-option>
        </a-select>
      </a-form-item>-->

      <a-form-item
          label="备件BOM"
          :labelCol="BaseTool.Constant.labelCol"
          :wrapperCol="BaseTool.Constant.wrapperCol"
        >
          <a-select v-decorator="['spareId', {rules: [{required: true, message: '备件不能为空'}]}]" placeholder="请选择">
            <a-select-option
              v-for="({spareId, name}) of spareInfoList"
              :key="spareId"
              :label="name"
              :value="spareId">{{ name }}
            </a-select-option>
          </a-select>
        </a-form-item>


        <a-form-item
        label="投用日期"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
        >
          <a-date-picker
            style="width: 100%"
            :format="BaseTool.Date.PICKER_NORM_DATE_PATTERN"
            v-decorator="['startDate', {rules: [{required: true, message: '投用日期不能为空'}]}]" />
        </a-form-item>

      <a-form-item
        label="使用状态"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select v-decorator="['status', {rules: [{required: true, message: '使用状态不能为空'}]}]" placeholder="请选择">
          <a-select-option
            v-for="(label,value) in statusMap"
            :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-date-picker
          style="width: 100%"
          :format="BaseTool.Date.PICKER_NORM_DATE_PATTERN"
          v-decorator="['changeDate', {rules: [{required: true, message: '预计报废日期不能为空'}]}]" />
      </a-form-item>

      <a-form-item
        label="实际更换日期"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-date-picker
          style="width: 100%"
          :format="BaseTool.Date.PICKER_NORM_DATE_PATTERN"
          v-decorator="['realChangeDate']" />
      </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 { addSparePartUsed, updateSparePartUsed } from '@/api/sqarepartmanage/sparepartused'
import { getSbInfoTree } from '@/api/sb/info'
import { fetchPartInfoOptions } from '@/api/part/info'
import { fetchSparePartInfoOptions } from '@/api/sqarepartmanage/sparepartinfo'
import { selectSpareInfoListByModelId } from '@/api/sb/modelbom'

export default {
  name: 'BaseSparePartUsed',
  components: {

  },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      sbId: null,
      modelId:null,
      record: null,
      // 下拉框map
      statusMap: {},
      sbInfoTreeData: [],
      partInfoOptions: [],
      spareInfoList:[],
      sparePartInfoOptions: []
    }
  },
  props: {
  },
  created () {
    // 下拉框map
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SPARE_PART_USED_STATUS)
  },
  methods: {
    base (record,obj) {

      this.sbId = obj.sbId
      this.modelId = obj.modelId

      this.setTree(record)
      this.visible = true
      this.record = record
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        const { form: { setFieldsValue } } = this

        this.$nextTick(() => {
          setFieldsValue(Object.assign(pick(obj, [
            'sbId',
            'sbName'
          ])))
        })
        return
      }
      this.modalTitle = '编辑'
      const { form: { setFieldsValue } } = this
      // 日期处理
      record.startDate = this.BaseTool.Moment(record.startDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
      record.changeDate = this.BaseTool.Moment(record.changeDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
      record.realChangeDate = this.BaseTool.Moment(record.realChangeDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'storeId',
          'sbId',
          'partId',
          'spareId',
          'startDate',
          'status',
          'changeDate',
          'realChangeDate',
          'remark'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        values.startDate = this.BaseTool.Date.formatter(values.startDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        values.changeDate = this.BaseTool.Date.formatter(values.changeDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        values.realChangeDate = this.BaseTool.Date.formatter(values.realChangeDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        if (this.BaseTool.String.isBlank(values.id)) {
          addSparePartUsed(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateSparePartUsed(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 = {}) {
      getSbInfoTree().then(res => {
        this.sbInfoTreeData = res.data
      })
      fetchPartInfoOptions().then(res => {
        this.partInfoOptions = res.data
      })
      selectSpareInfoListByModelId({id:this.modelId}).then(res => {
        this.spareInfoList = res.data
      })
    }

  }
}
</script>
