<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="130px"
        @submit.prevent="fnResetPaginationTransfer"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="题干" prop="KEYWORDS">
              <el-input
                v-model="searchForm.KEYWORDS"
                placeholder="请输入题干"
              />
            </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-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="fnGetDataTransfer"
        v-model:pagination="pagination"
      >
        <el-table-column reserve-selection type="selection" width="55" />
        <el-table-column prop="QUESTIONNUMBER" label="题号" width="60" />
        <el-table-column
          prop="QUESTIONTYPE"
          label="试题类型"
          show-overflow-tooltip
          width="80"
        >
          <template v-slot="{ row }">
            {{ fnFormatterQuestionType(row) }}
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="QUESTIONDRY"
          label="题干"
          align="left"
        />
        <el-table-column
          show-overflow-tooltip
          prop="ANSWER"
          label="答案"
          width="55"
        >
          <template v-slot="{ row }">
            <template v-if="row.QUESTIONTYPE === '3'">
              <div v-if="row.ANSWER === 'A'">正确</div>
              <div v-if="row.ANSWER === 'B'">错误</div>
            </template>
            <template v-else>
              <div>{{ row.ANSWER }}</div>
            </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)"
            >
              修改
            </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(null)"
          >
            新增
          </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"
      @get-data="fnResetPaginationTransfer"
    />
  </div>
</template>

<script setup>
import LayoutTable from "@/components/table/index.vue";
import LayoutCard from "@/components/card/index.vue";
import {
  importQuestion,
  deleteQuestion,
  deleteQuestionBatch,
  getQuestionListByCoursewareIdAndType,
} from "@/request/online_learn_exam.js";
import useListData from "@/assets/js/useListData.js";
import { 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.vue";

const route = useRoute();
const { COURSE_ID, COURSE_TYPE } = route.query;

const { list, pagination, searchForm, fnGetData, fnResetPagination, tableRef } =
  useListData(getQuestionListByCoursewareIdAndType, {
    otherParams: { COURSEWAREID: COURSE_ID, COURSEWARETYPE: COURSE_TYPE },
  });

const buttonJurisdiction = await useButtonJurisdiction("courseware");

const data = reactive({
  importDialogVisible: false,
  questionTypeOptions: [
    {
      value: "1",
      label: "单选题",
    },
    {
      value: "2",
      label: "多选题",
    },
    {
      value: "3",
      label: "判断题",
    },
  ],
  addOrEditDialog: {
    visible: false,
    form: {},
    type: "add",
  },
});

const fnGetDataTransfer = async () => {
  await fnGetData({
    COURSEWAREID: COURSE_ID,
    COURSEWARETYPE: COURSE_TYPE,
  });
};

const fnResetPaginationTransfer = async () => {
  await fnResetPagination({
    COURSEWAREID: COURSE_ID,
    COURSEWARETYPE: COURSE_TYPE,
  });
};

const fnFormatterQuestionType = (row) => {
  let label = "";
  data.questionTypeOptions.forEach((option) => {
    if (option.value === row.QUESTIONTYPE) {
      label = option.label;
    }
  });
  return label;
};

const fnDelete = debounce(
  1000,
  async (QUESTION_ID) => {
    await ElMessageBox.confirm("确定要删除吗？", { type: "warning" });
    await deleteQuestion({ QUESTION_ID });
    ElMessage.success("删除成功");
    fnGetDataTransfer();
  },
  { 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.RISKUNIT_ID).join(",");
    await deleteQuestionBatch({ DATA_IDS });
    ElMessage.success("删除成功");
    fnResetPaginationTransfer();
  },
  { atBegin: true }
);

const fnImportDialogChangeShow = () => {
  data.importDialogVisible = !data.importDialogVisible;
};

const fnSubmitImport = async (formData) => {
  formData.append("COURSEWARETYPE", COURSE_TYPE);
  formData.append("COURSEWAREID", COURSE_ID);
  const respData = await importQuestion(formData);
  ElMessage.success(respData.msg);
  fnImportDialogChangeShow();
  fnResetPaginationTransfer();
};

const fnAddOrEdit = (row) => {
  data.addOrEditDialog.form = {};
  if (row) {
    data.addOrEditDialog.form = JSON.parse(JSON.stringify(row));
    data.addOrEditDialog.type = "edit";
  } else {
    data.addOrEditDialog.form.COURSEWAREID = COURSE_ID;
    data.addOrEditDialog.form.COURSEWARETYPE = COURSE_TYPE;
    data.addOrEditDialog.type = "add";
  }
  data.addOrEditDialog.visible = true;
};
</script>

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