<script lang="ts" setup>
import type { VbenFormProps } from '@vben/common-ui';

import type {
  OnActionClickParams,
  VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { InterviewProfileApi } from '#/api/hr/interviewProfile';

import { Page, useVbenDrawer, useVbenModal } from '@vben/common-ui';
import { Plus } from '@vben/icons';

import { ExportOutlined, UploadOutlined } from '@ant-design/icons-vue';
import { Button, message, Space } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { apiDelete, apiExport, apiPage } from '#/api/hr/interviewProfile';
import { commonDownloadExcel } from '#/utils/file/download';

import { querySchema, useColumns } from './data';
import Form from './form.vue';
import interviewImportModal from './interview-import-modal.vue';

/**
 * 导入
 */
const [InterviewImportModal, interviewImportModalApi] = useVbenModal({
  connectedComponent: interviewImportModal,
});
function handleImport() {
  interviewImportModalApi.open();
}

const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 80,
    componentProps: {
      allowClear: true,
    },
  },
  schema: querySchema,
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
};

const [FormDrawer, formDrawerApi] = useVbenDrawer({
  connectedComponent: Form,
});

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions,
  gridOptions: {
    columns: useColumns(onActionClick),
    height: 'auto',
    keepSource: true,
    pagerConfig: {
      enabled: true,
    },
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues = {}) => {
          return await apiPage({
            pageNo: page.currentPage,
            pageSize: page.pageSize,
            ...formValues,
          });
        },
      },
    },
    rowConfig: {
      keyField: 'id',
      // 高亮当前行
      isCurrent: true,
    },
  } as VxeTableGridOptions,
});

function onActionClick({
  code,
  row,
}: OnActionClickParams<InterviewProfileApi.InterviewProfile>) {
  switch (code) {
    case 'delete': {
      onDelete(row);
      break;
    }
    case 'edit': {
      onEdit(row);
      break;
    }
    default: {
      break;
    }
  }
}

function onRefresh() {
  gridApi.query();
}
function onEdit(row: InterviewProfileApi.InterviewProfile) {
  formDrawerApi.setData(row).open();
}
function onCreate() {
  formDrawerApi.setData({}).open();
}
function onDelete(row: InterviewProfileApi.InterviewProfile) {
  const hideLoading = message.loading({
    content: `正在删除${row.name}...`,
    duration: 0,
    key: 'action_process_msg',
  });
  apiDelete(row.id || 0)
    .then(() => {
      message.success({
        content: `${row.name}删除成功`,
        key: 'action_process_msg',
      });
      onRefresh();
    })
    .catch(() => {
      hideLoading();
    });
}
function handleDownloadExcel() {
  commonDownloadExcel(apiExport, '面试档案', gridApi.formApi.form.values);
}
</script>
<template>
  <Page auto-content-height>
    <FormDrawer @success="onRefresh" />
    <Grid table-title="面试档案列表">
      <template #toolbar-tools>
        <Space>
          <Button
            type="primary"
            ghost
            v-access:code="['employee:interviewProfile:export']"
            @click.stop="handleDownloadExcel"
          >
            <template #icon>
              <ExportOutlined />
            </template>
            导出
          </Button>
          <Button
            v-access:code="['employee:interviewProfile:import']"
            @click="handleImport"
          >
            <template #icon>
              <UploadOutlined />
            </template>
            导入
          </Button>
          <Button
            v-access:code="['employee:interviewProfile:add']"
            type="primary"
            @click="onCreate"
          >
            <Plus class="size-5" />
            新增
          </Button>
        </Space>
      </template>
    </Grid>
    <InterviewImportModal @reload="onRefresh" />
  </Page>
</template>
