<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="备件id"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
        v-show="false"
      >
        <a-input
          v-decorator="['spareId', {rules: [{required: true, message: '备件id不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="备件名称"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          disabled
          style="width:70%;margin-right:5px"
          v-decorator="['spareName', {rules: [{required: true, message: '备件名称不能为空'}]}]" />
        <a-button type="primary" @click="handleSparePartUsedSelect">选择已替换备件</a-button>
      </a-form-item>

    </a-form>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
    <spare-recovery-form-select-modal ref="spareRecoveryFormSelectModal" @selected="handleSpareFormSelected"/>
    <spare-part-used-select-modal ref="sparePartUsedSelectModal" @selected="handleSparePartUsedSelected"/>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addSparePickDetail, updateSparePickDetail } from '@/api/store/sparerecoverydetail'
import SpareRecoveryFormSelectModal from '@/views/store/sparerecoveryform/modules/SpareRecoveryFormSelectModal'
import SparePartUsedSelectModal from '@/views/sqarepartmanage/sparepartused/modules/SparePartUsedSelectModal'
export default {
  name: 'BaseSpareRecoveryDetail',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false
      // 下拉框map
    }
  },
  components: {
    SpareRecoveryFormSelectModal,
    SparePartUsedSelectModal
  },
  props: {
  },
  created () {
    // 下拉框map
  },
  methods: {
    base (record) {
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        return
      }
      this.modalTitle = '编辑'
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'pickId',
          'pickNo',
          'spareId',
          'spareName'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
        }
        values.id = new Date().getTime()
        this.$emit('selected', values)
        this.handleCancel(values)
        this.confirmLoading = false
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    },
    handleSparePartUsedSelect () {
      this.$refs.sparePartUsedSelectModal.base({},{status:2})
    },
    handleSparePartUsedSelected (record, keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'spareId': key,
          'spareName': row.spareName
        }))
      })
    },
    handleSpareFormSelect () {
      this.$refs.spareRecoveryFormSelectModal.base()
    },
    handleSpareFormSelected (record, keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      console.log(row, 222)
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'pickId': key,
          'pickNo': row.pickNo
        }))
      })
    }

  }
}
</script>
