<script setup lang="ts">
import ahTable from "@/components/hTable/index.vue";
import {
  ConditionalType,
  intTableData,
  TableColumnSearch,
  TableConfig,
} from "@/components/hTable/hTable";
import { onMounted, ref } from "vue";
import { fa } from "element-plus/es/locales.mjs";
import { hTableAPI } from "@/api/hTable";
import { getenum } from "@/api/enum";
import {
  ruleNumber,
  ruleRequired,
  ruleRequiredGrade,
  ruleRequiredI,
  ruleRequiredNumber,
} from "@/utils/rules";
import { ImportExamInfo } from "@/api/exam";
import { ElMessage } from "element-plus";
import { entryExamInfo } from "./examFun";
const ControllerName = "Exam";

defineOptions({
  name: ControllerName,
});

function searchCallback(data) {}

const table = ref<{ initTable: (config: TableConfig) => void }>();
const tableData: TableConfig = intTableData({
  apiUrl: ControllerName,
  selectColumn: false, // 列表选择
  border: false, // 是否显示表格边框
  searchCallback: searchCallback,
  search: {
    // 查询条件
    show: true,
    PageIndex: 0,
    PageSize: 20,
    OrderBy: "Id", // 排序
    defaultConditions: [], // 默认查询条件
    Conditions: [],
  },
  operationColumn: true, // 显示操作按钮
  operationColumnData: [
    {
      // 操作按钮
      topBtn: false, // 是头部按钮
      label: "修改",
      btnType: "edit", // 按钮类型 add edit del custom
    },
    {
      // 操作按钮
      topBtn: true, // 是头部按钮
      label: "添加",
      btnStyle: "success",
      btnType: "add", // 按钮类型 add edit del custom
    },
    {
      topBtn: false, // 头部按钮
      show: true,
      label: "删除",
      btnType: "del", // 按钮类型 add edit del 不设置则 自定义按钮
      btnStyle: "danger", // topBtn: true才生效 success danger
    },
    {
      topBtn: false, // 头部按钮
      show: true,
      label: "详情",
      btnType: "custom", // 按钮类型 add edit del 不设置则 自定义按钮
      btnStyle: "primary",
      custom: {
        title: "考试班级详情", // 弹出框title
        src: "exam/classDetails", // 组件路径
        width: "1300px", // 弹框宽度
        height: "800px", // 弹框高度
      },
    },
    {
      topBtn: false, // 头部按钮
      show: true,
      label: "分段",
      btnType: "custom", // 按钮类型 add edit del 不设置则 自定义按钮
      btnStyle: "info",
      custom: {
        title: "考试分段", // 弹出框title
        src: "exam/examTags", // 组件路径
        width: "1200px", // 弹框宽度
        height: "800px", // 弹框高度
      },
    },
    {
      topBtn: false, // 头部按钮
      show: true,
      label: "录入成绩",
      click: entryExam,
      btnStyle: "primary", // topBtn: true才生效 success danger
    },
    {
      topBtn: true, // 头部按钮
      show: true,
      label: "录入成绩模板",
      click: DwImportTemplate,
      btnStyle: "info", // topBtn: true才生效 success danger
    },
  ],
  column: {
    // 行数据
    id: {
      label: "编号",
      search: new TableColumnSearch(true),
      width: "150px",
    },
    name: {
      label: "考试名称",
      width: "200px",
      search: new TableColumnSearch(true, ConditionalType.Like), // 搜索
      edit: {
        add: true,
        edit: true,
        rules: ruleRequiredI(20, 2),
      },
    },
    gradeLevel: {
      label: "年级",
      width: "100px",
      type: "dropdown",
      custom: (row) => row.gradeLevel + row.gradeYear + "届",
      search: new TableColumnSearch(true),
      edit: {
        add: true,
        edit: false,
        rules: ruleRequired,
      },
    },
    gradeYear: {
      label: "毕业年份",
      width: "100px",
      show: false,
      search: new TableColumnSearch(true),
      edit: {
        add: true,
        edit: false,
        rules: ruleRequiredNumber,
      },
    },
    testPaperType: {
      label: "试卷类型",
      width: "100px",
      type: "dropdown",
      search: new TableColumnSearch(true),
      edit: {
        add: true,
        edit: true,
        rules: ruleRequired,
      },
    },
    type: {
      label: "考试类型",
      width: "100px",
      type: "dropdown",
      search: new TableColumnSearch(true),
      edit: {
        add: true,
        edit: true,
        rules: ruleRequired,
      },
    },
    scoreLine: {
      label: "划线分数",
      width: "100px",
      edit: {
        add: true,
        edit: true,
        rules: ruleNumber,
      },
    },
    baseSchoolScore: {
      label: "资源校平均分",
      width: "150px",
      edit: {
        add: true,
        edit: true,
        rules: ruleNumber,
      },
    },
    startTime: {
      label: "考试时间",
      width: "180px",
      type: "datetime",
      custom: (row) => row.startTime?.replace("T", " ").substring(0, 10) ?? "",
      search: new TableColumnSearch(true),
      edit: {
        add: true,
        edit: true,
        rules: ruleRequired,
      },
    },
    createTime: {
      label: "创建时间",
      type: "datetime",
      custom: (row) => row.startTime?.replace("T", " ").substring(0, 10) ?? "",
    },
  },
  data: [],
  pageData: {
    total: 0,
  },
  selectRows: [],
});

function DwImportTemplate(obj, row, callBack) {
  const baseUrl = import.meta.env.VITE_API_BASEURL;
  const excelImportUsersUrl = `${baseUrl}/ExamClassInfo/DwImportTemplate`;
  window.open(excelImportUsersUrl, "_blank");
}
function entryExam(obj, row, callBack) {
  entryExamInfo(row[0].id);
}
const showTable = ref(false);
onMounted(async () => {
  //初始化数据原

  tableData.column.gradeLevel.setting.datasource = (
    await getenum("GradeLevelEnum")
  ).data.map((s) => {
    return { value: s.text, text: s.text };
  });

  tableData.column.testPaperType.setting.datasource = (
    await getenum("TestPaperTypeEnum")
  ).data;
  tableData.column.type.setting.datasource = (await getenum("ExamTypeEnum")).data;

  showTable.value = true;
});
</script>

<template>
  <div><ahTable v-if="showTable" ref="table" :tableConfig="tableData" /></div>
</template>
