<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined">新增</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls">导出</a-button>
        <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined" />
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>
            批量操作
            <Icon icon="mdi:chevron-down" />
          </a-button>
        </a-dropdown>
      </template>

      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>

    <SchoolModal @register="registerModal" @success="handleSuccess" />
    <SchoolFullDrawer @register="registerDrawer" />
    <SchoolSubManagerDrawer @register="registerSubDrawer" v-bind="$attrs" />
  </div>
</template>

<script lang="ts" name="school-school" setup>
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns, searchFormSchema } from './School.data';
  import { listOrSearch, deleteOne, batchDelete, getImportUrl, getExportUrl } from './School.api';
  import SchoolModal from './components/SchoolModal.vue';
  import SchoolFullDrawer from './components/SchoolFullDrawer.vue';
  import SchoolSubManagerDrawer from './components/SchoolSubManagerDrawer.vue';

  import { columns as detailColumns, searchFormSchema as detailSearchSchemas } from '../detail/SchoolDetail.data';
  import { list as detailList, deleteOne as detailDeleteOne, batchDelete as detailBatchDelete } from '../detail/SchoolDetail.api';
  import SchoolDetailModal from '../detail/components/SchoolDetailModal.vue';

  import { columns as nameColumns, searchFormSchema as nameSearchSchemas } from '../name/SchoolName.data';
  import { list as nameList, deleteOne as nameDeleteOne, batchDelete as nameBatchDelete } from '../name/SchoolName.api';
  import SchoolNameModal from '../name/components/SchoolNameModal.vue';

  import { columns as tagColumns, searchFormSchema as tagSearchSchemas } from '../tag/SchoolTag.data';
  import { list as tagList, deleteOne as tagDeleteOne, batchDelete as tagBatchDelete } from '../tag/SchoolTag.api';
  import SchoolTagModal from '../tag/components/SchoolTagModal.vue';

  import { columns as collegeColumns, searchFormSchema as collegeSearchSchemas } from '../college/SchoolCollege.data';
  import { list as collegeList, deleteOne as collegeDeleteOne, batchDelete as collegeBatchDelete } from '../college/SchoolCollege.api';
  import SchoolCollegeModal from '../college/components/SchoolCollegeModal.vue';

  import { columns as majorColumns, searchFormSchema as majorSearchSchemas } from '../major/SchoolMajor.data';
  import { list as majorList, deleteOne as majorDeleteOne, batchDelete as majorBatchDelete } from '../major/SchoolMajor.api';
  import SchoolMajorModal from '../major/components/SchoolMajorModal.vue';

  import { columns as majorTagColumns, searchFormSchema as majorTagSearchSchemas } from '../majorTag/SchoolMajorTag.data';
  import { list as majorTagList, deleteOne as majorTagDeleteOne, batchDelete as majorTagBatchDelete } from '../majorTag/SchoolMajorTag.api';
  import SchoolMajorTagModal from '../majorTag/components/SchoolMajorTagModal.vue';

  import { columns as campusColumns, searchFormSchema as campusSearchSchemas } from '../campus/SchoolCampus.data';
  import { list as campusList, deleteOne as campusDeleteOne, batchDelete as campusBatchDelete } from '../campus/SchoolCampus.api';
  import SchoolCampusModal from '../campus/components/SchoolCampusModal.vue';

  import { columns as dormColumns, searchFormSchema as dormSearchSchemas } from '../dorm/SchoolDorm.data';
  import { list as dormList, deleteOne as dormDeleteOne, batchDelete as dormBatchDelete } from '../dorm/SchoolDorm.api';
  import SchoolDormModal from '../dorm/components/SchoolDormModal.vue';

  import { columns as mediaColumns, searchFormSchema as mediaSearchSchemas } from '../media/SchoolMedia.data';
  import { list as mediaList, deleteOne as mediaDeleteOne, batchDelete as mediaBatchDelete } from '../media/SchoolMedia.api';
  import SchoolMediaModal from '../media/components/SchoolMediaModal.vue';

  import { columns as enrollColumns, searchFormSchema as enrollSearchSchemas } from '../enrollPlan/SchoolEnrollPlan.data';
  import { list as enrollList, deleteOne as enrollDeleteOne, batchDelete as enrollBatchDelete } from '../enrollPlan/SchoolEnrollPlan.api';
  import SchoolEnrollPlanModal from '../enrollPlan/components/SchoolEnrollPlanModal.vue';

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

  const [registerSubDrawer, { openDrawer: openSubDrawer }] = useDrawer();

  const { tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '学校主表',
      api: listOrSearch,
      columns,
      canResize: false,
      formConfig: {
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: [],
      },
      actionColumn: {
        width: 140,
        fixed: 'right',
      },
    },
    exportConfig: {
      name: '学校主表',
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess,
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  function handleAdd() {
    openModal(true, { isUpdate: false, showFooter: true });
  }

  function handleEdit(record: Recordable) {
    openModal(true, { record, isUpdate: true, showFooter: true });
  }

  function handleDetail(record: Recordable) {
    openModal(true, { record, isUpdate: true, showFooter: false });
  }

  function handleFullDetail(record: Recordable) {
    openDrawer(true, { record });
  }

  function openSubManager(title: string, config: Recordable, record: Recordable) {
    openSubDrawer(true, Object.assign({ title, record }, config));
  }

  async function handleDelete(record: Recordable) {
    await deleteOne({ id: record.id }, handleSuccess);
  }

  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }

  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }

  function getTableAction(record: Recordable) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
    ];
  }

  function getDropDownAction(record: Recordable) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '全量详情',
        onClick: handleFullDetail.bind(null, record),
      },
      {
        label: '学校详情管理',
        onClick: () =>
          openSubManager(
            '学校详情管理',
            {
              fixedMode: 'schoolId',
              columns: detailColumns,
              searchSchemas: detailSearchSchemas,
              listApi: detailList,
              deleteOne: detailDeleteOne,
              batchDelete: detailBatchDelete,
              modalComponent: SchoolDetailModal,
            },
            record
          ),
      },
      {
        label: '学校名称管理',
        onClick: () =>
          openSubManager(
            '学校名称管理',
            {
              fixedMode: 'schoolId',
              columns: nameColumns,
              searchSchemas: nameSearchSchemas,
              listApi: nameList,
              deleteOne: nameDeleteOne,
              batchDelete: nameBatchDelete,
              modalComponent: SchoolNameModal,
            },
            record
          ),
      },
      {
        label: '学校标签管理',
        onClick: () =>
          openSubManager(
            '学校标签管理',
            {
              fixedMode: 'schoolId',
              columns: tagColumns,
              searchSchemas: tagSearchSchemas,
              listApi: tagList,
              deleteOne: tagDeleteOne,
              batchDelete: tagBatchDelete,
              modalComponent: SchoolTagModal,
            },
            record
          ),
      },
      {
        label: '学院管理',
        onClick: () =>
          openSubManager(
            '学院管理',
            {
              fixedMode: 'schoolId',
              columns: collegeColumns,
              searchSchemas: collegeSearchSchemas,
              listApi: collegeList,
              deleteOne: collegeDeleteOne,
              batchDelete: collegeBatchDelete,
              modalComponent: SchoolCollegeModal,
            },
            record
          ),
      },
      {
        label: '专业管理',
        onClick: () =>
          openSubManager(
            '专业管理',
            {
              fixedMode: 'schoolId',
              columns: majorColumns,
              searchSchemas: majorSearchSchemas,
              listApi: majorList,
              deleteOne: majorDeleteOne,
              batchDelete: majorBatchDelete,
              modalComponent: SchoolMajorModal,
              getExtraDropDownActions: (r) => [
                {
                  label: '专业标签管理',
                  onClick: () =>
                    openSubDrawer(true, {
                      title: '专业标签管理',
                      majorId: r.id,
                      ownerName: r.majorName || r.majorCode || String(r.id),
                      fixedMode: 'majorId',
                      columns: majorTagColumns,
                      searchSchemas: majorTagSearchSchemas,
                      listApi: majorTagList,
                      deleteOne: majorTagDeleteOne,
                      batchDelete: majorTagBatchDelete,
                      modalComponent: SchoolMajorTagModal,
                    }),
                },
              ],
            },
            record
          ),
      },
      {
        label: '校区管理',
        onClick: () =>
          openSubManager(
            '校区管理',
            {
              fixedMode: 'schoolId',
              columns: campusColumns,
              searchSchemas: campusSearchSchemas,
              listApi: campusList,
              deleteOne: campusDeleteOne,
              batchDelete: campusBatchDelete,
              modalComponent: SchoolCampusModal,
            },
            record
          ),
      },
      {
        label: '宿舍管理',
        onClick: () =>
          openSubManager(
            '宿舍管理',
            {
              fixedMode: 'schoolId',
              columns: dormColumns,
              searchSchemas: dormSearchSchemas,
              listApi: dormList,
              deleteOne: dormDeleteOne,
              batchDelete: dormBatchDelete,
              modalComponent: SchoolDormModal,
            },
            record
          ),
      },
      {
        label: '媒体管理',
        onClick: () =>
          openSubManager(
            '媒体管理',
            {
              fixedMode: 'bizSchool',
              bizType: 'school',
              columns: mediaColumns,
              searchSchemas: mediaSearchSchemas,
              listApi: mediaList,
              deleteOne: mediaDeleteOne,
              batchDelete: mediaBatchDelete,
              modalComponent: SchoolMediaModal,
            },
            record
          ),
      },
      {
        label: '招生计划管理',
        onClick: () =>
          openSubManager(
            '招生计划管理',
            {
              fixedMode: 'schoolId',
              columns: enrollColumns,
              searchSchemas: enrollSearchSchemas,
              listApi: enrollList,
              deleteOne: enrollDeleteOne,
              batchDelete: enrollBatchDelete,
              modalComponent: SchoolEnrollPlanModal,
            },
            record
          ),
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }
</script>
