<template>
  <div>
    <Modal v-model="showEditModal" :mask-closable="false" width="600" @on-cancel="_cancel">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <div>
        <Form id="edit-forms" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="100">
          <Form-item label="样品编号:" prop="sampleCode" style="width:96.1%">
            <Input v-model="formObj.sampleCode" name="sampleCode" placeholder="请输入样品编号" disabled />
          </Form-item>
          <Form-item label="检测项目:" prop="name" style="width:96.1%">
            <Input v-model="formObj.name" name="name" placeholder="请输入检测项目" disabled />
          </Form-item>
          <Form-item label="检测结果:" prop="testValue" style="width:96.1%">
            <Input v-model="formObj.testValue" name="testValue" placeholder="请输入检测结果" disabled />
          </Form-item>
          <Form-item label="单位:" prop="unit" style="width:96.1%">
            <Input v-model="formObj.unit" name="unit" placeholder="请输入单位" disabled/>
          </Form-item>
          <Form-item label="偏离原因分析:" prop="deviationRemark" style="width:96.1%">
            <Input
              v-model="formObj.deviationRemark"
              name="deviationRemark"
              type="textarea"
              :autosize="{ minRows: 3, maxRows: 4}"
              placeholder="请输入偏离原因分析"
            />
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 添加偏离申请
 */
import { drugItem } from '../../../../api'
const defVal = {
  id: '',
  deviationRemark: '',
  name: '',
  contractCode: '',
  sampleCode: '',
  unit: '',
  testValue: ''
}
export default {
  data() {
    return {
      id: '',
      modalTitle: '偏离申请',
      formObj: defVal,
      ruleValidate: {
        deviationRemark: [
          { required: true, message: '偏离原因分析不能为空！', trigger: 'blur' }
        ]
      },
      showEditModal: false,
      levelList: [],
      complaintDate: '',
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '提交', type: 'primary' }
      ]
    }
  },
  methods: {
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '提交':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(result, msg) {
      if (result) {
        this.showEditModal = false
        this.$Message.success(msg)
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    _ok(msg) {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-forms')
          data.deviationRemark = this.formObj.deviationRemark
          data.id = this.formObj.id
          this._save(data)
        } else {
          this.$Message.error('表单验证失败')
          this._hideLoading()
        }
      })
    },
    _save: async function(data) {
      const result = await drugItem.submitDeviate(data)
      this._resultChange(result, '提交成功')
    },
    _cancel() {
      this.showEditModal = false
      setTimeout(() => {
        this.formObj = defVal
      }, 300)
    },
    _open(formObj) {
      this.showEditModal = true
      this.$refs.formObj.resetFields()
      this.formObj = Object.assign({}, {}, defVal)
      // 检测项目
      this.formObj.name = formObj.name
      // 委托编号
      this.formObj.reportCode = formObj.code
      this.formObj.sampleCode = formObj.sampleCode
      this.formObj.testValue = formObj.testValue
      this.formObj.contractCode = formObj.contractCode
      this.formObj.unit = formObj.unit
      this.formObj.id = formObj.id
    }
  }
}
</script>
