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

import type {
  OnActionClickParams,
  VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { SysMenuApi } from '#/api/auth/menu';

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

import { Button, message } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { deleteOne, findList } from '#/api/auth/menu';

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

const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 80,
    componentProps: {
      allowClear: true,
    },
  },
  schema: querySchema,
  submitButtonOptions: {
    content: '查询',
  },
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
};

const [FormDrawer, formDrawerApi] = useVbenDrawer({
  connectedComponent: Form,
  // destroyOnClose: true,
});

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions,
  gridOptions: {
    columns: useColumns(onActionClick),
    height: 'auto',
    keepSource: true,
    pagerConfig: {
      enabled: false,
    },
    proxyConfig: {
      ajax: {
        query: async (_, formValues = {}) => {
          const data = await findList({ ...formValues });
          return { data };
        },
      },
    },
    rowConfig: {
      keyField: 'id',
      // 高亮点击行
      isCurrent: true,
    },
    treeConfig: {
      parentField: 'parentId',
      rowField: 'id',
      transform: false,
    },
  } as VxeTableGridOptions,
});

function onActionClick({ code, row }: OnActionClickParams<SysMenuApi.menu>) {
  switch (code) {
    case 'append': {
      onAppend(row);
      break;
    }
    case 'delete': {
      onDelete(row);
      break;
    }
    case 'edit': {
      onEdit(row);
      break;
    }
    default: {
      break;
    }
  }
}

function onRefresh() {
  gridApi.query();
}
function onEdit(row: SysMenuApi.menu) {
  formDrawerApi.setData(row).open();
}
function onCreate() {
  formDrawerApi.setData({}).open();
}
function onAppend(row: SysMenuApi.menu) {
  formDrawerApi.setData({ parentId: row.id }).open();
}

function onDelete(row: SysMenuApi.menu) {
  const hideLoading = message.loading({
    content: `正在删除${row.name}...`,
    duration: 0,
    key: 'action_process_msg',
  });
  deleteOne(row.id || 0)
    .then(() => {
      message.success({
        content: `${row.name}删除成功`,
        key: 'action_process_msg',
      });
      onRefresh();
    })
    .catch(() => {
      hideLoading();
    });
}
</script>
<template>
  <Page auto-content-height>
    <FormDrawer @success="onRefresh" />
    <Grid
      id="system-menu-table"
      table-title="菜单列表"
      table-title-help="双击展开/收起子菜单"
    >
      <template #toolbar-tools>
        <Button
          v-access:code="['auth:dept:add']"
          type="primary"
          @click="onCreate"
        >
          <Plus class="size-5" />
          新增菜单
        </Button>
      </template>
    </Grid>
  </Page>
</template>

<style lang="scss">
#system-menu-table > .vxe-grid {
  --vxe-ui-table-row-current-background-color: hsl(var(--primary-100));

  html.dark & {
    --vxe-ui-table-row-current-background-color: hsl(var(--primary-800));
  }
}
</style>
