<template>
  <div>
    <Modal v-model="preparationModal" :mask-closable=false width="400">
      <p slot="header">
        填写处理
      </p>
      <Form id="edit-deal-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="70">
        <Form-item label="处理人" prop="handler">
          <Input
            v-model="formObj.handler"
            name="handler"
            placeholder="请选择处理人"
            clearable
            readonly
            @click.native="_selectStaff"
          />
        </Form-item>
        <Form-item label="处理时间" prop="handleTime">
          <Date-picker
            v-model="formObj.handleTime"
            type="datetime"
            format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择处理时间"
            style="width: 100%;"
            :editable="false"
            @on-change="_sampleHandleTimeChange"
          />
        </Form-item>
      </Form>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
    <!--选择人员-->
    <UserInfo ref="userModal" is-change @on-result-change="_userData" />
  </div>
</template>

<script>
import UserInfo from '../../components/user-info-single/assignPerson'
import { drugSampleKeep } from '../../api'
const defVal = {
  handler: '',
  handlerId: '',
  handleTime: new Date()
}
export default {
  name: 'PreparationManageEdit',
  components: { UserInfo },
  data() {
    return {
      id: '',
      formObj: defVal,
      ruleValidate: {
        handler: [
          { required: true, message: '处理人不能为空!', trigger: 'change' }
        ],
        handleTime: [
          {
            required: true,
            message: '处理时间不能为空!',
            trigger: 'blur',
            type: 'date'
          }
        ]
      },
      preparationModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ],
      producerList: [],
      selectIds: []
    }
  },
  methods: {
    _open(ids) {
      this.preparationModal = true
      this.selectIds = ids.join(',')
      this.$refs.formObj.resetFields()
      this.formObj = Object.assign({}, defVal)
      this.formObj.handleTime = new Date()
      this._hideLoading()
    },
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-deal-form')
          data.ids = this.selectIds
          data.handleTime = this.$dateformat(
            this.formObj.handleTime,
            'yyyy-mm-dd HH:MM:ss'
          )
          data.handlerId = this.formObj.handlerId
          data.handler = this.formObj.handler
          this._save(data)
        } else {
          this.$Message.error('表单验证失败')
          this._hideLoading()
        }
      })
    },
    _cancel() {
      this.preparationModal = false
    },
    _resultChange(msg) {
      this.preparationModal = false
      this.$Message.success(msg)
      this.$emit('on-result-change')
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    // 添加、编辑保存
    async _save(data) {
      // 添加
      const result = await drugSampleKeep.bathHandleEnd(data)
      if (result) {
        this._resultChange('保存成功!')
      }
      this._hideLoading()
    },
    _sampleHandleTimeChange(data) {
      this.formObj.handleTime = data
    },
    // 人员选择
    _selectStaff() {
      this.$refs.userModal._open('preparationPerson')
    },
    // 选择人员返回数据
    _userData(data) {
      this.formObj.handler = data.realname
      this.formObj.handlerId = data.userId
    }
  }
}
</script>

<style scoped>
</style>
