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

import type { VxeTableGridOptions } from '#/adapter/vxe-table';

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

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

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

import { querySchema, useColumns } from './data';
// import Form from './form.vue';

const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 80,
    componentProps: {
      allowClear: true,
    },
  },
  schema: querySchema,
  wrapperClass: 'grid-cols-2',
  // wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
  // 日期选择格式化
  fieldMappingTime: [
    [
      'entryDate',
      ['beginTime', 'endTime'],
      ['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: false,
    },
    proxyConfig: {
      ajax: {
        query: async (_, formValues = {}) => {
          const data = await apiPage({ ...formValues });
          return { data };
        },
      },
    },
    border: true,
    columnConfig: {
      resizable: false,
    },
    showCustomHeader: true,
    mergeHeaderCells: [
      { row: 0, col: 0, rowspan: 3, colspan: 1 },
      { row: 0, col: 1, rowspan: 3, colspan: 1 },
      { row: 0, col: 2, rowspan: 3, colspan: 1 },
      { row: 0, col: 3, rowspan: 3, colspan: 1 },
      { row: 0, col: 4, rowspan: 2, colspan: 2 },
      { row: 2, col: 4, rowspan: 1, colspan: 1 },
      { row: 2, col: 5, rowspan: 1, colspan: 1 },
      { row: 0, col: 6, rowspan: 1, colspan: 5 },
      { row: 1, col: 6, rowspan: 1, colspan: 3 },
      { row: 2, col: 6, rowspan: 1, colspan: 1 },
      { row: 2, col: 7, rowspan: 1, colspan: 1 },
      { row: 2, col: 8, rowspan: 1, colspan: 1 },
      { row: 1, col: 9, rowspan: 2, colspan: 1 },
      { row: 1, col: 10, rowspan: 2, colspan: 1 },
      { row: 0, col: 11, rowspan: 3, colspan: 1 },
      { row: 0, col: 12, rowspan: 1, colspan: 5 },
      { row: 1, col: 12, rowspan: 1, colspan: 3 },
      { row: 1, col: 15, rowspan: 2, colspan: 1 },
      { row: 1, col: 16, rowspan: 2, colspan: 1 },
    ],
    rowConfig: {
      keyField: 'plate',
      // 高亮当前行
      isCurrent: false,
    },
  } as VxeTableGridOptions,
});

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

// function onRefresh() {
//   gridApi.query();
// }
// function onEdit(row: ChangeAnalysisApi.ChangeAnalysis) {
//   formDrawerApi.setData(row).open();
// }
// function onCreate() {
//   formDrawerApi.setData({}).open();
// }
// function onDelete(row: ChangeAnalysisApi.ChangeAnalysis) {
//   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();
//     });
// }
async function handleDownloadExcel() {
  const values = await gridApi.formApi.getValues();
  commonDownloadExcel(apiExport, '人员变化分析', values);
}
</script>
<template>
  <Page auto-content-height>
    <!-- <FormDrawer @success="onRefresh" /> -->
    <Grid table-title="人员变化分析列表">
      <template #toolbar-tools>
        <Space>
          <Button
            v-access:code="['employee:changeAnalysis:export']"
            @click="handleDownloadExcel"
          >
            <template #icon>
              <ExportOutlined />
            </template>
            导出
          </Button>
          <!-- <Button
            v-access:code="['hr:changeAnalysis:add']"
            type="primary"
            @click="onCreate"
          >
            <Plus class="size-5" />
            新增
          </Button> -->
        </Space>
      </template>
    </Grid>
  </Page>
</template>
