<template>
  <a-card :bordered='false' v-show='visible' class='card' :title='modalTitle'>
    <a-row :gutter='48' slot='extra'>
      <a-col :md='48' :sm='48'>
              <span class='table-page-search-submitButtons' style='float: right'>
                <a-button :loading='confirmLoading' type='primary' @click='save()'>保存</a-button>
                <a-button style='margin-left: 8px' type='default' @click='handleCancel()'>返回</a-button>
              </span>
      </a-col>
    </a-row>
    <a-form :form='form'>

      <a-form-item v-show='false'>
        <a-input v-decorator="['id']" type='hidden' />
        <a-input v-decorator="['采购入库单']" type='hidden' />
        <a-input v-decorator="['备件']" type='hidden' />
      </a-form-item>

      <row-list :col='2'>
        <row-item>

          <a-form-item
            label='价值'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-input-number
              style='width: 100%'
              :min='0'
              :formatter='BaseTool.Amount.formatter'
              :parser='BaseTool.Amount.parser'
              v-decorator="['price', {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-number
              style='width: 100%'
              :min='0'
              :formatter='BaseTool.Amount.formatter'
              :parser='BaseTool.Amount.parser'
              v-decorator="['totalPrice', {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-number
              style='width: 100%'
              :min='0'
              :formatter='BaseTool.Amount.formatter'
              :parser='BaseTool.Amount.parser'
              v-decorator="['num', {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
              style='width: 70%'
              disabled
              v-decorator="['purchaseStoreFormName', {rules: [{required: true, message: '设备名称不能为空'}]}]" />
            <a-button style='width: 30%' type='primary' @click='purchaseStoreFormHandleSelect'>选择</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="['sparePartInfoName', {rules: [{required: true, message: '设备名称不能为空'}]}]" />
            <a-button style='width: 30%' type='primary' @click='sparePartInfoHandleSelect'>选择</a-button>
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
    <purchase-store-form-select-modal ref='purchaseStoreFormSelectModal' @selected='purchaseStoreFormHandleSelected' />
    <spare-part-info-select-modal ref='sparePartInfoSelectModal' @selected='sparePartInfoHandleSelected' />
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import { addPurchaseStoreFormDetail, updatePurchaseStoreFormDetail } from '@/api/store/purchaseformdetail'
import PurchaseStoreFormSelectModal from '@/views/store/purchaseform/modules/PurchaseStoreFormSelectModal'
import SparePartInfoSelectModal from '@/views/sqarepartmanage/sparepartinfo/modules/SparePartInfoSelectModal'
import { Ellipsis, STable } from '@/components'
import Detail from '@/views/store/purchaseformdetail/modules/Detail'

export default {
  name: 'BasePurchaseStoreFormDetail',
  components: {
    STable,
    Ellipsis,
    Detail,
    PurchaseStoreFormSelectModal,
    SparePartInfoSelectModal
  },
  data() {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false
      // 下拉框map
    }
  },
  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',
          'price',
          'totalPrice',
          'num',
          'purchaseStoreFormId',
          'sparePartInfoId'
        ])))
      })
    },
    save() {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        if (this.BaseTool.String.isBlank(values.id)) {
          addPurchaseStoreFormDetail(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
            this.confirmLoading = false
          })
        } else {
          updatePurchaseStoreFormDetail(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)
      } else {
        this.$emit('ok')
      }
    }
    ,
    purchaseStoreFormHandleSelect() {
      this.$refs.purchaseStoreFormSelectModal.base({}, {})
    },
    purchaseStoreFormHandleSelected(keys, rows) {
      const [key] = keys
      const [row] = rows
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'purchaseStoreFormId': key,
          'purchaseStoreFormName': row.name
        }))
      })
    },
    sparePartInfoHandleSelect() {
      this.$refs.sparePartInfoSelectModal.base({}, {})
    },
    sparePartInfoHandleSelected(keys, rows) {
      const [key] = keys
      const [row] = rows
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'sparePartInfoId': key,
          'sparePartInfoName': row.name
        }))
      })
    }
  }
}
</script>
