<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="备件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="['initNo']" />
      </a-form-item>
      <a-form-item
        label="规格型号"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          disabled
          v-decorator="['ggxh']" />
      </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"
          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']" />
      </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']" />
      </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'
import StoreSelectModal from '@/views/store/store/modules/StoreSelectModal'
import SparePartInfoSelectModal from '@/views/sqarepartmanage/sparepartinfo/modules/SparePartInfoSelectModal'
import InStoreFormSelectModal from '@/views/store/instoreform/modules/InStoreFormSelectModal'

export default {
  name: 'BaseInStoreDetail',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false
      // 下拉框map
    }
  },
  components: {
    StoreSelectModal,
    SparePartInfoSelectModal,
    InStoreFormSelectModal
  },
  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',
          'ggxh',
          'initNo',
          'num',
          'price',
          'totalPrice',
          'spareName'
        ])))
      })
    },
    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)
      }
    },
    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,
          'ggxh': row.ggxh,
          'initNo': row.initNo,
          'price': row.initialValue,
          'totalPrice': row.price
        }))
      })
    },
    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')
      console.log(num, price)
      const totalPrice = this.BaseTool.Number.mul(num, price)
      setFieldsValue({
        totalPrice
      })
    }

  }
}
</script>
