<template>
  <div>
    <Modal v-model="showModal" :mask-closable="false" class="zIndex-1100">
      <p slot="header">
        耗材直接入库
      </p>
      <div>
        <Form id="storage-form-storeIn" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="80">
          <Form-item label="入库数量" prop="stock">
            <InputNumber v-model.number="formObj.stock" :min="0" name="stock" />
            <label style="width: 10%;text-align: center">{{ formObj.unit }}</label>
          </Form-item>
          <Form-item label="价格" prop="price">
            <InputNumber v-model.number="formObj.price" :min="0" name="price" />
            <label style="width: 10%;text-align: center">元</label>
          </Form-item>
          <Form-item label="不合格数量" prop="unqualifiedQuantity">
            <InputNumber v-model.number="formObj.unqualifiedQuantity" :min="1" name="unqualifiedQuantity" />
            <label style="width: 10%;text-align: center">{{ formObj.unit }}</label>
          </Form-item>
          <Form-item label="不合格原因" prop="unqualifiedReason">
            <Input v-model="formObj.unqualifiedReason" name="unqualifiedReason" /></Input>
          </Form-item>
          <Form-item label="验收人" prop="checkPerson">
            <Input
              v-model="formObj.checkPerson"
              name="checkPerson"
              readonly
              @click.native="_selectUser('checkPerson')"
            /></Input>
          </Form-item>
          <Form-item label="入库人" prop="realname">
            <Input v-model="formObj.realname" name="realname" readonly @click.native="_selectUser('realname')" /></Input>
          </Form-item>
          <Form-item label="入库时间" prop="regdate">
            <DatePicker
              v-model="formObj.regdate"
              type="datetime"
              placeholder="入库时间"
              name="regdate"
              style="width: 100%;"
              @on-change="_timeChange"
            />
          </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>
    <!--选择人员（单选）-->
    <AssignPerson ref="userModal" @on-result-change="_userData" />
  </div>
</template>
<script>
/**
 * 添加编辑试验耗材库存表
 */
import AssignPerson from '../../../../components/user-info-single/AssignPerson'
import Global from '../../../../api/config'

export default {
  components: {
    AssignPerson
  },
  data() {
    const validateNumber = (rule, value, callback) => {
      // 只能输入正整数，不能输入小数
      const reg = /^\+?[1-9]\d*$/
      if (reg.test(value)) {
        callback()
      } else {
        callback(new Error('数量只能输入整数'))
      }
    }
    return {
      formObj: {
        stock: 1,
        unit: '',
        price: 1,
        unqualifiedQuantity: '',
        unqualifiedReason: '',
        checkPerson: '',
        checkPersonId: '',
        realname: '',
        regdate: new Date(),
        id: '',
        purchaseOrderRelId: '',
        chemicalId: '',
        orderId: '',
        batchNum: '',
        remark: ''
      },
      showModal: false,
      ruleValidate: {
        stock: [{ validator: validateNumber, trigger: 'blur', type: 'number' }],
        remark: [
          { required: true, trigger: 'blur', message: '入库原因不能为空' }
        ]
      },
      registrantList: [],
      registrantListTemp: []
    }
  },
  methods: {
    _open(data, letData, orderId) {
      this.$refs.formObj.resetFields()
      this.showModal = true
      this.formObj.checkPersonId = ''
      this.formObj.orderId = orderId // 采购单id
      this.formObj.id = data.id // 右边的数据id
      this.formObj.chemicalId = letData.chemicalId // 耗材id
      this.formObj.purchaseOrderRelId = letData.id // 左边的数据Id
      this.formObj.batchNum = data.batchNum
      this.formObj.stock = letData.quantity ? letData.quantity : 1
      this.formObj.price = letData.price ? letData.price : 0
      this.formObj.unit = data.unit
      this.formObj.realname = Global.getUserInfo().realname
      this.formObj.regdate = new Date()
    },
    _timeChange(data) {
      this.formObj.regdate = data
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serializeFormSearch(this.formObj)
          if (data.regdate) {
            data.regdate = this.$dateformat(
              this.formObj.regdate,
              'yyyy-mm-dd HH:MM:ss'
            )
          }
        } else {
          this.$Message.error('表单验证失败!')
        }
      })
    },
    _cancel() {
      this.showModal = false
    },
    // 选择人员
    _selectUser(msg) {
      this.$refs.userModal._open(msg)
    },
    _userData(data, msg) {
      if (msg === 'checkPerson') {
        this.formObj.checkPerson = data.realname
        this.formObj.checkPersonId = data.userId
      } else {
        this.formObj.realname = data.realname
      }
    }
  }
}
</script>
