
<!--
  @description: 评审管理-终止评审弹框
  @Modifier：yf
  @routerPath:
  @date：   2020-09-22 16:02:13
-->
<template>
  <el-dialog
    :visible.sync="dialogData.dialog"
    title="终止说明"
    width="40%"
  >
    <el-form ref="formData" :model="formData" :inline-message="true" :rules="rules" style="margin-bottom: 10px">
      <table class="from_table">
        <tr>
          <td>
            <p v-if="dialogData.reviewStatus === '02'">
              终止评审的原因
            </p>
            <el-form-item v-else label="请填写终止评审的原因" prop="endEvalReason" />
          </td>
          <td>
            <p v-if="dialogData.reviewStatus === '02'" v-html="dialogData.endEvalReason" />
            <el-form-item v-else>
              <el-input
                v-model="formData.endEvalReason"
                :autosize="{ minRows: 4, maxRows: 4 }"
                type="textarea"
                maxlength="1000"
                show-word-limit
              />
            </el-form-item>
          </td>
        </tr>
      </table>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit('formData')">确定</el-button>
      <el-button @click="dialogData.dialog=false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
/* api*/
/* api接口*/
import {
  endEvalProcedureByPackId,
  viewEndEvalProcedureByPackId
} from '@/api/procurement/procurementEvaluation'
/* 公共方法*/
import { restoreInitialValue } from '@assets/js/utils'
import { characters1_auto } from '@/assets/js/validate'
export default {
  inject: ['getDriveTask'],
  props: {
    dialogData: {
      type: Object,
      default: function() {
        return {
          dialog: false, // false 关闭 true 打开
          reviewStatus: '' // 评审状态
        }
      }
    },
    // 项目信息
    getTenderBase: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      loading: false,
      formData: {
        packId: this.getTenderBase.packId,
        endEvalReason: '' // 终止原因
      },
      rules: {
        endEvalReason: [{ required: true, validator: characters1_auto, trigger: 'blur' }]
      }
    }
  },
  watch: {
    // 弹框打开状态
    'dialogData.dialog'(val) {
      if (val === false) {
        // 清空 value
        restoreInitialValue(this.formData)
        // 清空校验
        this.$refs.formData.clearValidate()
      } else {
        this.reviewTerminated()
      }
    }
  },
  mounted() {
  },
  methods: {
    // 提交退回申请
    submit(formName) {
      if (this.dialogData.reviewStatus === '02') {
        this.dialogData.dialog = false
        return
      }
      this.loading = true
      // 是否通过必填校验
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm('终止评审后，所有专家将无法进行评标工作，确认终止吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            confirmButtonClass: 'confirmBtn',
            cancelButtonClass: 'cancelBtn',
            type: 'warning'
          }).then(() => {
            const data = {
              endEvalReason: this.formData.endEvalReason,
              packId: this.getTenderBase.packId
            }
            endEvalProcedureByPackId(data).then(res => {
              this.loading = false
              this.dialogData.dialog = false
              this.$message.success('终止评审成功！')
              // 刷新驱动节点
              this.getDriveTask()
              // this.$emit('initData')
            })
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.loading = false
        }
      })
    },
    reviewTerminated() { // 评审终止原因查询
      viewEndEvalProcedureByPackId(this.getTenderBase.packId).then(res => {
        const endEvalReasonDomArr = []
        res.data.endEvalReason ? res.data.endEvalReason.split('\n').forEach(item => endEvalReasonDomArr.push(`<p>${item.trim()}</p>`)) : ''
        this.$set(this.dialogData, 'endEvalReason', endEvalReasonDomArr.join('<br>'))
      })
    }
  }
}
</script>

<style lang='less' scoped>
</style>
