import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { z } from '#/adapter/form';

// ========== 本地枚举常量 ==========

/** 通知类型：1=通知 2=公告 */
export const NOTICE_TYPE = {
  NOTICE: 1,
  ANNOUNCEMENT: 2,
} as const;

export const NOTICE_TYPE_OPTIONS = [
  { label: '通知', value: 1 },
  { label: '公告', value: 2 },
];

export const NOTICE_TYPE_TAG: Record<number, string> = {
  1: 'blue',
  2: 'orange',
};

/** 通知状态：0=草稿 1=已发布 2=已撤销 */
export const NOTICE_STATUS = {
  DRAFT: 0,
  PUBLISHED: 1,
  REVOKED: 2,
} as const;

export const NOTICE_STATUS_OPTIONS = [
  { label: '草稿', value: 0 },
  { label: '已发布', value: 1 },
  { label: '已撤销', value: 2 },
];

export const NOTICE_STATUS_TAG: Record<number, string> = {
  0: 'default',
  1: 'green',
  2: 'red',
};

/** 发送范围：1=通知所有人 2=指定人 */
export const SEND_SCOPE = {
  ALL: 1,
  SPECIFIC: 2,
} as const;

export const SEND_SCOPE_OPTIONS = [
  { label: '通知所有人', value: 1 },
  { label: '指定人', value: 2 },
];

export const SEND_SCOPE_MAP: Record<number, string> = {
  1: '通知所有人',
  2: '指定人',
};

// ========== 表单 Schema ==========

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      component: 'Input',
      fieldName: 'title',
      label: '标题',
      rules: 'required',
      componentProps: {
        placeholder: '请输入标题',
        maxlength: 50,
        showCount: true,
      },
    },
    {
      fieldName: 'type',
      label: '类型',
      component: 'RadioGroup',
      componentProps: {
        options: NOTICE_TYPE_OPTIONS,
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: 'required',
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      componentProps: {
        options: NOTICE_STATUS_OPTIONS,
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(NOTICE_STATUS.DRAFT),
    },
    {
      fieldName: 'sendScope',
      label: '发送范围',
      component: 'RadioGroup',
      componentProps: {
        options: SEND_SCOPE_OPTIONS,
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: 'required',
    },
    {
      fieldName: 'isTop',
      label: '是否置顶',
      component: 'RadioGroup',
      componentProps: {
        options: [
          { label: '否', value: 0 },
          { label: '是', value: 1 },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(0),
    },
    {
      fieldName: 'content',
      label: '内容',
      component: 'Textarea',
      rules: 'required',
      componentProps: {
        rows: 6,
        placeholder: '请输入内容',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'title',
      label: '标题',
      component: 'Input',
      componentProps: {
        placeholder: '请输入标题',
        allowClear: true,
      },
    },
    {
      fieldName: 'type',
      label: '类型',
      component: 'Select',
      componentProps: {
        placeholder: '请选择类型',
        allowClear: true,
        options: NOTICE_TYPE_OPTIONS,
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        placeholder: '请选择状态',
        allowClear: true,
        options: NOTICE_STATUS_OPTIONS,
      },
    },
    {
      fieldName: 'sendScope',
      label: '发送范围',
      component: 'Select',
      componentProps: {
        placeholder: '请选择发送范围',
        allowClear: true,
        options: SEND_SCOPE_OPTIONS,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'id',
      title: '编号',
      minWidth: 80,
    },
    {
      field: 'isTop',
      title: '置顶',
      minWidth: 70,
      align: 'center',
      slots: { default: 'isTop' },
    },
    {
      field: 'title',
      title: '标题',
      minWidth: 220,
    },
    {
      field: 'type',
      title: '类型',
      minWidth: 80,
      align: 'center',
      slots: { default: 'type' },
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 90,
      align: 'center',
      slots: { default: 'status' },
    },
    {
      field: 'sendScope',
      title: '发送范围',
      minWidth: 110,
      align: 'center',
      slots: { default: 'sendScope' },
    },
    {
      field: 'publishTime',
      title: '发布时间',
      minWidth: 170,
      formatter: 'formatDateTime',
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 170,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
