<template>
  <div>
    <Modal v-model="showBackModal" :mask-closable="false" class="zIndex-1100">
      <p slot="header">{{modalTitle}}</p>
      <div>
        <Form ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="90">
          <Form-item label="判定结果:" prop="type">
            <RadioGroup v-model="formObj.singleJudge">
              <Radio :label="'PASS'">合格</Radio>
              <Radio :label="'FAIL'">不合格</Radio>
            </RadioGroup>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <ModalFooter ref="footerModal" :footer="footerList" @on-result-change="_footerResult"></ModalFooter>
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 判定
 */
import ModalFooter from '../../../components/base/modalFooter'
import { meterItem } from '../../../api'

export default {
  components: {
    ModalFooter
  },
  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.type = 1
      this.$refs.footerModal._hideLoading()
    },
    _cancel() {
      this.showBackModal = false
    },
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
      this.$refs.footerModal._hideLoading()
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = {}
          data.ids = this.ids.join(',')
          this.$extend(data, { singleJudge: this.formObj.singleJudge })
          this._handJudge(data)
        } else {
          this.$Message.error('表单验证失败!')
        }
      })
    },
    _handJudge: async function(data) {
      const result = await meterItem._handJudge(data)
      if (result) {
        this.$Message.success('判定成功!')
        this.showBackModal = false
        this.$emit('on-result-change')
      }
    }
  }
}
</script>
