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

import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SysLogApi } from '#/api/system/log/sysLog';

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

import { Button } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { apiExport, apiPage } from '#/api/system/log/sysLog';
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-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
  // 日期选择格式化
  fieldMappingTime: [
    [
      'createTime',
      ['beginTime', 'endTime'],
      ['YYYY-MM-DD 00:00:00', 'YYYY-MM-DD 23:59:59'],
    ],
  ],
};

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

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 handlePreview(row: SysLogApi.SysLog) {
  formDrawerApi.setData(row).open();
}
function handleDownloadExcel() {
  commonDownloadExcel(apiExport, '操作日志', gridApi.formApi.form.values, {
    fieldMappingTime: formOptions.fieldMappingTime,
  });
}

// function onDelete(row: SysLogApi.SysLog) {
//   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();
//     });
// }
</script>
<template>
  <Page auto-content-height>
    <FormDrawer @success="onRefresh" />
    <Grid table-title="操作日志列表">
      <template #toolbar-tools>
        <!-- <Button type="primary" @click="onCreate">
          <Plus class="size-5" />
          新增
        </Button> -->
        <Button
          v-access:code="['system:log:export']"
          @click="handleDownloadExcel"
        >
          导出
        </Button>
      </template>
      <template #status="{ row }">
        <div :class="row.status === 1 ? 'dot-before-green' : 'dot-before-red'">
          {{ row.status === 1 ? '成功' : '失败' }}
        </div>
      </template>
      <template #action="{ row }">
        <Button @click.stop="handlePreview(row)"> 预览 </Button>
      </template>
    </Grid>
  </Page>
</template>
