<script setup lang="tsx">
import { ref } from 'vue';
import { NDivider } from 'naive-ui';
import { fetchBatchDeleteSchoolRecruitMajor, fetchGetSchoolRecruitMajorList } from '@/service/api/art/school-recruit-major';
import { useAppStore } from '@/store/modules/app';
import { useAuth } from '@/hooks/business/auth';
import { useDownload } from '@/hooks/business/download';
import { defaultTransform, useNaivePaginatedTable, useTableOperate } from '@/hooks/common/table';
import { $t } from '@/locales';
import ButtonIcon from '@/components/custom/button-icon.vue';
import SchoolRecruitMajorOperateDrawer from './modules/school-recruit-major-operate-drawer.vue';
import SchoolRecruitMajorSearch from './modules/school-recruit-major-search.vue';

defineOptions({
  name: 'SchoolRecruitMajorList'
});


const appStore = useAppStore();
const { download } = useDownload();
const { hasAuth } = useAuth();

const searchParams = ref<Api.Art.SchoolRecruitMajorSearchParams>({
  pageNum: 1,
  pageSize: 10,
  schoolId: null,
  schoolCode: null,
  schoolName: null,
  year: null,
  majorId: null,
  majorCode: null,
  majorName: null,
  enrollCode: null,
  dataStatus: null,
  batchName: null,
  majorType: null,
  majorTypeSub: null,
  subjectType: null,
  admissionWayShort: null,
  admissionWayExternal: null,
  admissionWayExternalOp: null,
  admissionWayInternal: null,
  admissionWayInternalOp: null,
  planEnroll: null,
  mainExamSubject: null,
  schoolingYears: null,
  enrollLimitDesc: null,
  tuitionFee: null,
  cultureScoreLimit: null,
  majorScoreLimit: null,
  chineseScoreLimit: null,
  englishScoreLimit: null,
  params: {}
});

const { columns, columnChecks, data, getData, getDataByPage, loading, mobilePagination, scrollX } =
  useNaivePaginatedTable({
  api: () => fetchGetSchoolRecruitMajorList(searchParams.value),
  transform: response => defaultTransform(response),
  onPaginationParamsChange: params => {
    searchParams.value.pageNum = params.page;
    searchParams.value.pageSize = params.pageSize;
  },
  columns: () => [
    {
      type: 'selection',
      align: 'center',
      width: 48
    },
    {
      key: 'index',
      title: $t('common.index'),
      align: 'center',
      width: 64,
      render: (_, index) => index + 1
    },
    {
      key: 'recruitMajorId',
      title: '院校招录专业ID',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'schoolId',
      title: '学校ID',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'schoolCode',
      title: '学校代码',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'schoolName',
      title: '学校名称(冗余)',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'year',
      title: '年份',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'majorId',
      title: '专业ID',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'majorCode',
      title: '专业代码',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'majorName',
      title: '专业名称',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'enrollCode',
      title: '招生代码(为空则存空串)',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'dataStatus',
      title: '数据状态(停招/新招/新增)',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'batchName',
      title: '批次',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'majorType',
      title: '专业类型',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'majorTypeSub',
      title: '二级专业类型',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'subjectType',
      title: '科类(文/理)',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'admissionWayShort',
      title: '录取方式缩写',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'admissionWayExternal',
      title: '对外录取方式',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'admissionWayExternalOp',
      title: '对外录取方式运算符',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'admissionWayInternal',
      title: '内部录取方式',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'admissionWayInternalOp',
      title: '内部录取方式运算符',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'planEnroll',
      title: '计划招生人数',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'mainExamSubject',
      title: '主考科目',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'schoolingYears',
      title: '学制(年)',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'enrollLimitDesc',
      title: '院校限制说明',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'tuitionFee',
      title: '学费(元/年)',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'cultureScoreLimit',
      title: '文化分数限制',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'majorScoreLimit',
      title: '专业分数限制',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'chineseScoreLimit',
      title: '语文成绩限制',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'englishScoreLimit',
      title: '英语成绩限制',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'remark',
      title: '备注',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'operate',
      title: $t('common.operate'),
      align: 'center',
      width: 130,
      render: row => {
        const divider = () => {
          if (!hasAuth('art:schoolRecruitMajor:edit') || !hasAuth('art:schoolRecruitMajor:remove')) {
            return null;
          }
          return <NDivider vertical />;
        };

        const editBtn = () => {
          if (!hasAuth('art:schoolRecruitMajor:edit')) {
            return null;
          }
          return (
            <ButtonIcon
              text
              type="primary"
              icon="material-symbols:drive-file-rename-outline-outline"
              tooltipContent={$t('common.edit')}
              onClick={() => edit(row.recruitMajorId)}
            />
          );
        };

        const deleteBtn = () => {
          if (!hasAuth('art:schoolRecruitMajor:remove')) {
            return null;
          }
          return (
            <ButtonIcon
              text
              type="error"
              icon="material-symbols:delete-outline"
              tooltipContent={$t('common.delete')}
              popconfirmContent={$t('common.confirmDelete')}
              onPositiveClick={() => handleDelete(row.recruitMajorId)}
            />
          );
        };

        return (
          <div class="flex-center gap-8px">
            {editBtn()}
            {divider()}
            {deleteBtn()}
          </div>
        );
      }
    }
  ]
});

const { drawerVisible, operateType, editingData, handleAdd, handleEdit, checkedRowKeys, onBatchDeleted, onDeleted } =
  useTableOperate(data, 'recruitMajorId', getData);

async function handleBatchDelete() {
  // request
  const { error } = await fetchBatchDeleteSchoolRecruitMajor(checkedRowKeys.value);
  if (error) return;
  onBatchDeleted();
}

async function handleDelete(recruitMajorId: CommonType.IdType) {
  // request
  const { error } = await fetchBatchDeleteSchoolRecruitMajor([recruitMajorId]);
  if (error) return;
  onDeleted();
}

function edit(recruitMajorId: CommonType.IdType) {
  handleEdit(recruitMajorId);
}

function handleExport() {
  download('/art/schoolRecruitMajor/export', searchParams.value, `院校招录专业_${new Date().getTime()}.xlsx`);
}
</script>

<template>
  <div class="min-h-500px flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <SchoolRecruitMajorSearch v-model:model="searchParams" @search="getDataByPage" />
    <NCard title="院校招录专业列表" :bordered="false" size="small" class="card-wrapper sm:flex-1-hidden">
      <template #header-extra>
        <TableHeaderOperation
          v-model:columns="columnChecks"
          :disabled-delete="checkedRowKeys.length === 0"
          :loading="loading"
          :show-add="hasAuth('art:schoolRecruitMajor:add')"
          :show-delete="hasAuth('art:schoolRecruitMajor:remove')"
          :show-export="hasAuth('art:schoolRecruitMajor:export')"
          @add="handleAdd"
          @delete="handleBatchDelete"
          @export="handleExport"
          @refresh="getData"
        />
      </template>
      <NDataTable
        v-model:checked-row-keys="checkedRowKeys"
        :columns="columns"
        :data="data"
        size="small"
        :flex-height="!appStore.isMobile"
        :scroll-x="scrollX"
        :loading="loading"
        remote
        :row-key="row => row.recruitMajorId"
        :pagination="mobilePagination"
        class="sm:h-full"
      />
      <SchoolRecruitMajorOperateDrawer
        v-model:visible="drawerVisible"
        :operate-type="operateType"
        :row-data="editingData"
        @submitted="getDataByPage"
      />
    </NCard>
  </div>
</template>

<style scoped></style>
