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

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

import { useRouter } from 'vue-router';

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

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

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { apiDelete, apiPage } from '#/api/hr/staffingPlanRecords';

import { querySchema, useColumns } from './data';
import staffingPlanRecordModal from './modal.vue';

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 [StaffingPlanRecordModal, modalApi] = useVbenModal({
  connectedComponent: staffingPlanRecordModal,
});

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<StaffingPlanRecordsApi.StaffingPlanRecords>) {
  switch (code) {
    case 'delete': {
      onDelete(row);
      break;
    }
    case 'detail': {
      onRecordData(row);
      break;
    }
    case 'edit': {
      onEdit(row);
      break;
    }
    default: {
      break;
    }
  }
}

function onRefresh() {
  gridApi.query();
}
function onEdit(row: StaffingPlanRecordsApi.StaffingPlanRecords) {
  modalApi.setData(row).open();
}
function onCreate() {
  modalApi.setData({}).open();
}
function onDelete(row: StaffingPlanRecordsApi.StaffingPlanRecords) {
  const hideLoading = message.loading({
    content: `正在删除${row.recordDate}...`,
    duration: 0,
    key: 'action_process_msg',
  });
  apiDelete(row.id || 0)
    .then(() => {
      message.success({
        content: `${row.recordDate}删除成功`,
        key: 'action_process_msg',
      });
      onRefresh();
    })
    .catch(() => {
      hideLoading();
    });
}
const router = useRouter();
function onRecordData(record: StaffingPlanRecordsApi.StaffingPlanRecords) {
  router.push(`/hr/report/staffingPlan/data/${record.id}`);
}
// function handleDownloadExcel() {
//   commonDownloadExcel(apiExport, '编制规划记录', gridApi.formApi.form.values);
// }
</script>
<template>
  <Page auto-content-height>
    <StaffingPlanRecordModal @success="onRefresh" />
    <Grid table-title="编制规划记录列表">
      <template #toolbar-tools>
        <Space>
          <!-- <Button
            v-access:code="['employee:staffingPlanRecords:export']"
            @click="handleDownloadExcel"
          >
            导出
          </Button> -->
          <Button
            v-access:code="['employee:staffingPlanRecords:add']"
            type="primary"
            @click="onCreate"
          >
            <Plus class="size-5" />
            新增
          </Button>
        </Space>
      </template>
    </Grid>
  </Page>
</template>
