<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleEdit({}, false)">新增</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '录入成绩',
              onClick: handleEnter.bind(null, record),
            },
            {
              label: '删除',
              color: 'error',
              onClick: handleDelete.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <FormEdit @register="registerModal" @success="handleSuccess" />
    <FormDetailEdit @register="registerDetailModal" />
  </PageWrapper>
</template>

<script setup lang="ts">
  import { onMounted } from 'vue';
  import { PageWrapper } from '/@/components/Page';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { tableColumns, searchFormSchema } from './data.config';
  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import FormEdit from './edit.vue';
  import FormDetailEdit from './editForm.vue';
  import {
    getExamSubjectScoreEnterPage,
    deleteXycxeduExamSubjectScoreEnter,
  } from '/@/services/apis/ExamSubjectScoreEnterController';

  const [registerModal, { openModal }] = useModal();
  const [registerDetailModal, { openModal: openDetailModal }] = useModal();

  const [registerTable, { reload }] = useTable({
    api: getExamSubjectScoreEnterPage,
    title: '考试记录表',
    rowKey: 'id',
    columns: tableColumns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    immediate: true,
    canResize: true,
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  });
  const { createConfirm, createMessage } = useMessage();

  const handleDelete = (record: any) => {
    createConfirm({
      iconType: 'warning',
      title: '温馨提醒',
      content: '是否删除该记录?',
      onOk: async () => {
        try {
          await deleteXycxeduExamSubjectScoreEnter([record.id]);
          createMessage.success('删除成功');
          await reload();
        } catch (e) {
          createMessage.error('删除失败');
        }
      },
      okText: '确认',
      cancelText: '取消',
    });
  };

  const handleEdit = (record: any, isUpdate: boolean) => {
    openModal(true, {
      isUpdate: isUpdate,
      baseData: {
        ...record,
      },
    });
  };
  const handleEnter = (record: any) => {
    openDetailModal(true, {
      isUpdate: true,
      baseData: {
        ...record,
      },
    });
  };
  const handleSuccess = async () => {
    await reload();
  };

  onMounted(async () => {});
</script>

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