<script setup lang="ts">
import type { Recordable } from '@vben/types';

import { nextTick } from 'vue';

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

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

import categoryModal from './category-modal.vue';
import { columns, querySchema } from './data';

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',
};

const gridOptions: VxeGridProps = {
  columns,
  height: 'auto',
  keepSource: true,
  pagerConfig: {
    enabled: false,
  },
  proxyConfig: {
    ajax: {
      query: async (_, formValues = {}) => {
        const resp = await categoryList({
          ...formValues,
        });
        return { rows: resp };
      },
      // 默认请求接口后展开全部 不需要可以删除这段
      querySuccess: () => {
        nextTick(() => {
          expandAll();
        });
      },
    },
  },
  /**
   * 虚拟滚动  默认关闭
   */
  scrollY: {
    enabled: false,
    gt: 0,
  },
  rowConfig: {
    keyField: 'categoryId',
  },
  treeConfig: {
    parentField: 'parentId',
    rowField: 'categoryId',
    transform: true,
  },
  // 表格全局唯一表示 保存列配置需要用到
  id: 'workflow-category-index',
};

const [BasicTable, tableApi] = useVbenVxeGrid({ formOptions, gridOptions });
const [CategoryDrawer, drawerApi] = useVbenDrawer({
  connectedComponent: categoryModal,
});

function handleAdd(row?: Recordable<any>) {
  drawerApi.setData({ parentId: row?.categoryId });
  drawerApi.open();
}

async function handleEdit(row: Recordable<any>) {
  drawerApi.setData({ id: row.categoryId });
  drawerApi.open();
}

async function handleDelete(row: Recordable<any>) {
  await categoryRemove(row.categoryId);
  await tableApi.query();
}

function expandAll() {
  tableApi.grid?.setAllTreeExpand(true);
}

function collapseAll() {
  tableApi.grid?.setAllTreeExpand(false);
}
</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="handleAdd"
          >
            {{ $t('pages.common.add') }}
          </el-button>
        </ElSpace>
      </template>
      <template #action="{ row }">
        <ElSpace>
          <el-button
            type="default"
            plain
            v-access:code="['workflow:category:edit']"
            @click.stop="handleEdit(row)"
          >
            {{ $t('pages.common.edit') }}
          </el-button>
          <el-button
            type="success"
            plain
            v-access:code="['workflow:category:edit']"
            @click.stop="handleAdd(row)"
          >
            {{ $t('pages.common.add') }}
          </el-button>
          <ElPopconfirm
            :teleported="false"
            placement="left"
            title="确认删除？"
            @confirm="handleDelete(row)"
          >
            <template #reference>
              <el-button
                type="danger"
                plain
                v-access:code="['workflow:category:remove']"
              >
                {{ $t('pages.common.delete') }}
              </el-button>
            </template>
          </ElPopconfirm>
        </ElSpace>
      </template>
    </BasicTable>
    <CategoryDrawer @reload="tableApi.query()" />
  </Page>
</template>
