<script lang="ts" setup>
import type {
  OnActionClickParams,
  VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { UserApi } from '#/api';

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

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

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

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

// const selectedRowIds = ref<number[]>([]);

const [FormModal, formModalApi] = useVbenModal({
  connectedComponent: Form,
  destroyOnClose: true,
});

/**
 * 编辑工作流
 * @param row
 */
function onEdit(row: any) {
  formModalApi.setData(row).open();
}

/**
 * 表格操作按钮的回调函数
 */
function onActionClick({ code, row }: OnActionClickParams<any>) {
  switch (code) {
    case 'edit': {
      onEdit(row);
      break;
    }
  }
}

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions: {
    schema: useGridFormSchema(),
  },
  gridOptions: {
    columns: useColumns(onActionClick),
    height: 'auto',
    keepSource: true,
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          const res = await getAppList({
            page: page.currentPage,
            size: page.pageSize,
            current: page.currentPage,
            ...formValues,
          });
          return {
            items: res,
            total: res.length,
          };
        },
      },
    },
    rowConfig: {
      keyField: 'id',
    },

    toolbarConfig: {
      custom: true,
      export: false,
      refresh: { code: 'query' },
      search: true,
      zoom: true,
    },
  } as VxeTableGridOptions<UserApi.User>,
});

function onRefresh() {
  gridApi.query();
}
</script>
<template>
  <Page auto-content-height>
    <FormDrawer />
    <FormModal @success="onRefresh" />
    <Grid table-title="工作流列表">
      <!--      <template #toolbar-tools>-->
      <!--        <Button type="primary" @click="onDelete" danger> 批量删除 </Button>-->
      <!--      </template>-->
      <!--      <template #makeTime="{ row }">-->
      <!--        {{ dayjs(row.makeTime).format('YYYY-MM-DD HH:mm') }}-->
      <!--      </template>-->
    </Grid>
  </Page>
</template>
