import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';

const TypeEnum = [
  { title: '菜单', value: 'MENU' },
  { title: '目录', value: 'DIR' },
  { title: '按钮', value: 'BUTTON' },
];

export const columns: BasicColumn[] = [
  {
    title: '标题',
    dataIndex: 'title',
    width: 160,
    align: 'left',
  },
  {
    title: '名称',
    dataIndex: 'name',
    width: 220,
    align: 'left',
  },
  {
    title: '路径',
    dataIndex: 'path',
    width: 180,
    align: 'left',
  },
  {
    title: '重定向路径',
    dataIndex: 'redirect',
    width: 180,
    align: 'left',
  },
  // {
  //   title: '图标',
  //   dataIndex: 'icon',
  //   width: 50,
  //   customRender: ({ record }) => {
  //     return h(Icon, { icon: record.icon });
  //   },
  // },
  {
    title: '组件',
    dataIndex: 'component',
    width: 180,
    align: 'left',
  },
  {
    title: '是否缓存',
    dataIndex: 'keepAlive',
    width: 80,
    customRender: ({ text }) => {
      return text ? '是' : '否';
    },
  },
  {
    title: '是否隐藏',
    dataIndex: 'hidden',
    width: 80,
    customRender: ({ text }) => {
      return text ? '是' : '否';
    },
  },
  {
    title: '排序',
    dataIndex: 'sortOrder',
    width: 50,
  },
  {
    title: '类型',
    dataIndex: 'type',
    width: 60,
    customRender: ({ text }) => {
      const option: any = TypeEnum.find((item) => item.value === text);
      return option ? option.title : text;
    },
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 180,
  },
];

const isDir = (type: string) => type === 'DIR';
const isMenu = (type: string) => type === 'MENU';
const isButton = (type: string) => type === 'BUTTON';

export const searchFormSchema: FormSchema[] = [
  {
    field: 'title',
    label: '标题',
    component: 'Input',
    colProps: { span: 8 },
  },
];

const TypeOptions = () => TypeEnum.map((item) => ({ label: item.title, value: item.value }));
export const formSchema: FormSchema[] = [
  {
    field: 'type',
    label: '菜单类型',
    component: 'RadioButtonGroup',
    defaultValue: 'MENU',
    componentProps: {
      options: TypeOptions(),
    },
    colProps: { lg: 24, md: 24 },
  },
  {
    field: 'title',
    label: '标题',
    component: 'Input',
    required: true,
  },
  {
    field: 'name',
    label: '名称',
    component: 'Input',
    required: true,
  },
  {
    field: 'path',
    label: '路径',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'redirect',
    label: '重定向路径',
    component: 'Input',
    required: false,
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'frameSrc',
    label: '内嵌Url',
    component: 'Input',
    required: false,
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'component',
    label: '组件',
    component: 'Input',
    required: false,
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'authorityId',
    label: '上级菜单',
    component: 'TreeSelect',
    componentProps: {
      fieldNames: {
        label: 'title',
        key: 'id',
        value: 'id',
      },
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'sortOrder',
    label: '排序',
    component: 'InputNumber',
    defaultValue: 0,
    required: true,
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'icon',
    label: '图标',
    component: 'IconPicker',
    required: false,
    ifShow: ({ values }) => !isButton(values.type),
  },

  {
    field: 'keepAlive',
    label: '是否缓存',
    component: 'RadioButtonGroup',
    defaultValue: false,
    componentProps: {
      options: [
        { label: '否', value: false },
        { label: '是', value: true },
      ],
    },
    ifShow: ({ values }) => isMenu(values.type),
  },

  {
    field: 'hidden',
    label: '是否隐藏',
    component: 'RadioButtonGroup',
    defaultValue: false,
    componentProps: {
      options: [
        { label: '否', value: false },
        { label: '是', value: true },
      ],
    },
    ifShow: ({ values }) => !isButton(values.type),
  },
];
