<script setup lang="ts">
  import { PageWrapper } from '/@/components/Page';
  import { useTable, BasicTable, TableAction } from '/@/components/Table';
  import { searchFormSchema, tableColumns } from './components/data.config';
  import FormEdit from './components/edit.vue';
  import ClassTree from './components/tree.vue';
  import FormImport from './components/import.vue';
  import FormDivision from './components/division.vue';
  import StudentImport from './components/import1.vue';
  import { useModal } from '/@/components/Modal';
  import { reactive } from 'vue';
  import { getBaseNewStudentPage } from '/@/services/apis/BaseNewStudentController';
  import { useMessage } from '/@/hooks/web/useMessage';

  const searchInfo = reactive<Recordable>({});

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

  const { createMessage } = useMessage();

  const [registerImportModal, { openModal: openImportModal }] = useModal();

  const [registerStudentImportModal, { openModal: openStudentImportModal }] = useModal();
  const [registerDivisionModal, { openModal: openDivisionModal }] = useModal();

  const [registerTable, { reload, getSelectRows }] = useTable({
    api: getBaseNewStudentPage,
    title: '新生列表',
    rowKey: 'id',
    columns: tableColumns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    immediate: true,
    canResize: true,
    actionColumn: {
      width: 100,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
    rowSelection: {
      type: 'checkbox',
    },
  });
  const handleEdit = (record: any, isUpdate: boolean) => {
    if (isUpdate === false) {
      if (searchInfo.enrollmentPlanId) {
        record.enrollmentPlanId = searchInfo.enrollmentPlanId;
      } else {
        createMessage.warning('请选择招生计划');
        return;
      }
    }
    openModal(true, {
      isUpdate: isUpdate,
      baseData: {
        ...record,
      },
    });
  };

  function handleSelect(id) {
    searchInfo.enrollmentPlanId = id;
    reload();
  }

  const handleSuccess = async () => {
    await reload();
  };

  const handelStudenImport = () => {
    if (searchInfo.enrollmentPlanId) {
      openStudentImportModal(true, { baseData: { enrollmentPlanId: searchInfo.enrollmentPlanId } });
    } else {
      createMessage.warning('请选择招生计划');
    }
  };
  const handelImport = () => {
    if (searchInfo.enrollmentPlanId) {
      openImportModal(true, { baseData: { enrollmentPlanId: searchInfo.enrollmentPlanId } });
    } else {
      createMessage.warning('请选择招生计划');
    }
  };

  const handleDivision = () => {
    const data = getSelectRows();
    if (data.length === 0) {
      createMessage.warning('请选择的学生');
      return;
    }
    openDivisionModal(true, { isUpdate: false, baseData: [...data] });
  };
</script>

<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <ClassTree class="w-1/3 xl:w-1/4" @select="handleSelect" />
    <BasicTable class="w-2/3 xl:w-3/4" @register="registerTable" :searchInfo="searchInfo">
      <template #toolbar>
        <a-button type="primary" @click="handleEdit({}, false)">新增</a-button>
        <a-button type="primary" @click="handleDivision">手动分班</a-button>
        <a-button type="primary" @click="handelStudenImport">学生导入</a-button>
        <a-button type="primary" @click="handelImport">成绩导入</a-button>
      </template>
      <template #action="{ record }">
        <div style="display: flex; justify-content: center">
          <TableAction
            :actions="[
              {
                label: '编辑',
                onClick: handleEdit.bind(null, record, true),
              },
            ]"
          />
        </div>
      </template>
    </BasicTable>
    <FormEdit @register="registerModal" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
    <StudentImport @register="registerStudentImportModal" @success="handleSuccess" />
    <FormDivision @register="registerDivisionModal" @success="handleSuccess" />
  </PageWrapper>
</template>

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