<template>
  <div>
    <Modal v-model="detalAdviceModal" width="500">
      <p slot="header">
        登记处理意见
      </p>
      <Form id="edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="110">
        <Form-item label="样本编号" prop="sampleCode">
          <Input
            v-model="formObj.sampleCode"
            name="sampleCode"
            placeholder="请输入样本编号"
            clearable
            disabled
          />
        </Form-item>
        <Form-item label="不符合数量" prop="noItemNum">
          <Input
            v-model="formObj.noItemNum"
            name="noItemNum"
            placeholder="请输入不符合数量"
            clearable
            disabled
          />
        </Form-item>
        <Form-item label="不符合情况描述" prop="describable">
          <Input
            v-model="formObj.describable"
            name="describable"
            placeholder="请输入不符合情况描述"
            clearable
            type="textarea"
            :rows="3"
          />
        </Form-item>
        <Form-item label="处理意见" prop="suggestion" style="width: 100%;">
          <el-select
            v-model="formObj.suggestion"
            name="suggestion"
            placeholder="请选择处理意见"
            size="small"
            style="width:100%"
          >
            <el-option
              v-for="item in handlingOpinionsList"
              :key="item.value"
              :label="item.label"
              :value="item.label"
            />
          </el-select>
        </Form-item>
      </Form>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
  </div>
</template>

<script>
import { drugSample } from '../../../api'
const defVal = {
  sampleCode: '',
  noItemNum: '',
  describable: '',
  suggestion: ''
}
export default {
  name: 'PreparationManageEdit',
  data() {
    return {
      id: '',
      formObj: defVal,
      ruleValidate: {
        sampleCode: [
          { required: true, message: '样本编号不能为空!', trigger: 'blur' }
        ],
        describable: [
          {
            required: true,
            message: '不符合情况描述不能为空!',
            trigger: 'blur'
          }
        ],
        suggestion: [
          { required: true, message: '处理意见不能为空!', trigger: 'change' }
        ],
        noItemNum: [
          {
            required: true,
            message: '不符合数量不能为空!',
            trigger: 'blur',
            type: 'number'
          }
        ]
      },
      detalAdviceModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ],
      producerList: [],
      handlingOpinionsList: [
        { label: '复检', value: 1 },
        { label: '不予处理', value: 1 }
      ],
      sampleId: ''
    }
  },
  methods: {
    _open(formObj) {
      this.detalAdviceModal = true
      this.$refs.formObj.resetFields()
      this.formObj = Object.assign({}, defVal)
      this.sampleId = formObj.id
      this.formObj.sampleCode = formObj.sampleCode
      this.formObj.noItemNum = formObj.noItemNum
      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-form')
          data.sampleId = this.sampleId
          data.noItemNum = this.formObj.noItemNum
          this._save(data)
        } else {
          this.$Message.error('表单验证失败')
          this._hideLoading()
        }
      })
    },
    _cancel() {
      this.detalAdviceModal = false
    },
    _resultChange(msg) {
      this.detalAdviceModal = false
      this.$Message.success(msg)
      this.$emit('on-result-change')
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    // 添加、编辑保存
    async _save(data) {
      // 添加
      const result = await drugSample.registerSuggestion(data)
      if (result) {
        this._resultChange('登记成功!')
      }
      this._hideLoading()
    }
  }
}
</script>

<style scoped>
</style>
