<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="['id', {rules: [{required: true, message: 'id不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="物品编号"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          disabled
          v-decorator="['no']" />
      </a-form-item>
      <a-form-item
        label="物品名称"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          disabled
          v-decorator="['name']" />
      </a-form-item>
      <a-form-item
        label="采购/发运数量"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          disabled
          v-decorator="['purchaseNum']" />
      </a-form-item>
      <a-form-item
        label="采购/发运单价"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          disabled
          v-decorator="['purchasePrice']" />
      </a-form-item>
      <a-form-item
        label="采购/发运总价"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          disabled
          v-decorator="['purchaseTotalPrice']" />
      </a-form-item>
      <a-form-item
        label="数量"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input-number
          @blur="calcAmount"
          style="width: 100%"
          :min="1"
          step="0.01"
          v-decorator="['num', {initialValue:1,rules: [{required: true, message: '数量不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="单价"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input-number
          @blur="calcAmount"
          style="width: 100%"
          :min="0"
          :step="0.01"
          :formatter="BaseTool.Amount.formatter"
          :parser="BaseTool.Amount.parser"
          v-decorator="['price', {initialValue:0,rules: [{required: true, message: '单价不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="总价"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input-number
          style="width: 100%"
          :min="0"
          :step="0.01"
          :formatter="BaseTool.Amount.formatter"
          :parser="BaseTool.Amount.parser"
          v-decorator="['totalPrice', {initialValue:0,rules: [{required: true, message: '总价不能为空'}]}]" />
      </a-form-item>

    </a-form>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
    <store-select-modal ref="storeSelectModal" @selected="handleStoreSelected"/>
    <spare-part-info-select-modal ref="spareSelectModal" @selected="handleSpareSelected"/>

  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addInStoreDetail, updateInStoreDetail } from '@/api/sb/sbcheckdetail'
import StoreSelectModal from '@/views/store/store/modules/StoreSelectModal'
import SparePartInfoSelectModal from '@/views/sqarepartmanage/sparepartinfo/modules/SparePartInfoSelectModal'
import SbCheckFormSelectModal from '@/views/sb/sbcheckform/modules/SbCheckFormSelectModal'

export default {
  name: 'BaseInStoreDetail',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      id:null,
      form: this.$form.createForm(this),
      visible: false
      // 下拉框map
    }
  },
  components: {
    StoreSelectModal,
    SparePartInfoSelectModal,
    SbCheckFormSelectModal
  },
  props: {
  },
  created () {
    // 下拉框map
  },
  methods: {
    base (record) {
      this.visible = true
      this.id = record.id
      this.modalTitle = '编辑'
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'no',
          'name',
          'purchaseNum',
          'purchasePrice',
          'purchaseTotalPrice',
          'num',
          'price',
          'totalPrice'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        if (this.BaseTool.String.isBlank(values.id)) {
          addInStoreDetail(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateInStoreDetail(values)
            .then(() => {
              if (this.BaseTool.Object.isNotBlank(values)) {
                this.$emit('ok', values)
              }
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
    },
    handleStoreSelect () {
      this.$refs.storeSelectModal.base()
    },
    handleStoreSelected (record, keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'storeId': key,
          'storeName': row.name
        }))
      })
    },
    handleSpareSelect () {
      this.$refs.spareSelectModal.base()
    },
    handleSpareSelected (record, keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'spareId': key,
          'spareName': row.name
        }))
      })
    },
    handleSelectInNo () {
      this.$refs.inStoreFormSelectModal.base()
    },
    handleInNoSelected (record, keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'inId': key,
          'inNo': row.inNo
        }))
      })
    },
    calcAmount () {
      const { form: { setFieldsValue, getFieldValue } } = this
      const num = getFieldValue('num')
      const price = getFieldValue('price')
      const totalPrice = this.BaseTool.Number.mul(num, price)
      setFieldsValue({
        totalPrice
      })
    }

  }
}
</script>
