<template>
  <div>
    <Modal v-model="showStorageModal" :mask-closable="false" class="zIndex-1100">
      <p slot="header">
        耗材出库
      </p>
      <div>
        <Form id="storage-form-single" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="80">
          <Form-item label="出库数量" prop="stock">
            <label style="width: 80%">
              <InputNumber v-model.number="formObj.stock" :min="1" name="stock" style="width: 100%" />
            </label>
            <label style="width: 10%;text-align: center">{{ formObj.unit }}</label>
          </Form-item>
          <Form-item label="使用人" prop="applyer">
            <Select v-model="formObj.registrant" name="registrant" style="width:100%">
              <Option v-for="item in salesmanList" :key="item" :value="item">
                {{ item }}
              </Option>
            </Select>
          </Form-item>
          <Form-item label="出库时间" prop="outStorageDate">
            <Date-picker
              v-model="formObj.outStorageDate"
              name="outStorageDate"
              type="datetime"
              placeholder="出库时间"
              style="width: 100%;"
            />
          </Form-item>
          <Form-item label="备注" prop="remark">
            <Input v-model="formObj.remark" name="remark" style="width: 100%" />
          </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 global from '../../../../api/config'

const defVal = {
  remark: '',
  stock: '',
  validDate: '',
  price: '',
  accuracyRange: '',
  casNum: '',
  batchNum: '',
  supplier: '',
  id: '',
  department: '',
  registrant: '',
  unit: ''
}
export default {
  components: {},
  data() {
    const validateNumber = (rule, value, callback) => {
      // 只能输入正整数，不能输入小数
      const reg = /^\+?[1-9]\d*$/
      if (!reg.test(value)) {
        callback(new Error('数量只能输入整数'))
      } else if (value > this.quantity) {
        callback(new Error('出库数量不能大于库存量'))
      } else {
        callback()
      }
    }
    return {
      id: '',
      formObj: defVal,
      showStorageModal: false,
      ruleValidate: {
        stock: [
          {
            required: true,
            validator: validateNumber,
            trigger: 'blur',
            type: 'number'
          }
        ],
        department: [
          { required: true, message: '科室不能为空', trigger: 'change' }
        ],
        registrant: [
          { required: true, message: '使用人不能为空', trigger: 'blur' }
        ],
        outStorageDate: [
          {
            required: true,
            message: '出库时间不能为空',
            trigger: 'change',
            type: 'date'
          }
        ]
      },
      quantity: '',
      salesmanList: [],
      salesmanListTemp: [],
      stock: '',
      alreadyQuantity: '',
      chemical: {}
    }
  },
  methods: {
    _resultChange(msg) {
      if (this.$store.state.LmsChemicalRelDelivery.success) {
        this.formObj = defVal
        this.showStorageModal = false
        this.$Message.success(msg)
        this.$emit('on-result-change')
      }
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('storage-form-single')
          data.validDate = this.$dateformat(
            this.chemical.validDate,
            'yyyy-mm-dd'
          )
          data.batchNum = this.chemical.batchNum
          this.$store
            .dispatch('LmsChemicalRelDelivery/outStockPurchase', {
              id: this.formObj.id,
              obj: data
            })
            .then(() => {
              this._resultChange('出库成功!')
            })
        } else {
          this.$Message.error('表单验证失败!')
        }
      })
    },
    _cancel() {
      this.showStorageModal = false
    },
    _open(data, chemicalData) {
      this.showStorageModal = true
      this.$refs.formObj.resetFields()
      this.formObj.id = data.id
      this.formObj.outStorageDate = new Date()
      this.alreadyQuantity = data.alreadyQuantity
      this.stock = data.quantity
      this.quantity = chemicalData.quantity
      this.chemical = data
      this._getUser()
      this._getSalesmanList()
      this._getStock()
    },
    _getStock() {
      if (this.alreadyQuantity === undefined) {
        this.formObj.stock = this.stock
      } else {
        this.formObj.stock = this.stock - this.alreadyQuantity
      }
    },
    _getUser() {
      const user = global.getUserInfo()
      this.formObj.registrant = user.realname
    },
    // 人员列表
    _getSalesmanList() {
      this.$store.dispatch('LmsMarshalManage/userPage').then(() => {
        const salesmanList = this.$store.state.LmsMarshalManage.userPage.records
        const salesmanListTemp = []
        for (let i = 0; i < salesmanList.length; i++) {
          salesmanListTemp.push(salesmanList[i].realname)
        }
        if (salesmanListTemp.indexOf(this.formObj.registrant) === -1) {
          salesmanListTemp.push(global.getUserInfo().realname)
        }
        this.salesmanList = salesmanListTemp
        this.salesmanListTemp = this.salesmanList
      })
    }
  }
}
</script>
