<template>
  <div>
    <Modal v-model="chemicalStockOutModal" :mask-closable="false">
      <p slot="header">
        耗材出库
      </p>
      <div>
        <Form id="storage-form-apply-storeOut" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="80">
          <Form-item label="数量" prop="quantity">
            <label style="width: 40%">
              <InputNumber v-model.number="formObj.quantity" :min="1" name="quantity" style="width: 100%" />
            </label>
            <label style="width: 10%;text-align: center">{{ formObj.unit }}</label>
            <input v-model="formObj.stockId" hidden name="stockId">
          </Form-item>
          <Form-item label="备注" prop="remark">
            <Input v-model="formObj.remark" name="remark" placeholder="请输入备注" /></Input>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <Button type="ghost" style="margin-left: 8px" @click="_cancel">
          取消
        </Button>
        <Button type="primary" @click="_ok">
          提交
        </Button>
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 添加编辑试验耗材库存表
 */
import { consumeRelDelivery } from '../../../api'
const defVal = {
  quantity: 1,
  unit: '',
  stockId: '', // 库存id
  remark: ''
}
export default {
  data() {
    const validateNumber = (rule, value, callback) => {
      const reg = /^\d+(\.\d{0,5})?$/
      if (reg.test(value)) {
        callback()
      } else {
        callback(new Error('小数点后只能输入五位'))
      }
    }
    return {
      id: '',
      formObj: defVal,
      chemicalStockOutModal: false,
      ruleValidate: {
        quantity: [
          {
            required: true,
            validator: validateNumber,
            trigger: 'change',
            type: 'number'
          }
        ]
      }
    }
  },
  methods: {
    _resultChange(msg) {
      this.formObj = defVal
      this.chemicalStockOutModal = false
      this.$Message.success(msg)
      this.$emit('on-result-change')
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('storage-form-apply-storeOut')
          console.log(data)
          // this.$store
          //   .dispatch('LmsChemicalRelDelivery/purchaseApplyStoreOut', data)
          //   .then(() => {
          //     this._resultChange('成功添加至出库单!')
          //   })
          this._save(data)
        } else {
          this.$Message.error('表单验证失败!')
        }
      })
    },

    _save: async function(data) {
      console.log('----------------ddd----', data)
      const result = await consumeRelDelivery.add(data)
      if (result) {
        this._resultChange('保存成功')
      }
    },

    _cancel() {
      this.chemicalStockOutModal = false
    },
    _open(data) {
      this.formObj.unit = data.unit
      this.formObj.stockId = data.id
      this.chemicalStockOutModal = true
      this.formObj.quantity = 1
      this.formObj.remark = ''
    }
  }
}
</script>
