<template>
  <el-dialog
    class="audit-paper-dialog"
    :visible.sync="modalIsShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    width="600px"
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="审核结果" prop="auditResult">
        <el-select v-model="modalForm.auditResult" disabled>
          <el-option label="通过" value="PASS"></el-option>
          <el-option label="不通过" value="NOT_PASS"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核意见" prop="auditRemark">
        <el-input
          v-model="modalForm.auditRemark"
          type="textarea"
          placeholder="请输入内容"
          maxlength="200"
        ></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="text-center">
      <el-button type="primary" :disabled="isSubmit" @click="confirm"
        >确定</el-button
      >
      <el-button @click="cancel"> 取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { objAssign } from "@/plugins/utils";
import { auditQuestionApi } from "../api";

const initModelForm = {
  questionIds: [],
  auditResult: "",
  auditRemark: "",
};

export default {
  name: "AuditQuestionDialog",
  props: {
    info: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: { ...initModelForm },
      rules: {
        auditResult: [
          {
            required: true,
            message: "请选择审核结果",
            trigger: "change",
          },
        ],
        auditRemark: [
          {
            validator: (rule, value, callback) => {
              if (this.modalForm.auditResult === "NOT_PASS" && !value) {
                return callback(new Error(`请输入审核意见`));
              }

              callback();
            },
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    visibleChange() {
      this.modalForm = objAssign(initModelForm, this.info);
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async confirm() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      let datas = { ...this.modalForm };
      datas.questionIds = datas.questionIds.join();
      const res = await auditQuestionApi(datas).catch(() => {});
      this.isSubmit = false;
      if (!res) return;

      this.$emit("modified");
      this.$message.success("操作成功！");
      this.cancel();
    },
  },
};
</script>
