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

import type { TaskPageProps } from './types';

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

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

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

import { ApprovalPanelDrawerComp } from '..';
import { columns, querySchema } from './data';

const props = defineProps<TaskPageProps>();

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',
      ['params[beginTime]', 'params[endTime]'],
      ['YYYY-MM-DD 00:00:00', 'YYYY-MM-DD 23:59:59'],
    ],
  ],
};

const gridOptions: VxeGridProps = {
  checkboxConfig: {
    // 高亮
    highlight: true,
    // 翻页时保留选中状态
    reserve: true,
  },
  columns,
  height: 'auto',
  keepSource: true,
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        return await props.listApi({
          pageNum: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
        });
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  /**
   * TODO: id
   */
  id: 'workflow-task',
  rowClassName: 'cursor-pointer',
};

const [BasicTable, tableApi] = useVbenVxeGrid({
  formOptions,
  gridOptions,
  gridEvents: {
    cellClick: ({ row }) => {
      handleOpen(row);
    },
  },
});

const [ApprovalPanelDrawer, drawerApi] = useVbenDrawer({
  connectedComponent: ApprovalPanelDrawerComp,
});

function handleOpen(row: any) {
  drawerApi.setData({ task: row, type: props.type }).open();
}
</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable :table-title="tableTitle" />
    <ApprovalPanelDrawer @reload="() => tableApi.query()" />
  </Page>
</template>
