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

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

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

import { Button, message, Popconfirm, Space } from 'ant-design-vue';

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

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

/**
 * 导入
 */
const [EmployeeImportModal, employeeImportModalApi] = useVbenModal({
  connectedComponent: employeeImportModal,
});
function handleImport() {
  employeeImportModalApi.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 [FormDrawer, formDrawerApi] = useVbenDrawer({
  connectedComponent: Form,
});

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 onActionClick({
//   code,
//   row,
// }: OnActionClickParams<EmployeeInfoApi.Employee>) {
//   switch (code) {
//     case 'delete': {
//       onResign(row);
//       break;
//     }
//     case 'edit': {
//       onEdit(row);
//       break;
//     }
//     case 'export': {
//       handleDownloadExcel(row);
//       break;
//     }
//     default: {
//       break;
//     }
//   }
// }

function onRefresh() {
  gridApi.query();
}
function onEdit(row: EmployeeInfoApi.Employee) {
  formDrawerApi.setData(row).open();
}
function onCreate() {
  formDrawerApi.setData({}).open();
}
function onResign(row: EmployeeInfoApi.Employee) {
  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(row: EmployeeInfoApi.Employee) {
  commonDownloadExcel(apiExport, '员工信息', row.id);
}
</script>
<template>
  <Page auto-content-height>
    <FormDrawer @success="onRefresh" />
    <Grid table-title="员工信息列表">
      <template #toolbar-tools>
        <Space>
          <Button
            v-access:code="['employee:info:import']"
            @click="handleImport"
          >
            导入
          </Button>
          <!-- <Button
            v-access:code="['employee:info:export']"
            @click="handleDownloadExcel"
          >
            导出
          </Button> -->
          <Button
            v-access:code="['employee:info:add']"
            type="primary"
            @click="onCreate"
          >
            <Plus class="size-5" />
            入职
          </Button>
        </Space>
      </template>
      <template #action="{ row }">
        <!-- 租户管理员不可修改admin角色 防止误操作 -->
        <!-- 超级管理员可通过租户切换来操作租户管理员角色 -->
        <!--  -->
        <!-- <template
          v-if="!row.superAdmin && (row.roleKey !== 'admin' || isSuperAdmin)"
        > -->
        <Space>
          <GhostButton
            v-access:code="['employee:info:export']"
            @click.stop="handleDownloadExcel(row)"
          >
            导出简历
          </GhostButton>
          <GhostButton
            v-access:code="['employee:info:edit']"
            @click.stop="onEdit(row)"
          >
            编辑
          </GhostButton>
          <!-- <GhostButton
            v-access:code="['auth:role:edit']"
            @click.stop="onAssignRole(row)"
          >
            分配
          </GhostButton> -->
          <Popconfirm
            :get-popup-container="getVxePopupContainer"
            placement="left"
            :title="`确认离职【${row.name}】？`"
            @confirm="onResign(row)"
          >
            <GhostButton
              danger
              v-access:code="['employee:info:resign']"
              @click.stop=""
            >
              离职
            </GhostButton>
          </Popconfirm>
        </Space>
        <!-- </template> -->
      </template>
    </Grid>
    <EmployeeImportModal @reload="onRefresh" />
  </Page>
</template>
