<template>
  <el-dialog
    class="merge-push-dialog"
    :visible.sync="modalIsShow"
    title="批量合并印刷任务"
    top="10vh"
    width="448px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <div class="mb-4">
      <p class="tips-info">
        1、此操作会将多个印刷计划合并，合并操作不可还原，是否继续合并操作？
      </p>
      <p class="tips-info">
        2、如果确定需要合并，请填写合并后考试名称及对应考试ID，然后点击确定按钮。
      </p>
    </div>
    <div class="mb-4 tab-btns">
      <el-button
        v-for="(val, key) in EXAM_TYPE"
        :key="key"
        :type="examType == key ? 'primary' : 'default'"
        @click="selectMenu(key)"
        >{{ val }}</el-button
      >
    </div>
    <el-form
      v-if="examType === '0'"
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="考试ID：">
        <el-input
          v-model.trim="modalForm.thirdRelateId"
          placeholder="请输入考试ID"
          clearable
          @input="thirdRelateIdChange"
        ></el-input>
      </el-form-item>
      <el-form-item label="考试名称：">
        <el-input
          v-model.trim="modalForm.thirdRelateName"
          placeholder="请输入考试名称"
          clearable
          :disabled="!canEditThirdRelateName"
        ></el-input>
      </el-form-item>
      <el-form-item prop="thirdRelate"></el-form-item>
    </el-form>
    <el-form
      v-else
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules1"
      label-width="100px"
    >
      <el-form-item prop="thirdRelateId" label="考试ID：">
        <el-select
          v-model="modalForm.thirdRelateId"
          placeholder="请选择"
          filterable
          style="width: 100%"
        >
          <el-option
            v-for="(item, index) in exams"
            :key="index"
            :value="item.thirdRelateId"
            :label="item.thirdRelateName"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" :disabled="isSubmit" @click="submit"
        >确认</el-button
      >
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { printPlanMergePush, printPlanSyncExamList } from "../api";

const initModalForm = {
  thirdRelateId: "",
  thirdRelateName: "",
};

export default {
  name: "merge-push-dialog",
  props: {
    ids: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    const thirdRelateValidator = (rule, value, callback) => {
      if (!this.modalForm.thirdRelateId && !this.modalForm.thirdRelateName) {
        return callback(new Error(`考试ID和名称至少填一项。`));
      }

      if (
        this.modalForm.thirdRelateId &&
        this.modalForm.thirdRelateId.length > 50
      ) {
        return callback(new Error("考试ID长度不能超过50"));
      }
      if (
        this.modalForm.thirdRelateName &&
        this.modalForm.thirdRelateName.length > 50
      ) {
        return callback(new Error("考试名称长度不能超过50"));
      }

      return callback();
    };
    return {
      modalIsShow: false,
      isSubmit: false,
      exams: [],
      canEditThirdRelateName: true,
      modalForm: { ...initModalForm },
      rules: {
        thirdRelate: [
          {
            required: true,
            validator: thirdRelateValidator,
            trigger: "change",
          },
        ],
      },
      rules1: {
        thirdRelateId: [
          {
            required: true,
            message: "请选择考试ID",
            trigger: "change",
          },
        ],
      },
      examType: "0",
      EXAM_TYPE: {
        0: "未同步考试",
        1: "已同步考试",
      },
    };
  },
  methods: {
    initData(val) {
      this.modalForm = { ...initModalForm };
      this.$nextTick(() => {
        this.$refs.modalFormComp.clearValidate();
      });
    },
    visibleChange() {
      this.initData(this.instance);
      this.getExamList();
    },
    async getExamList() {
      const data = await printPlanSyncExamList();
      this.exams = data || [];
    },
    thirdRelateIdChange() {
      const exam = this.exams.find(
        (exam) => this.modalForm.thirdRelateId === exam.thirdRelateId
      );
      if (!exam) {
        this.modalForm.thirdRelateName = "";
        this.canEditThirdRelateName = true;
        return;
      }
      this.modalForm.thirdRelateName = exam.thirdRelateName;
      this.canEditThirdRelateName = false;
    },
    selectMenu(val) {
      this.examType = val;
      this.modalForm = { ...initModalForm };
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      const data = await printPlanMergePush({
        ...this.modalForm,
        list: this.ids,
      }).catch(() => {
        this.isSubmit = false;
      });

      if (!data) return;

      this.isSubmit = false;
      this.$message.success("提交成功！");
      this.$emit("modified");
      this.cancel();
    },
  },
};
</script>
