<template>
  <el-dialog
    class="download-set-dialog"
    :visible.sync="modalIsShow"
    title="批量下载"
    top="10vh"
    width="650px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      label-width="120px"
    >
      <el-form-item prop="paperFileDownloadContentType" label="下载文件：">
        <el-select
          v-model="modalForm.paperFileDownloadContentType"
          placeholder="下载文件"
          clearable
          @change="paperFileDownloadContentTypeChange"
        >
          <el-option
            v-for="(val, key) in FILE_TYPE"
            :key="key"
            :value="key"
            :label="val"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="modalForm.paperFileDownloadContentType !== 'ONLY_CARD'"
        prop="paperFileDownloadExposureStatus"
        label="试卷状态："
      >
        <el-select
          v-model="modalForm.paperFileDownloadExposureStatus"
          placeholder="试卷状态"
          clearable
        >
          <el-option
            v-for="(val, key) in PAPER_STATUS"
            :key="key"
            :value="key"
            :label="val"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="nameRule" label="文件命名规则：">
        <el-checkbox-group v-model="nameRule" @change="nameRuleChange">
          <el-checkbox
            v-for="rule in NAME_RULE"
            :key="rule.code"
            :label="rule.code"
            :disabled="rule.disabled"
            >{{ rule.name }}</el-checkbox
          >
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="命名示例：">
        <span>{{ nameRuleContent }}</span>
      </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 { dataBatchDownload } from "../api";

const initModalForm = {
  paperFileDownloadContentType: "",
  paperFileDownloadExposureStatus: null,
  namedByCourseInfo: false,
  namedByPaperNumber: false,
  namedByOriginalFile: false,
};

export default {
  name: "download-set-dialog",
  props: {
    downloadSet: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      nameRule: [],
      modalForm: { ...initModalForm },
      rules: {
        paperFileDownloadContentType: [
          {
            required: true,
            message: "请选择下载文件",
            trigger: "change",
          },
        ],
        paperFileDownloadExposureStatus: [
          {
            required: true,
            message: "请选择试卷状态",
            trigger: "change",
          },
        ],
        nameRule: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (!this.nameRule || !this.nameRule.length) {
                return callback(new Error("请设置文件命名规则"));
              }

              callback();
            },
            trigger: "change",
          },
        ],
      },
      PAPER_STATUS: {
        EXPOSED_PAPER: "已曝光的试卷",
        UNEXPOSED_PAPER: "未曝光的试卷",
        ALL_PAPER: "全部试卷",
      },
      FILE_TYPE: {
        ONLY_PAPER: "试卷",
        ONLY_CARD: "题卡",
        PAPER_AND_CARD: "试卷和题卡",
      },
      NAME_RULE: [
        {
          code: "namedByCourseInfo",
          name: "课程名称-课程代码",
          disabled: false,
        },
        {
          code: "namedByPaperNumber",
          name: "试卷编号",
          disabled: false,
        },
        {
          code: "namedByOriginalFile",
          name: "原文件名",
          disabled: false,
        },
      ],
    };
  },
  computed: {
    nameRuleContent() {
      const names = this.NAME_RULE.filter((item) =>
        this.nameRule.includes(item.code)
      )
        .map((item) => item.name)
        .join("-");
      return names ? `${names}.pdf` : "";
    },
  },
  methods: {
    visibleChange() {
      this.modalForm = { ...initModalForm };
      this.nameRule = [];
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    nameRuleChange() {
      this.NAME_RULE.forEach((item) => {
        this.modalForm[item.code] = this.nameRule.includes(item.code);
      });
    },
    paperFileDownloadContentTypeChange(val) {
      if (val === "ONLY_CARD") {
        const originalItem = this.NAME_RULE.find(
          (item) => item.code === "namedByOriginalFile"
        );
        originalItem.disabled = true;

        this.nameRule = this.nameRule.filter(
          (item) => item !== "namedByOriginalFile"
        );
        this.modalForm.paperFileDownloadExposureStatus = null;
      } else {
        this.NAME_RULE.forEach((item) => (item.disabled = false));
      }
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      let datas = { ...this.modalForm, ...this.downloadSet };
      const data = await dataBatchDownload(datas).catch(() => {
        this.isSubmit = false;
        this.$message.error("下载任务提交失败，请重新尝试！");
      });

      if (!data) return;

      this.isSubmit = false;
      this.$message.success("下载任务已提交！");
      this.cancel();
    },
  },
};
</script>
