<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="['spareId']" type="hidden"/>
      </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']" />
      </a-form-item>
      <a-form-item
        label="价格"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input-number
          disabled
          v-decorator="['price']" />
      </a-form-item>
      <a-form-item
        label="总价"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input-number
          disabled
          v-decorator="['totalPrice']" />
      </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'

export default {
  name: 'BaseOutStoreDetail',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false
      // 下拉框map
    }
  },
  components: {
  },
  props: {
  },
  created () {
    // 下拉框map
  },
  methods: {
    base (record) {
      this.visible = true
      // 如果是空标识添加
      this.modalTitle = '修改数量'
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'spareId',
          'num',
          'price',
          'totalPrice'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        this.handleCancel(values)
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('selected', values)
      }
    },
    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>
