import type { VbenFormProps } from '#/adapter/form';

import { REMIND_CHANNEL, REMIND_CHANNEL_OPTIONS } from './constants';

/**
 * 待办新增/编辑弹窗表单配置
 * 提醒相关字段级联显隐：设置了提醒时间才显示渠道选择，勾选邮箱/短信才显示对应收件人输入框
 */
export const todoModalFormProps: VbenFormProps = {
  commonConfig: {
    labelWidth: 90,
  },
  layout: 'vertical',
  schema: [
    {
      component: 'VbenInput',
      fieldName: 'id',
      label: 'ID',
      dependencies: {
        show: false,
        triggerFields: ['id'],
      },
    },
    {
      component: 'VbenInput',
      componentProps: {
        placeholder: '请输入待办标题',
        maxlength: 200,
      },
      fieldName: 'title',
      label: '待办标题',
      rules: 'required',
    },
    {
      component: 'DatePicker',
      componentProps: {
        class: 'w-full',
        format: 'YYYY-MM-DD HH:mm',
        valueFormat: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
        placeholder: '请选择待办时间',
      },
      fieldName: 'todo_time',
      label: '待办时间',
      rules: 'selectRequired',
    },
    {
      // Markdown 编辑器（自动注册的表单组件），内容以 MD 源码入库，站内信/邮件提醒按 MD 渲染
      component: 'MarkdownEditor',
      componentProps: {
        placeholder: '详情备注（选填，支持 Markdown 格式）',
        height: 220,
      },
      fieldName: 'content',
      label: '详情备注',
    },
    {
      component: 'DatePicker',
      componentProps: {
        class: 'w-full',
        format: 'YYYY-MM-DD HH:mm',
        valueFormat: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
        placeholder: '不选则不提醒',
        allowClear: true,
      },
      fieldName: 'remind_at',
      help: '到达提醒时间后按所选渠道自动发送提醒，需晚于当前时间',
      label: '提醒时间',
    },
    {
      component: 'CheckboxGroup',
      componentProps: {
        options: REMIND_CHANNEL_OPTIONS,
      },
      // 只有设置了提醒时间才需要选择渠道
      dependencies: {
        show: (values) => !!values.remind_at,
        rules: (values) => (values.remind_at ? 'required' : null),
        triggerFields: ['remind_at'],
      },
      fieldName: 'remind_channels',
      label: '提醒方式',
    },
    {
      component: 'VbenInput',
      componentProps: {
        placeholder: '不填则使用个人资料中的邮箱',
      },
      dependencies: {
        show: (values) =>
          !!values.remind_at &&
          Array.isArray(values.remind_channels) &&
          values.remind_channels.includes(REMIND_CHANNEL.EMAIL),
        triggerFields: ['remind_at', 'remind_channels'],
      },
      fieldName: 'remind_email',
      label: '提醒邮箱',
    },
    {
      component: 'VbenInput',
      componentProps: {
        placeholder: '不填则使用个人资料中的手机号',
        maxlength: 11,
      },
      dependencies: {
        show: (values) =>
          !!values.remind_at &&
          Array.isArray(values.remind_channels) &&
          values.remind_channels.includes(REMIND_CHANNEL.SMS),
        triggerFields: ['remind_at', 'remind_channels'],
      },
      fieldName: 'remind_phone',
      label: '提醒手机号',
    },
  ],
  showDefaultActions: false,
};
