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

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

interface RowType {
  id: number;
  name: string;
  category_id: number;
  category_name: string;
  cover_image: string;
  tags: string[];
  price_per_dose: number;
  sales_count: number;
  sort: number;
  status: number;
  status_txt: string;
  prescription_sub_type: number;
  created_at: string;
}

const prescriptionTypeMap: Record<number, string> = {
  1: '中药',
  2: '西药',
  3: '颗粒药',
};

export const gridOptions: VxeGridProps<RowType> = {
  checkboxConfig: {
    highlight: true,
    labelField: '',
  },
  columnConfig: {
    useKey: true,
  },
  rowConfig: {
    useKey: true,
    isHover: true,
    isCurrent: true,
  },
  columns: [
    { type: 'checkbox', width: 60 },
    { field: 'id', align: 'left', title: 'ID', width: 80 },
    { field: 'name', align: 'left', title: '名称', minWidth: 160 },
    { field: 'category_name', align: 'left', title: '分类', width: 120 },
    {
      field: 'cover_image',
      align: 'left',
      title: '封面',
      width: 100,
      slots: { default: 'cover' },
    },
    {
      field: 'tags',
      align: 'left',
      title: '标签',
      minWidth: 160,
      slots: { default: 'tags' },
    },
    {
      field: 'prescription_sub_type',
      align: 'left',
      title: '处方类型',
      width: 100,
      formatter: ({ cellValue }) => prescriptionTypeMap[cellValue] || '-',
    },
    { field: 'price_per_dose', align: 'left', title: '每剂价格', width: 100 },
    { field: 'sales_count', align: 'left', title: '销量', width: 80 },
    { field: 'sort', align: 'left', title: '排序', width: 80 },
    {
      field: 'status',
      align: 'left',
      title: '状态',
      width: 90,
      slots: { default: 'status' },
    },
    { field: 'created_at', title: '创建时间', width: 180 },
    { type: 'html', title: '操作', slots: { default: 'action' }, width: 220 },
  ],
  keepSource: true,
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues) => {
        return await getSpecialPrescriptionList({
          page: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
        });
      },
    },
  },
  border: false,
  toolbarConfig: {
    search: true,
    refresh: true,
    print: false,
    export: false,
    zoom: true,
    slots: {
      buttons: 'toolbar-buttons',
    },
    custom: {
      icon: 'vxe-icon-menu',
    },
  },
  showOverflow: false,
};
