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

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

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

import { ElButton } from 'element-plus';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { addRecordApi, queryrRecordApilist } from '#/api/schedule/work/record';

import { columns, querySchema } from './config-data';
import CreateRecordDrawer from './CreateRecordDrawer.vue';

// 定义新增抽屉
const [CreateRecordDrawerComp, createRecordDrawerApi] = useVbenDrawer({
  connectedComponent: CreateRecordDrawer,
});

// 新增按钮点击事件
const handleAddClick = () => {
  createRecordDrawerApi.setData({ isUpdate: false }).open();
};

// 重新加载表格数据
const handleReload = async () => {
  if (BasicTableApi) {
    await BasicTableApi.reload();
  }
};
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,
    // 点击行选中
    trigger: 'default',
    checkMethod: ({ row }) => row?.roleId !== 1,
  },
  columns,
  size: 'medium',
  height: 'auto',
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        const resp = await queryrRecordApilist({
          ...formValues,
          pageNum: page.currentPage,
          pageSize: page.pageSize,
        });
        return { items: resp.rows, total: resp.total };
      },
    },
  },
  rowConfig: {
    keyField: 'roleId',
  },
  toolbarConfig: {
    custom: true,
    refresh: true,
    zoom: true,
  },
  id: 'system-role-index',
};
const [BasicTable, BasicTableApi] = useVbenVxeGrid({
  formOptions,
  gridOptions,
});
const handleConfirm = async (data: any) => {
  try {
    await addRecordApi(data);
    createRecordDrawerApi.close();
    handleReload();
  } catch (error) {
    console.error(error);
  }
};
</script>
<template>
  <Page :auto-content-height="true">
    <BasicTable table-title="上班记录">
      <template #toolbar-tools>
        <!-- <ElSpace>
          <ElButton type="primary" @click="handleAddClick"> 新增 </ElButton>
        </ElSpace> -->
      </template>
      <template #action="{ row }">
        <ElButton size="small" type="primary" plain>查看</ElButton>
        <ElButton size="small" type="primary" plain>编辑</ElButton>
      </template>
    </BasicTable>
    <CreateRecordDrawerComp @reload="handleReload" @confirm="handleConfirm" />
  </Page>
</template>
<style scoped lang="scss">
:deep(.el-tooltip__trigger:focus) {
  outline: none;
}
</style>
