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

import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { EmployeeInfoApi } from '#/api/hr/employeeModel';

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

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

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { apiPage, exportEmployeeList } from '#/api/hr/resignEmployee';
import { GhostButton } from '#/components/global/button';
import { commonDownloadExcel } from '#/utils/file/download';

import employeeDetailDrawer from '../employeeInfo/employee-detail-drawer.vue';
import { querySchema, useColumns } from './data';
import resignEmployeeImportModal from './resign-employee-import-modal.vue';

/**
 * 导入
 */
const [ResignEmployeeImportModal, resignEmployeeImportModalApi] = useVbenModal({
  connectedComponent: resignEmployeeImportModal,
});
function handleImport() {
  resignEmployeeImportModalApi.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',
  // 日期选择格式化
  fieldMappingTime: [
    [
      'entryDate',
      ['params[entryBeginTime]', 'params[entryEndTime]'],
      ['YYYY-MM-DD 00:00:00', 'YYYY-MM-DD 23:59:59'],
    ],
  ],
};

const [EmployeeDetailDrawer, employeeDetailDrawerApi] = useVbenDrawer({
  connectedComponent: employeeDetailDrawer,
});

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions,
  gridOptions: {
    columns: useColumns(),
    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 onRefresh() {
  gridApi.query();
}
function onEdit(row: EmployeeInfoApi.Employee) {
  // formDrawerApi.setData({ id: row.id }).open();
  employeeDetailDrawerApi.setData({ id: row.id, resignFlag: true }).open();
}
function handleDownloadEmployeeList() {
  commonDownloadExcel(
    exportEmployeeList,
    '员工信息',
    gridApi.formApi.form.values,
  );
}
</script>
<template>
  <Page auto-content-height>
    <EmployeeDetailDrawer />
    <Grid table-title="离职员工列表">
      <template #toolbar-tools>
        <Space>
          <Button
            v-access:code="['employee:infoResign:export']"
            @click.stop="handleDownloadEmployeeList"
          >
            <template #icon>
              <ExportOutlined />
            </template>
            导出
          </Button>
          <Button
            v-access:code="['employee:infoResign:import']"
            @click="handleImport"
            type="primary"
          >
            <template #icon>
              <UploadOutlined />
            </template>
            导入
          </Button>
        </Space>
      </template>
      <template #action="{ row }">
        <!-- 租户管理员不可修改admin角色 防止误操作 -->
        <!-- 超级管理员可通过租户切换来操作租户管理员角色 -->
        <!--  -->
        <!-- <template
          v-if="!row.superAdmin && (row.roleKey !== 'admin' || isSuperAdmin)"
        > -->
        <Space>
          <GhostButton
            v-access:code="['employee:infoResign:query']"
            @click.stop="onEdit(row)"
          >
            查看
          </GhostButton>
        </Space>
        <!-- </template> -->
      </template>
    </Grid>
    <ResignEmployeeImportModal @reload="onRefresh" />
  </Page>
</template>
