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

import { getDrugCategoriesList } from '../api';

interface RowType {
  id: number;
  category_name: string;
  zone_id: number;
  zone_name: string;
  level: number;
  parent_id: number;
  sort: number;
  created_at: string;
  updated_at: string;
}

export const gridOptions: VxeGridProps<RowType> = {
  checkboxConfig: {
    highlight: true,
    labelField: '',
  },
  columnConfig: {
    useKey: true,
  },
  rowConfig: {
    useKey: true,
  },
  columns: [
    { type: 'checkbox', width: 60 },
    { width: 60, treeNode: true },
    { field: 'id', align: 'left', title: 'ID', width: 80 },
    { field: 'zone_name', align: 'left', title: '所属专区', width: 120 },
    { field: 'category_name', align: 'left', title: '分类名称' },
    {
      field: 'level',
      title: '层级',
      width: 100,
      formatter: ({ cellValue }) => {
        const levelMap: Record<number, string> = {
          1: '一级分类',
          2: '二级分类',
          3: '三级分类',
        };
        return levelMap[cellValue] || `${cellValue}级`;
      },
    },
    { field: 'sort', title: '排序', width: 80 },
    { field: 'created_at', title: '创建时间', width: 180 },
    { type: 'html', title: '操作', width: 200, slots: { default: 'action' } },
  ],
  keepSource: true,
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues) => {
        return await getDrugCategoriesList({
          page: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
        });
      },
    },
  },
  treeConfig: {
    parentField: 'parent_id',
    rowField: 'id',
    transform: true,
    expandAll: false,
  },
  height: 'auto',
  border: false,
  toolbarConfig: {
    // @ts-ignore
    search: true,
    refresh: true,
    print: false,
    export: false,
    zoom: true,
    slots: {
      buttons: 'toolbar-actions',
    },
    custom: {
      icon: 'vxe-icon-menu',
    },
  },
  showOverflow: false,
};

