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';
import { useI18n } from '/@/hooks/web/useI18n';
const { t } = useI18n();
// const label =[]

export const columns: BasicColumn[] = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: t('routes.system.component'),
    dataIndex: 'path',//component
    width: 80,
  },
  {
    title: t('routes.system.menuName'),
    dataIndex: 'name',
    width: 200,
    align: 'left',
    fixed: 'left',
  },
  // {
  //   title: t('routes.system.menuParentName'),
  //   dataIndex: 'parentName',
  //   width: 100,
  // },
  {
    title: t('routes.system.icon'),
    dataIndex: 'icon',
    width: 50,
    customRender: ({ record }) => {
      return h(Icon, { icon: record.icon });
    },
  },
  // permission
  {
    title: t('routes.system.perms'),
    dataIndex: 'perms',
    width: 250,
  },
  {
    title: t('routes.system.type.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 = t('routes.system.type.0');
          break;
        case 1:
          color = 'warning';
          label = t('routes.system.type.1');
          break;
        case 2:
          color = 'error';
          label = t('routes.system.type.2');
          break;
        default:
          color = 'green';
          label = t('routes.system.type.0');
          break;
      }

      return h(Tag, { color: color }, () => label);
    },
  },
  {
    title: t('routes.system.sortOrder'),
    dataIndex: 'sort',
    width: 50,
  },

  {
    title: t('routes.system.status'),
    dataIndex: 'isShow',
    width: 80,
    customRender: ({ record }) => {
      const isShow = record.isShow;
      const enable = ~~isShow == 1;
      const color = enable ? 'green' : 'red';
      const text = enable ? t('routes.system.enable') : t('routes.system.stopUsing');
      return h(Tag, { color: color }, () => text);
    },
  },
  {
    title: t('routes.system.menuPath'),
    dataIndex: 'path',
    width: 180,
  },
  {
    title: t('routes.system.menuUrl'),
    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: 'id',
    label: 'id',
    show:false,
    component: 'Input',
  },
  {
    field: 'name',
    label: t('routes.system.menuName'),
    component: 'Input',
    componentProps: {
      maxLength: 100,
    },
    colProps: { span: 8 },
  },
  {
    field: 'status',
    label: t('routes.system.status'),
    component: 'Select',
    componentProps: {
      options: [
        { label: t('routes.system.enable'), value: 0 },
        { label: t('routes.system.stopUsing'), value: 1 },
      ],
    },
    colProps: { span: 8 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'type',
    label: t('routes.system.menuType'),
    component: 'RadioButtonGroup',
    defaultValue: 0,

    componentProps: {
      options: [
        { label: t('routes.system.type.0'), value: 0 },
        { label: t('routes.system.type.1'), value: 1 },
        { label: t('routes.system.type.2'), value: 2 },
      ],
    },
    colProps: { lg: 24, md: 24 },
  },
  {
    field: 'name',
    label: t('routes.system.menuName'),
    component: 'Input',
    componentProps: {
      maxLength: 15,
    },
    rules: [
      {
        required: true,
        // @ts-ignore
        validator: async (_, value) => {
          // const reg_tel = /^[a-zA-Z0-9\u4e00-\u9fa5()]+$/;
          // var reg = /\S+@\S+\.\S+/;
          if (!value) {
            return Promise.reject(t('routes.system.menuName1'));
          }
          // if (!reg_tel.test(value)) {
          //   /* eslint-disable-next-line */
          //   return Promise.reject(t('routes.system.menuName2'));
          // }
          return Promise.resolve();
        },
        trigger: 'change',
      },
    ],
  },

  {
    field: 'parentId',
    label: t('routes.system.menuParentName'),
    component: 'TreeSelect',
    componentProps: {
      replaceFields: {
        label: 'name',
        key: 'id',
        value: 'id',
      },
      getPopupContainer: () => document.body,
    },
  },

  {
    field: 'sort',
    label: t('routes.system.sortOrder'),
    defaultValue: 1,
    component: 'InputNumber',
    componentProps: {
      min:0
    },
      required: true,
  },
  {
    field: 'icon',
    label: t('routes.system.icon'),
    component: 'IconPicker',
    // required: true,
    ifShow: ({ values }) => !isButton(values.type),
  },

  {
    field: 'path',
    label: t('routes.system.routerPath'),
    component: 'Input',
    required: true,
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'component',
    label: t('routes.system.component'),
    component: 'Input',
    ifShow: ({ values }) => isMenu(values.type),
  },
  {
    field: 'perms',
    label: t('routes.system.perms'),
    defaultValue: '',
    component: 'Input',
    ifShow: ({ values }) => !isDir(values.type),
  },
  {
    field: 'isShow',
    label: t('routes.system.status'),
    component: 'RadioButtonGroup',
    defaultValue: 1,
    componentProps: {
      options: [
        { label: t('routes.system.enable'), value: 1 },
        { label: t('routes.system.stopUsing'), value: 0 },
      ],
    },
  },
  // {
  //   field: 'isExt',
  //   label: t('routes.system.isExt.title'),
  //   component: 'RadioButtonGroup',
  //   defaultValue: 0,
  //   componentProps: {
  //     options: [
  //       { label: t('routes.system.isExt.1'), value: 1 },
  //       { label: t('routes.system.isExt.0'), value: 0 },
  //     ],
  //   },
  //   ifShow: ({ values }) => !isButton(values.type),
  // },

  // {
  //   field: 'url',
  //   label: t('routes.system.menuUrl'),
  //   component: 'Input',
  //   ifShow: ({ values }) => idShowExt(values.isExt),
  // },

  {
    field: 'hideMenu',
    label: t('routes.system.isShow'),
    component: 'RadioButtonGroup',
    defaultValue: 0,
    componentProps: {
      options: [
        { label: t('routes.system.isExt.0'), value: 1 },
        { label: t('routes.system.isExt.1'), value: 0 },
      ],
    },
    ifShow: ({ values }) => !isButton(values.type),
  },
];
