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

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

import { onMounted, ref } from 'vue';

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

import { ElMessageBox } from 'element-plus';

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

import { queryFormSchema, tableColumns, formActions, mockData } from './taskTemplate-data';
import taskTemplateDrawerComp from './taskTemplate-drawer.vue';

const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 80,
    componentProps: {
      allowClear: true,
    },
  },
  schema: queryFormSchema(),
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-4',
  formActions,
};

const gridOptions: VxeGridProps = {
  checkboxConfig: {
    highlight: true,
    reserve: true,
    trigger: 'default',
  },
  columns: tableColumns,
  size: 'medium',
  height: 'auto',
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        // 模拟API请求
        console.log('查询参数:', formValues, page);
        // 模拟过滤
        let filteredRows = [...mockData];
        
        // 提取通用过滤逻辑
        const filterFields = [
          { key: 'taskName', type: 'include' },
          { key: 'taskType', type: 'exact' },
          { key: 'taskResponsible', type: 'include' },
          { key: 'executivePost', type: 'exact' },
          { key: 'taskFrequency', type: 'exact' },
          { key: 'status', type: 'exact' },
          { key: 'taskLevel', type: 'exact' },
          { key: 'formType', type: 'exact' },
          { key: 'timedDistribution', type: 'exact' },
          { key: 'taskSource', type: 'exact' },
        ];
        
        filterFields.forEach(({ key, type }) => {
          if (formValues[key]) {
            filteredRows = filteredRows.filter(row => {
              if (type === 'exact') {
                return row[key] === formValues[key];
              } else if (type === 'include') {
                return row[key]?.includes(formValues[key]) || false;
              }
              return true;
            });
          }
        });
        
        // 模拟分页
        const start = (page.currentPage - 1) * page.pageSize;
        const end = start + page.pageSize;
        const paginatedRows = filteredRows.slice(start, end);
        return { 
          items: paginatedRows, 
          total: filteredRows.length 
        };
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  toolbarConfig: {
    custom: true,
    refresh: true,
    zoom: true,
  },
  id: 'system-checklis-taskTemplate-index',
};

const [BasicTable, basicTableApi] = useVbenVxeGrid({
  formOptions,
  gridOptions,
});

const [taskTemplateDrawer, drawerApi] = useVbenDrawer({
  connectedComponent: taskTemplateDrawerComp,
  // 设置抽屉宽度为700px，使用class属性覆盖默认宽度
  class: 'w-[850px]',
});

// 新增任务
function openDrawer() {
  drawerApi.setData({ isUpdate: false }).open();
}

// 查看任务
function handleView(row: any) {
  drawerApi.setData({ ...row, isView: true }).open();
}

// 编辑任务
function handleEdit(row: any) {
  drawerApi.setData({ ...row, isUpdate: true }).open();
}

// 复制任务
function handleCopy(row: any) {
  drawerApi.setData({ ...row, isCopy: true, id: '' }).open();
}

// 分发任务
function handleDistribute(row: any) {
  console.log('分发任务:', row.id);
  ElMessageBox.alert('任务分发功能待实现', '提示');
}

// 导出任务模板
function handleExport() {
  console.log('导出任务模板');
  ElMessageBox.alert('任务模板导出功能待实现', '提示');
}
</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable table-title="任务模板列表">
      <template #toolbar-tools>
        <ElSpace>
          <ElButton
            type="primary"
            @click="openDrawer"
          >
            新增任务
          </ElButton>
          <ElButton
            type="primary"
            ghost
            @click="handleExport"
          >
            导出
          </ElButton>
        </ElSpace>
      </template>
      <template #action="{ row }">
        <ElSpace>
          <ElButton
            size="small"
            type="primary"
            plain
            @click="handleView(row)"
          >
            查看
          </ElButton>
          <ElButton
            size="small"
            type="primary"
            plain
            @click="handleEdit(row)"
          >
            编辑
          </ElButton>
          <ElButton
            size="small"
            type="primary"
            plain
            @click="handleCopy(row)"
          >
            复制
          </ElButton>
          <ElButton
            size="small"
            type="primary"
            plain
            @click="handleDistribute(row)"
          >
            分发
          </ElButton>
        </ElSpace>
      </template>
    </BasicTable>
    <taskTemplateDrawer @reload="basicTableApi.reload()" />
  </Page>
</template>

<style scoped lang="scss">
:deep(.el-tooltip__trigger:focus) {
  outline: none;
}
</style>
