<template>
  <div>
    <Modal v-model="deliverySubmitModal" :mask-closable="false">
      <p slot="header">
        耗材出库
      </p>
      <div>
        <Form id="storage-form-temp-submit" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="80">
          <Form-item label="使用人" prop="registrant">
            <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="remark">
            <Input v-model="formObj.remark" name="remark" placeholder="请输入申请原因" />
          </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="selectUser" @on-result-change="_userData" />
  </div>
</template>
<script>
/**
 * 添加编辑试验耗材库存表
 */
import global from '../../../../api/config'
import { consumeDeliveryOrder, dict, sysUser } from '../../../../api'
import AssignPerson from '../../../../components/user-info-single/assignPerson'

const defVal = {
  depId: '',
  departmentId: '',
  department: '',
  registrant: '',
  remark: '实验所需'
}
export default {
  components: {
    AssignPerson
  },
  data() {
    return {
      approve: false,
      formObj: defVal,
      deliverySubmitModal: false,
      ruleValidate: {
        department: [
          { required: true, trigger: 'change', message: '科室不能为空' }
        ],
        registrant: [
          { required: true, trigger: 'blur', message: '使用人不能为空' }
        ],
        remark: [
          { required: true, trigger: 'blur', message: '申请原因不能为空' }
        ]
      },
      salesmanList: [],
      salesmanListTemp: [],
      sendUserId: '',
      isApproval: ''
    }
  },
  methods: {
    _resultChange(msg) {
      this.deliverySubmitModal = false
      this.formObj = defVal
      this.$Message.success(msg)
      this.$emit('on-result-change')
    },
    _ok() {
      if (this.approve) {
        this._dicSearch()
      } else {
        this._save()
      }
    },
    _userData(data) {
      this.sendUserId = data.userId
      this._save()
    },
    _dicSearch: async function() {
      const result = await dict.getItem('耗材使用申请是否需要提交审批人')
      console.log('result-----', result)
      if (result !== undefined && result.length > 0) {
        if (result[0].name === '是') {
          this.$refs.selectUser._open()
        } else {
          this._save()
        }
      } else {
        this._save()
      }
    },
    _save() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('storage-form-temp-submit')
          data.approve = this.approve
          if (this.sendUserId) {
            data.sendUserId = this.sendUserId
          }
          this._add(data)
          // this.$store
          //   .dispatch('LmsChemicalDeliveryOrder/add', data)
          //   .then(() => {
          //     if (this.approve) {
          //       // 提交审批
          //       this._resultChange('审批提交成功!')
          //     } else {
          //       // 保存
          //       this._resultChange('保存成功!')
          //     }
          //   })
        } else {
          this.$Message.error('表单验证失败!')
        }
      })
    },

    _add: async function(data) {
      const result = await consumeDeliveryOrder.add(data)
      if (result) {
        this._resultChange('操作成功')
      }
    },

    _cancel() {
      this.deliverySubmitModal = false
    },
    _open(approve) {
      this.approve = approve
      this.$refs.formObj.resetFields()
      this.deliverySubmitModal = true
      this.formObj.department = ''
      this.formObj.depId = ''
      this.formObj.registrant = ''
      // this.formObj.remark = '';
      this._getUser()
      this._getSalesmanList()
    },
    _getUser() {
      const user = global.getUserInfo()
      this.formObj.registrant = user.realname
    },
    _getSalesmanList: async function() {
      const result = await sysUser.list({})
      const salesmanListTemp = []
      for (let i = 0; i < result.length; i++) {
        salesmanListTemp.push(result[i].realname)
      }
      if (salesmanListTemp.indexOf(this.formObj.registrant) === -1) {
        salesmanListTemp.push(global.getUserInfo().realname)
      }
      this.salesmanList = salesmanListTemp
      this.salesmanListTemp = this.salesmanList
      // 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>
