<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-input v-decorator="['sbId']" type='hidden' />
        <a-input v-decorator="['spareId']" type='hidden' />
        <a-input v-decorator="['partId']" type='hidden' />
        <a-input v-decorator="['isSpecial']" type='hidden' />
        <a-input v-decorator="['num']" type='hidden' />
        <a-input v-decorator="['price']" type='hidden' />
        <a-input v-decorator="['totalPrice']" type='hidden' />
        <a-input v-decorator="['startDate']" type='hidden' />
      </a-form-item>
<!--      <a-form-item
        label="备件"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
        disable
      >
        <a-input
          style='width: 70%'
          disabled
          v-decorator="['spareInfoName', {rules: [{required: true, message: '备件不能为空'}]}]" />
        <a-button style='width: 30%' type='primary' @click='handleSpareStoreSelect'>选择备件</a-button>
      </a-form-item>-->
      <row-list :col='1'>
        <row-item>
          <a-form-item
            label='选择备件'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-input
              style='width: 70%'
              disabled
              v-decorator="['spareInfoName', {rules: [{required: true, message: '备件不能为空'}]}]" />
            <a-button style='width: 30%' type='primary' @click='handleSpareStoreSelect'>选择</a-button>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label='设备位置'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-input
              style='width: 70%'
              disabled
              v-decorator="['partName']" />
            <a-button style='width: 30%' type='primary' @click='partInfoHandleSelect'>选择</a-button>
          </a-form-item>
        </row-item>
        <row-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>
        </row-item>
        <row-item>
          <a-form-item
            label='备注'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-input
              v-decorator="['remark']" />
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
    <part-info-select-modal ref='partInfoSelectModal' @selected='partInfoHandleSelected' />
    <spare-store-select-modal ref="spareStoreSelectModal" @selected="handleSpareStoreSelected"/>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { stringify } from 'qs'
import { addInitialized } from '@/api/sqarepartmanage/sparepartused'
import PartInfoSelectModal from '@/views/part/info/modules/PartInfoSelectModal'
import SpareStoreSelectModal from '@/views/store/sparestore/modules/SpareStoreSelectModal'
import BaseTool from '@/utils/tool'
export default {
  name: 'SpecialSpareBaseForm',
  components: {
    PartInfoSelectModal,
    SpareStoreSelectModal
  },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      // 下拉框map
      statusMap: {},
      data: {}
    }
  },
  props: {},
  created () {
    // 下拉框map
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_STORE_FORM_STATUS)
  },
  methods: {
    base (record) {
      this.visible = true
      this.sbId = record.sbId
      this.modalTitle = '添加'
      // 如果是空标识添加
      /*if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        return
      }
      this.modalTitle = '编辑'
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'sbId',
          'changeDate',
          'isSpecial',
          'spareId',
          'num',
          'partName',
          'partId',
          'spareInfoName',
          'price',
          'totalPrice',
          'startDate'
        ])))
      })*/
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      const then = this
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        values.changeDate = this.BaseTool.Date.formatter(values.changeDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        if (this.BaseTool.String.isBlank(values.id)) {
          addInitialized(values)
            .then(() => {
              this.confirmLoading = false
              this.visible = false
              then.$emit('ok')
            }).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)
      } else {
        this.$emit('ok')
      }
    },
    // 部位选择
    partInfoHandleSelect () {
      const params = { sbId: this.sbId }
      this.$refs.partInfoSelectModal.base(params)
    },
    partInfoHandleSelected (keys, rows) {
      const [key] = keys
      const [row] = rows
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'partId': key,
          'partName': row.name
        }))
      })
    },
    // 备件选择
    handleSpareStoreSelect () {
      this.$refs.spareStoreSelectModal.base({}, { repairFlag: 2, isSpecial: 1 })
    },
    handleSpareStoreSelected (record, keys, rows) {
      const data = {}
      for (let i = 0; i < rows.length; i++) {
        data.isSpecial = 1
        data.sbId = this.sbId
        data.spareId = rows[i].spareId
        data.num = 1
        data.price = rows[i].price
        data.totalPrice = data.price
        data.startDate = BaseTool.Date.formatter(new Date(), BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        data.spareInfoName = rows[i].spareName
      }
      this.form.setFieldsValue({
        'spareInfoName': data.spareInfoName,
        'sbId': this.sbId,
        'isSpecial': 1,
        'spareId': data.spareId,
        'num': 1,
        'price': data.price,
        'totalPrice': data.totalPrice,
        'startDate': data.startDate
      })
    }
  }
}
</script>
