<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
          v-decorator="['spareName', {rules: [{required: true, message: '备件名称不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="库存数量"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          disabled
          v-decorator="['storeNum']" />
      </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"
          :min="1"
          step="0.01"
          v-decorator="['price']" />
      </a-form-item>
      <a-form-item
        label="总价"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['totalPrice']" />
      </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-store-select-modal ref="spareStoreSelectModal" @selected="handleSpareStoreSelected"/>
    <out-store-form-select-modal ref="outStoreFormSelectModal" @selected="handleOutNoSelected"/>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import StoreSelectModal from '@/views/store/store/modules/StoreSelectModal'
import SpareStoreSelectModal from '@/views/store/sparestore/modules/SpareStoreSelectModal'
import OutStoreFormSelectModal from '@/views/store/outstoreform/modules/OutStoreFormSelectModal'

export default {
  name: 'BaseOutStoreDetail',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false
      // 下拉框map
    }
  },
  components: {
    StoreSelectModal,
    SpareStoreSelectModal,
    OutStoreFormSelectModal
  },
  props: {
  },
  created () {
    // 下拉框map
  },
  methods: {
    base (record) {
      this.visible = true
      this.storeId = record.storeId
      // 如果是空标识添加
      const { form: { setFieldsValue } } = this
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        this.$nextTick(() => {
          setFieldsValue(Object.assign(pick(record, [
            'storeId',
            'storeName'
          ])))
        })
        return
      }
      this.modalTitle = '编辑'

      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'spareId',
          'num',
          'price',
          'totalPrice',
          'spareName',
          'storeNum'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true

      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        if (values.num > values.storeNum) {
          this.$message.error('调拨数量不能大于库存数量')
          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)
      }
    },
    handleStoreSelect () {
      this.$refs.storeSelectModal.base()
    },
    handleStoreSelected (record, keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      this.storeId = key
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'storeId': key,
          'storeName': row.name
        }))
      })
    },
    handleSpareStoreSelect () {
      this.$refs.spareStoreSelectModal.base({}, { storeId: this.storeId })
    },
    handleSpareStoreSelected (record, keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'spareId': row.spareId,
          'spareName': row.spareName,
          'storeNum': row.num
          // 'price':row.price
        }))
      })
    },
    handleOutNoSelect () {
      this.$refs.outStoreFormSelectModal.base()
    },
    handleOutNoSelected (record, keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'outId': key,
          'outNo': row.outNo
        }))
      })
    },
    calcAmount () {
      const { form: { setFieldsValue, getFieldValue } } = this
      const num = getFieldValue('num')
      const price = getFieldValue('price')
      console.log(num, price)
      const totalPrice = this.BaseTool.Number.mul(num, price)
      setFieldsValue({
        totalPrice
      })
    }

  }
}
</script>
