<script setup lang="ts">
import ahTable from "@/components/hTable/index.vue";
import { ConditionalType, 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,
  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 = {
  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: "录入成绩",
      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: true,
      add: false, // 字段允许添加
      edit: false, // 字段允许修改
      width: "150px",
    },
    name: {
      label: "考试名称",
      rules: ruleRequired,
      width: "200px",
      search: true,
      searchType: ConditionalType.Like, // 搜索类型
      add: true, // 字段允许添加
      edit: true, // 字段允许修改
      setting: {},
    },
    grade: {
      label: "年级",
      rules: ruleRequiredI(6, 2),
      width: "100px",
      search: true,
      add: true, // 字段允许添加
      edit: false, // 字段允许修改
    },
    testPaperType: {
      label: "试卷类型",
      rules: ruleRequired,
      width: "100px",
      type: "dropdown",
      setting: {},
      search: true,
      add: true, // 字段允许添加
      edit: true, // 字段允许修改
    },
    type: {
      label: "考试类型",
      rules: ruleRequired,
      width: "100px",
      type: "dropdown",
      setting: {},
      search: true,
      add: true, // 字段允许添加
      edit: true, // 字段允许修改
    },
    scoreLine: {
      label: "划线分数",
      rules: ruleNumber,
      search: false,
      width: "100px",
      add: true, // 字段允许添加
      edit: true, // 字段允许修改
    },
    baseSchoolScore: {
      label: "资源校平均分",
      rules: ruleNumber,
      search: false,
      width: "150px",
      add: true, // 字段允许添加
      edit: true, // 字段允许修改
    },
    startTime: {
      label: "考试时间",
      width: "210px",
      rules: ruleRequired,
      search: true,
      type: "datetime",
      setting: {},
      add: true, // 字段允许添加
      edit: true, // 字段允许修改
    },
    createTime: {
      label: "创建时间",
      type: "datetime",
      search: false,
      add: false, // 字段允许添加
      edit: false, // 字段允许修改
    },
  },
  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.level.setting.datasource = (await getenum("GradeEnum")).data;

  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>
