import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Tag } from 'ant-design-vue';
import { Icon } from '/@/components/Icon';

// const label =[]

export const columns: BasicColumn[] = [
  {
    title: 'ID',
    dataIndex: 'menuId',
    width: 80,
  },
  {
    title: '组件路径',
    dataIndex: 'component',
    width: 80,
  },
  {
    title: '菜单名称',
    dataIndex: 'name',
    width: 200,
    align: 'left',
    fixed: 'left',
  },
  {
    title: '上级菜单',
    dataIndex: 'parentName',
    width: 100,
  },
  {
    title: '图标',
    dataIndex: 'icon',
    width: 50,
    customRender: ({ record }) => {
      return h(Icon, { icon: record.icon });
    },
  },
  // permission
  {
    title: '权限标识',
    dataIndex: 'perms',
    width: 250,
  },
  {
    title: '类型',
    dataIndex: 'type',
    width: 100,
    customRender: ({ record }) => {
      let color: string;
      let label: string;
      const type: number = record.type;
      switch (type) {
        case 0:
          color = 'processing';
          label = '目录';
          break;
        case 1:
          color = 'warning';
          label = '菜单';
          break;
        case 2:
          color = 'error';
          label = '按钮';
          break;
        default:
          color = 'green';
          label = '目录';
          break;
      }

      return h(Tag, { color: color }, () => label);
    },
  },
  {
    title: '排序',
    dataIndex: 'orderNum',
    width: 50,
  },

  {
    title: '状态',
    dataIndex: 'status',
    width: 80,
    customRender: ({ record }) => {
      const status = record.status;
      const enable = ~~status === 0;
      const color = enable ? 'green' : 'red';
      const text = enable ? '启用' : '停用';
      return h(Tag, { color: color }, () => text);
    },
  },
  {
    title: '菜单路径',
    dataIndex: 'path',
    width: 180,
  },
  {
    title: '外链地址',
    dataIndex: 'url',
    width: 180,
  },
];

const isDir = (type: number) => type === 0;
const isMenu = (type: number) => type === 1;
const isButton = (type: number) => type === 2;
const idShowExt = (isExt: number) => isExt === 1;

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '菜单名称',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '启用', value: 0 },
        { label: '停用', value: 1 },
      ],
    },
    colProps: { span: 8 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'type',
    label: '菜单类型',
    component: 'RadioButtonGroup',
    defaultValue: 0,

    componentProps: {
      options: [
        { label: '目录', value: 0 },
        { label: '菜单', value: 1 },
        { label: '按钮', value: 2 },
      ],
    },
    colProps: { lg: 24, md: 24 },
  },
  {
    field: 'name',
    label: '菜单名称',
    component: 'Input',
    required: true,
  },

  {
    field: 'parentId',
    label: '上级菜单',
    component: 'TreeSelect',
    componentProps: {
      fieldNames: {
        label: 'name',
        key: 'menuId',
        value: 'menuId',
      },
      getPopupContainer: () => document.body,
    },
  },

  {
    field: 'orderNum',
    label: '排序',
    defaultValue: 0,
    component: 'InputNumber',
    required: true,
  },
  {
    field: 'icon',
    label: '图标',
    component: 'IconPicker',
    required: true,
    ifShow: ({ values }) => !isButton(values.type),
  },

  {
    field: 'path',
    label: '路由地址',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'component',
    label: '组件路径',
    component: 'Input',
    ifShow: ({ values }) => isMenu(values.type),
  },
  {
    field: 'perms',
    label: '权限标识',
    defaultValue: '',
    component: 'Input',
    ifShow: ({ values }) => !isDir(values.type),
  },
  {
    field: 'status',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '启用', value: 0 },
        { label: '禁用', value: 1 },
      ],
    },
  },
  {
    field: 'isExt',
    label: '是否外链',
    component: 'RadioButtonGroup',
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
    ifShow: ({ values }) => !isButton(values.type),
  },

  {
    field: 'url',
    label: '外链地址',
    component: 'Input',
    ifShow: ({ values }) => idShowExt(values.isExt),
  },

  {
    field: 'hideMenu',
    label: '是否显示',
    component: 'RadioButtonGroup',
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '是', value: 0 },
        { label: '否', value: 1 },
      ],
    },
    ifShow: ({ values }) => !isButton(values.type),
  },
];
