<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="40px"
        @submit.prevent="fnResetPagination"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="题干" prop="KEYWORDS">
              <el-input v-model="searchForm.KEYWORDS" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="10px">
              <el-button type="primary" native-type="submit">搜索</el-button>
              <el-button native-type="reset" @click="fnResetPagination">
                重置
              </el-button>
            </el-form-item>
          </el-col>
          <el-col :span="6"></el-col>
          <el-col :span="6">
            <el-form-item label-width="10px" class="end">
              <el-button @click="fnImportDialogChangeShow">导入</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        ref="tableRef"
        row-key="QUESTION_ID"
        :data="list"
        @get-data="fnGetData"
        v-model:pagination="pagination"
      >
        <el-table-column reserve-selection type="selection" width="55" />
        <el-table-column prop="QUESTIONNUMBER" label="题号" width="60" />
        <el-table-column label="试题类型" width="80">
          <template v-slot="{ row }">
            {{ translationStatus(row.QUESTIONTYPE, questionTypeOptions) }}
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="QUESTIONDRY"
          label="题干"
        />
        <el-table-column label="答案" width="55">
          <template v-slot="{ row }">
            <template v-if="row.QUESTIONTYPE === '3'">
              <span v-if="row.ANSWER === 'A'">正确</span>
              <span v-if="row.ANSWER === 'B'">错误</span>
            </template>
            <template v-else>
              <span>{{ row.ANSWER }}</span>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100">
          <template v-slot="{ row }">
            <el-button
              type="primary"
              text
              link
              v-if="
                buttonJurisdiction.edit &&
                row.STUDYCOUNT === 0 &&
                row.COURSEEXAMRECORDCOUNT === 0 &&
                row.STAGEEXAMRECORDCOUNT === 0 &&
                row.COURSEEXAMCOUNT === 0 &&
                row.STAGEEXAMCOUNT === 0
              "
              @click="fnAddOrEdit(row, 'edit')"
            >
              修改
            </el-button>
            <el-button
              type="primary"
              text
              link
              v-if="
                buttonJurisdiction.del &&
                row.STUDYCOUNT === 0 &&
                row.COURSEEXAMRECORDCOUNT === 0 &&
                row.STAGEEXAMRECORDCOUNT === 0 &&
                row.COURSEEXAMCOUNT === 0 &&
                row.STAGEEXAMCOUNT === 0
              "
              @click="fnDelete(row.QUESTION_ID)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
        <template #button>
          <el-button
            v-if="buttonJurisdiction.add"
            type="primary"
            @click="fnAddOrEdit({}, 'add')"
          >
            新增
          </el-button>
          <el-button
            v-if="buttonJurisdiction.del"
            type="danger"
            @click="fnBatchDelete"
          >
            删除
          </el-button>
        </template>
      </layout-table>
    </layout-card>
    <layout-import-file
      v-model:visible="data.importDialogVisible"
      template-url="/template/questionExcelTemplate.xls"
      @submit="fnSubmitImport"
    />
    <add
      v-model:visible="data.addOrEditDialog.visible"
      v-model:form="data.addOrEditDialog.form"
      :type="data.addOrEditDialog.type"
      :courseware-id="COURSEWAREID"
      :courseware-type="COURSEWARETYPE"
      @get-data="fnResetPagination"
    />
  </div>
</template>

<script setup>
import {
  setExercisesImport,
  setExercisesDelete,
  setExercisesDeleteBatch,
  getExercisesList,
} from "@/request/online_learn_exam.js";
import useListData from "@/assets/js/useListData.js";
import { nextTick, reactive } from "vue";
import { useRoute } from "vue-router";
import useButtonJurisdiction from "@/assets/js/useButtonJurisdiction.js";
import { debounce } from "throttle-debounce";
import { ElMessage, ElMessageBox } from "element-plus";
import LayoutImportFile from "@/components/import_file/index.vue";
import Add from "./components/add_exercises.vue";
import { translationStatus } from "@/assets/js/utils.js";
import { cloneDeep } from "lodash-es";

const route = useRoute();
const { COURSEWAREID, COURSEWARETYPE } = route.query;
const questionTypeOptions = [
  { ID: "1", NAME: "单选题" },
  { ID: "2", NAME: "多选题" },
  { ID: "3", NAME: "判断题" },
];
const { list, pagination, searchForm, fnGetData, fnResetPagination, tableRef } =
  useListData(getExercisesList, {
    otherParams: { COURSEWAREID, COURSEWARETYPE },
  });
const buttonJurisdiction = await useButtonJurisdiction("courseware");
const data = reactive({
  importDialogVisible: false,
  addOrEditDialog: {
    visible: false,
    form: {
      QUESTIONTYPE: "",
      QUESTIONDRY: "",
      OPTIONA: "",
      OPTIONB: "",
      OPTIONC: "",
      OPTIOND: "",
      ANSWER: "",
    },
    type: "add",
  },
});
const fnDelete = debounce(
  1000,
  async (QUESTION_ID) => {
    await ElMessageBox.confirm("确定要删除吗？", { type: "warning" });
    await setExercisesDelete({ QUESTION_ID });
    ElMessage.success("删除成功");
    fnResetPagination();
  },
  { atBegin: true }
);
const fnBatchDelete = debounce(
  1000,
  async () => {
    const selectionData = tableRef.value.getSelectionRows();
    if (selectionData.length === 0) {
      ElMessage.warning("请选中要删除的项");
      return;
    }
    await ElMessageBox.confirm("确定要删除选中的数据吗？", { type: "warning" });
    const DATA_IDS = selectionData.map((item) => item.QUESTION_ID).join(",");
    await setExercisesDeleteBatch({ DATA_IDS });
    ElMessage.success("删除成功");
    fnResetPagination();
  },
  { atBegin: true }
);
const fnImportDialogChangeShow = () => {
  data.importDialogVisible = !data.importDialogVisible;
};
const fnSubmitImport = async (formData) => {
  formData.append("COURSEWARETYPE", COURSEWARETYPE);
  formData.append("COURSEWAREID", COURSEWAREID);
  const resData = await setExercisesImport(formData);
  ElMessage.success(resData.msg);
  fnImportDialogChangeShow();
  fnResetPagination();
};
const fnAddOrEdit = async (row, type) => {
  data.addOrEditDialog.visible = true;
  await nextTick();
  data.addOrEditDialog.type = type;
  if (type === "edit") data.addOrEditDialog.form = cloneDeep(row);
};
</script>

<style scoped lang="scss"></style>
