import type { NamePath, RuleObject } from 'ant-design-vue/lib/form/interface';
import type { VNode } from 'vue';
import type { ButtonProps as AntdButtonProps } from '/@/components/Button';
import type { FormItem } from './formItem';
import type { ColEx, ComponentType } from './index';
import type { TableActionType } from '/@/components/Table/src/types/table';
import type { CSSProperties } from 'vue';
import type { RowProps } from 'ant-design-vue/lib/grid/Row';

export type FieldMapToTime = [string, [string, string], string?][];

export type Rule = RuleObject & {
  trigger?: 'blur' | 'change' | ['change', 'blur'];
};

export interface RenderCallbackParams {
  schema: FormSchema;
  values: Recordable;
  model: Recordable;
  field: string;
}

export interface ButtonProps extends AntdButtonProps {
  text?: string;
}

export interface FormActionType {
  submit: () => Promise<void>;
  setFieldsValue: <T>(values: T) => Promise<void>;
  resetFields: () => Promise<void>;
  getFieldsValue: () => Recordable;
  clearValidate: (name?: string | string[]) => Promise<void>;
  updateSchema: (data: Partial<FormSchema> | Partial<FormSchema>[]) => Promise<void>;
  resetSchema: (data: Partial<FormSchema> | Partial<FormSchema>[]) => Promise<void>;
  setProps: (formProps: Partial<FormProps>) => Promise<void>;
  removeSchemaByFiled: (field: string | string[]) => Promise<void>;
  appendSchemaByField: (
    schema: FormSchema,
    prefixField: string | undefined,
    first?: boolean | undefined
  ) => Promise<void>;
  validateFields: (nameList?: NamePath[]) => Promise<any>;
  validate: (nameList?: NamePath[]) => Promise<any>;
  scrollToField: (name: NamePath, options?: ScrollOptions) => Promise<void>;
}

export type RegisterFn = (formInstance: FormActionType) => void;

export type UseFormReturnType = [RegisterFn, FormActionType];
export interface FormProps {
  layout?: 'vertical' | 'inline' | 'horizontal';  // 表单布局方式，可选值为 'vertical', 'inline', 'horizontal'
  model?: Recordable;  // 表单的数值模型
  labelWidth?: number | string;  // 整个表单中所有项的标签宽度
  labelAlign?: 'left' | 'right';  // 标签的对齐方式，可选值为 'left', 'right'
  rowProps?: RowProps;  // 整个表单的行配置
  submitOnReset?: boolean;  // 重置表单时是否提交表单
  labelCol?: Partial<ColEx>;  // 整个表单的标签列配置
  wrapperCol?: Partial<ColEx>;  // 整个表单的包裹列配置
  baseRowStyle?: CSSProperties;  // 通用行样式
  baseColProps?: Partial<ColEx>;  // 通用列配置
  schemas?: FormSchema[];  // 表单配置规则
  mergeDynamicData?: Recordable;  // 使用于动态控制的函数值合并到表单项中
  compact?: boolean;  // 是否为紧凑模式（搜索表单常用）
  emptySpan?: number | Partial<ColEx>;  // 空白行占位的跨列数
  size?: 'default' | 'small' | 'large';  // 表单控件的尺寸
  disabled?: boolean;  // 是否禁用整个表单
  fieldMapToTime?: FieldMapToTime;  // 时间间隔字段映射为多个字段
  autoSetPlaceHolder?: boolean;  // 是否自动设置占位符
  autoSubmitOnEnter?: boolean;  // 在输入框上按回车键时是否自动提交表单
  rulesMessageJoinLabel?: boolean;  // 是否将提示信息添加到标签中显示
  showAdvancedButton?: boolean;  // 是否显示展开/收起按钮
  autoFocusFirstItem?: boolean;  // 是否自动聚焦到第一个输入框，仅在第一项为输入框时有效
  autoAdvancedLine?: number;  // 超过指定行数时自动折叠
  alwaysShowLines?: number;  // 总是显示的行数
  showActionButtonGroup?: boolean;  // 是否显示操作按钮

  resetButtonOptions?: Partial<ButtonProps>;  // 重置按钮的配置
  submitButtonOptions?: Partial<ButtonProps>;  // 确认按钮的配置
  actionColOptions?: Partial<ColEx>;  // 操作列的配置

  showResetButton?: boolean;  // 是否显示重置按钮
  showSubmitButton?: boolean;  // 

  resetFunc?: () => Promise<void>;
  submitFunc?: () => Promise<void>;
  transformDateFunc?: (date: any) => string;
  colon?: boolean;
}
export interface FormSchema {
  // 表单字段名称
  field: string;
  // 触发内部值改变的事件名称，默认为 change
  changeEvent?: string;
  // 绑定到 v-model 的变量名称，默认值
  valueField?: string;
  // 标签名称
  label: string;
  // 辅助文本
  subLabel?: string;
  // 文本右侧的帮助信息
  helpMessage?:
    | string
    | string[]
    | ((renderCallbackParams: RenderCallbackParams) => string | string[]);
  // BaseHelp 组件属性
  helpComponentProps?: Partial<HelpComponentProps>;
  // 标签宽度，如果传递了，itemProps 配置的 labelCol 和 wrapperCol 将无效
  labelWidth?: string | number;
  // 禁用使用 formModel 的全局设置来调整 labelWidth，并手动设置 labelCol 和 wrapperCol
  disabledLabelWidth?: boolean;
  // 渲染的组件
  component: ComponentType;
  // 组件参数
  componentProps?:
    | ((opt: {
        schema: FormSchema;
        tableAction: TableActionType;
        formActionType: FormActionType;
        formModel: Recordable;
      }) => Recordable)
    | object;
  // 是否必填
  required?: boolean | ((renderCallbackParams: RenderCallbackParams) => boolean);

  suffix?: string | number | ((values: RenderCallbackParams) => string | number);

  // 验证规则
  rules?: Rule[];
  // 检查是否将信息添加到标签中
  rulesMessageJoinLabel?: boolean;

  // 引用 formModelItem
  itemProps?: Partial<FormItem>;

  // formModelItem 外的 col 配置
  colProps?: Partial<ColEx>;

  // 默认值
  defaultValue?: any;
  isAdvanced?: boolean;

  // 匹配详情组件
  span?: number;

  ifShow?: boolean | ((renderCallbackParams: RenderCallbackParams) => boolean);

  show?: boolean | ((renderCallbackParams: RenderCallbackParams) => boolean);

  // 渲染 form-item 标签中的内容
  render?: (renderCallbackParams: RenderCallbackParams) => VNode | VNode[] | string;

  // 渲染 col 内容需要外部包裹 form-item
  renderColContent?: (renderCallbackParams: RenderCallbackParams) => VNode | VNode[] | string;

  renderComponentContent?:
    | ((renderCallbackParams: RenderCallbackParams) => any)
    | VNode
    | VNode[]
    | string;

  // 自定义插槽，在 from-item 中
  slot?: string;

  // 自定义插槽，类似于 renderColContent
  colSlot?: string;

  dynamicDisabled?: boolean | ((renderCallbackParams: RenderCallbackParams) => boolean);

  dynamicRules?: (renderCallbackParams: RenderCallbackParams) => Rule[];
}
export interface HelpComponentProps {
  maxWidth: string;
  // Whether to display the serial number
  showIndex: boolean;
  // Text list
  text: any;
  // colour
  color: string;
  // font size
  fontSize: string;
  icon: string;
  absolute: boolean;
  // Positioning
  position: any;
}
