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

import { nextTick } from 'vue';

import { Page, useVbenDrawer } from '@vben/common-ui';
import { ElPopconfirm, ElSpace } from 'element-plus';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { categoryList, categoryRemove } from '#/api/workflow/category';

import CategoryDrawer from './category-drawer.vue';
import { queryFormSchema, tableColumns } from './config-data';

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

const gridOptions: VxeGridProps = {
  // 配置属性结构
  treeConfig: {
    transform: true,
    rowField: 'categoryId',
    parentField: 'parentId',
  },
  // 配置字体大小
  size: 'medium',
  // 配置表格右上角全屏、刷新
  toolbarConfig: {
    custom: true,
    refresh: true,
    zoom: true,
  },
  // 关闭分页
  pagerConfig: {
    enabled: false,
  },
  // 获取表格数据
  proxyConfig: {
    ajax: {
      query: async (_, formValues = {}) => {
        const resp = await categoryList({
          ...formValues,
        });
        //  return { items: resp.rows, total: resp.total };
         return { items: resp };
      },
      querySuccess: () => {
        nextTick(() => {
          basicTableApi.grid?.setAllTreeExpand(true);
        });
      },
    },
  },
  // 配置显示列
  columns: tableColumns,
};

const [BasicTable, basicTableApi] = useVbenVxeGrid({
  formOptions,
  gridOptions,
});
const [CategoryVbenDrawer, categoryVbenDrawerApi] = useVbenDrawer({
  connectedComponent: CategoryDrawer,
});

// 编辑分类
function handleEdit(row: any) {
  categoryVbenDrawerApi.setData({ ...row, isUpdate: true }).open();
}

// 新增分类
function handleAdd(row?: any) {
  // 当点击新增按钮时，如果传入了row，则将当前行的categoryId作为新分类的parentId
  const parentId = row?.categoryId;
  categoryVbenDrawerApi.setData({ parentId, isUpdate: false }).open();
}

// 删除分类
async function confirmEvent(row: any) {
  await categoryRemove(row.categoryId);
  await basicTableApi.reload();
}

// 展开
const expandAll = () => {
  basicTableApi.grid?.setAllTreeExpand(true);
};

// 折叠
const collapseAll = () => {
  basicTableApi.grid?.setAllTreeExpand(false);
};

// 添加分类
const addCategory = () => {
  categoryVbenDrawerApi.setData({ isUpdate: false }).open();
};
</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable table-title="流程分类列表">
      <template #toolbar-tools>
        <ElSpace>
          <el-button @click="collapseAll">
            {{ $t('pages.common.collapse') }}
          </el-button>
          <el-button @click="expandAll">
            {{ $t('pages.common.expand') }}
          </el-button>
          <el-button
            type="primary"
            v-access:code="['workflow:category:add']"
            @click="addCategory"
          >
            {{ $t('pages.common.add') }}
          </el-button>
        </ElSpace>
      </template>
      <template #action="{ row }">
        <ElSpace>
          <el-button
            size="small"
            type="primary"
            plain
            @click="handleEdit(row)"
            v-access:code="['workflow:category:edit']"
          >
            编辑
          </el-button>
          <el-button
            size="small"
            type="success"
            plain
            @click="handleAdd(row)"
            v-access:code="['workflow:category:add']"
          >
            新增
          </el-button>
          <ElPopconfirm
            :teleported="false"
            placement="left"
            title="确认删除？"
            @confirm="confirmEvent(row)"
            v-if="row.parentId"
          >
            <template #reference>
              <el-button
                size="small"
                type="danger"
                plain
                v-access:code="['workflow:category:remove']"
              >
                删除
              </el-button>
            </template>
          </ElPopconfirm>
        </ElSpace>
      </template>
    </BasicTable>
    <CategoryVbenDrawer @reload="basicTableApi.reload()" />
  </Page>
</template>

