<template>
  <div>
    <Modal v-model="showBackModal" :mask-closable="false" class="zIndex-1100">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <div>
        <Form id="contract-back-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="90">
          <Form-item label="判定结果:" prop="singleJudge">
            <RadioGroup v-model="formObj.singleJudge">
              <Radio :label="'PASS'">
                合格
              </Radio>
              <Radio :label="'FAIL'">
                不合格
              </Radio>
              <Radio :label="'NO_TEST'">
                不检测
              </Radio>
              <Radio :label="'NO_JUDGE'">
                不判定
              </Radio>
            </RadioGroup>
          </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'
export default {
  components: {},
  data() {
    return {
      ids: [], // 合同ids
      modalTitle: '判定',
      showBackModal: false,
      formObj: {
        singleJudge: 'PASS'
      },
      path: '',
      ruleValidate: {
        singleJudge: [
          { required: true, message: '判定结果不能为空', trigger: 'blur' }
        ]
      },
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ]
    }
  },
  methods: {
    _open(ids) {
      this.ids = ids // 合同id
      this.showBackModal = true
      this.formObj.singleJudge = 'PASS'
      this._hideLoading()
    },
    _cancel() {
      this.showBackModal = false
    },
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(result, msg) {
      if (result) {
        this.showBackModal = false
        this.$Message.success(msg)
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    _save: async function(data) {
      const result = await drugItem.handJudge(data)
      this._resultChange(result, '操作成功')
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form')
          data.singleJudge = this.formObj.singleJudge
          data.itemIds = this.ids.join(',')
          this._save(data)
        } else {
          this.$Message.error('表单验证失败')
          this._hideLoading()
        }
      })
    }
  }
}
</script>
