<template>
  <div>
    <Modal v-model="chemicalStockOutModal" :mask-closable="false">
      <p slot="header">
        耗材库存校正
      </p>
      <div>
        <Form id="storage-form-storeOut--33" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="80">
          <Form-item label="数量" prop="stock">
            <label style="width: 40%">
              <InputNumber v-model="formObj.stock" name="stock" style="width: 100%" />
            </label>
            <label style="width: 10%;text-align: center">{{ formObj.unit }}</label>
            <input v-model="formObj.unit" hidden name="unit">
          </Form-item>
          <Form-item label="校正人" prop="registrant">
            <Select v-model="formObj.registrant" filterable style="width:100%" @on-change="_selectChange">
              <Option v-for="item in salesmanList" :key="item" :value="item">
                {{ item }}
              </Option>
            </Select>
          </Form-item>
          <Form-item label="校正时间" prop="time">
            <Date-picker
              v-model="formObj.regdate"
              type="datetime"
              placeholder="校正时间"
              style="width: 100%;"
              @on-change="_timeChange"
            />
            <input name="regdate" type="hidden">
          </Form-item>
          <Form-item label="校正原因" prop="reason">
            <Input v-model="formObj.reason" name="reason" placeholder="请输入校正原因" /></Input>
          </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 global from '../../../../api/config'
import { consumeStock, sysUser } from '../../../../api'
const defVal = {
  stock: 1,
  unit: '',
  depId: '',
  department: '',
  registrant: '',
  remark: '',
  reason: '',
  regdate: new Date(),
  batchNum: ''
}

export default {
  components: {},
  data() {
    return {
      id: '',
      formObj: defVal,
      chemicalStockOutModal: false,
      chemicalId: '',
      ruleValidate: {
        stock: [{ trigger: 'blur', message: '请输入数字', type: 'number' }],
        department: [
          { required: true, trigger: 'blur', message: '科室不能为空' }
        ],
        registrant: [
          { required: true, trigger: 'change', message: '使用人不能为空' }
        ]
      },
      selectList: [],
      departmentList: [],
      salesmanList: [],
      salesmanListTemp: [],
      chemical: {}
    }
  },
  methods: {
    _selectChange(data) {
      this.formObj.registrant = data
    },
    _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) {
          if (this.formObj.stock === 0) {
            this.$Message.warning('数量不能为0')
            return
          }
          const data = this.$serialize('storage-form-storeOut--33')
          data.chemicalId = this.chemicalId
          data.registrant = this.formObj.registrant
          data.type = 3
          data.validDate = this.$dateformat(
            this.chemical.validDate,
            'yyyy-mm-dd'
          )
          data.batchNum = this.chemical.batchNum
          this._save(data)
        } else {
          this.$Message.error('表单验证失败!')
        }
      })
    },
    _save: async function(data) {
      const result = await consumeStock.editStock(data)
      if (result) {
        this._resultChange('入库成功')
      }
    },
    _cancel() {
      this.chemicalStockOutModal = false
    },
    _open(data) {
      this._selectdepartment()
      this.formObj.unit = data.unit
      this.formObj.remark = ''
      this.chemicalId = data.id
      this.chemical = data
      this.chemicalStockOutModal = true
      this.$refs.formObj.resetFields()
      $('input[name=regdate]').val(
        this.$dateformat(new Date(), 'yyyy-mm-dd HH:MM:SS')
      )
      this.formObj.department = ''
      this.formObj.depId = ''
      this.remark = ''
      this.salesmanList = []
      this._getUser()
      this._getSalesmanList()
    },
    _getUser() {
      const user = global.getUserInfo()
      this.formObj.registrant = user.realname
    },
    // 人员列表
    _getSalesmanList: async function() {
      const result = await sysUser.list({})
      if (result) {
        const salesmanList = result
        const salesmanListTemp = []
        for (let i = 0; i < salesmanList.length; i++) {
          salesmanListTemp.push(salesmanList[i].realname)
        }
        if (salesmanListTemp.indexOf(this.formObj.realname) === -1) {
          salesmanListTemp.push(global.getUserInfo().realname)
        }
        this.salesmanList = salesmanListTemp
        this.salesmanListTemp = this.salesmanList
      }
    },
    // 出库时间
    _timeChange(data) {
      $('input[name="regdate"]').val(data)
    },
    // 选择科室
    /* _selectUserGroup() {
         this.$refs.userGroupModal._openDepart();
       },
       _userGroupData(data) {
         this.formObj.department = data.name;
         this.formObj.depId = data.id;
         this.$store.dispatch('LmsMarshalManage/userPage', {rows: 500, groupId: data.id}).then(() => {
           let result = this.$store.state.LmsMarshalManage.userPage.records;
           this.salesmanList = result;
         });
       }, */
    _selectdepartment() {
      // this.$store.dispatch('LmsMarshalManage/page').then(() => {
      //   this.departmentList = this.$store.state.LmsMarshalManage.page.records
      // })
    },
    _departmentChange(id) {
      // this.formObj.department = data.name;
      this.formObj.depId = id
      // this.$store
      //   .dispatch('LmsMarshalManage/userPage', { rows: 500, groupId: id })
      //   .then(() => {
      //     const result = this.$store.state.LmsMarshalManage.userPage.records
      //     this.salesmanList = result
      //   })
    }
  }
}
</script>
