<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="['storeId', {rules: [{required: true, message: '仓库id不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="仓库"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          disabled
          style="width:70%;margin-right:5px"
          v-decorator="['storeName', {rules: [{required: true, message: '仓库名称不能为空'}]}]" />
        <a-button type="primary" @click="handleStoreSelect">选择仓库</a-button>
      </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-button type="primary" @click="handleSpareSelect">选择备件</a-button>-->
      </a-form-item>
      <a-form-item
        label="库存数量"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          disabled
          style="width: 100%"
          v-decorator="['storeNum']" />
      </a-form-item>
      <a-form-item
        label="价格"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          disabled
          style="width: 100%"
          v-decorator="['price']" />
      </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.00,rules: [{required: true, message: '领用数量不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="总价"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          disabled
          style="width: 100%"
          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="spareSelectModal" @selected="handleSpareSelected"/>
    <spare-pick-form-select-modal ref="sparePickFormSelectModal" @selected="handleSpareFormSelected"/>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addSparePickDetail, updateSparePickDetail } from '@/api/store/sparepickdetail'
import SpareStoreSelectModal from '@/views/store/sparestore/modules/SpareStoreSelectModal'
import SparePickFormSelectModal from '@/views/store/sparepickform/modules/SparePickFormSelectModal'
import StoreSelectModal from '@/views/store/store/modules/StoreSelectModal'

export default {
  name: 'BaseSparePickDetail',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      storeId:null,
      record: null,
      form: this.$form.createForm(this),
      visible: false
      // 下拉框map
    }
  },
  components: {
    StoreSelectModal,
    SpareStoreSelectModal,
    SparePickFormSelectModal
  },
  props: {
  },
  created () {
    // 下拉框map
  },
  methods: {
    base (record) {
      this.visible = true
      this.record = record
      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',
          'pickId',
          //'pickNo',
          //'storeId',
          //'storeName',
          'spareId',
          'spareName',
          'storeNum',
          'num',
          'price',
          'totalPrice'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
        }
        if(values.num > values.storeNum){
          this.$message.error("领用数量不能大于库存数量")
          this.confirmLoading = false
          return
        }
        values.id = new Date().getTime()
        this.$emit('selected', values)
        this.handleCancel(values)
        this.confirmLoading = false
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.storeId = null
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    },
    handleSpareSelect () {
      this.$refs.spareSelectModal.base({},{storeId:this.storeId, num:0})
    },
    handleSpareSelected (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
        }))
      })
    },
    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
        }))
      })
    },
    handleSpareFormSelect () {
      this.$refs.sparePickFormSelectModal.base()
    },
    handleSpareFormSelected (record, keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      console.log(row, 222)
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'pickId': key,
          'pickNo': row.pickNo
        }))
      })
    },
    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>
