import { request } from '../../utils/axios';

// 邮件提醒周期配置 VO 类型
export interface MailNotifyConfigVO {
  id?: number;
  configId?: number;
  configName?: string;
  configCode?: string;
  name?: string; // 提醒事项
  templateCode?: string; // 邮件模板编码
  reminderTime?: number; // 提醒时长（秒）
  notifyCycle?: string;
  notifyTime?: string;
  notifyType?: string;
  status?: string;
  remark?: string;
  createTime?: Date;
}

// 分页参数类型
export interface PageParam {
  pageNo: number;
  pageSize: number;
  configName?: string;
  configCode?: string;
  name?: string; // 提醒事项
  templateName?: string; // 邮件模板名称
  status?: string;
}

// 分页响应类型
export interface PageResponse<T> {
  list: T[];
  total: number;
  records?: T[]; // 兼容其他响应格式
  data?: {
    list: T[];
    total: number;
  };
}

// 邮件提醒周期配置 API
export const mailNotifyConfigApi = {
  // 查看系统邮件提醒周期配置-分页
  listIsMailNotifyConfigPage: async (params: PageParam): Promise<PageResponse<MailNotifyConfigVO>> => {
    const response: any = await request.get({
      url: '/iscs/mail-notify-config/getMailNotifyConfigPage',
      params,
    });
    
    // 处理响应数据格式
    if (response && typeof response === 'object') {
      if ('data' in response && response.data) {
        return response.data;
      } else if ('list' in response || 'records' in response) {
        return {
          list: response.list || response.records || [],
          total: response.total || 0,
        };
      }
    }
    return { list: [], total: 0 };
  },

  // 获取系统邮件提醒周期配置详细信息（GET /admin-api/iscs/mail-notify-config/selectMailNotifyConfigById?id=4，兼容 { code, data } 与直接 data）
  getIsMailNotifyConfigById: async (id: number): Promise<MailNotifyConfigVO> => {
    const response: any = await request.get({
      url: '/iscs/mail-notify-config/selectMailNotifyConfigById',
      params: { id },
    });
    const raw = response?.data ?? response;
    return raw?.data ?? raw;
  },

  // 新增系统邮件提醒周期配置（不返回响应体，避免被误当作成功提示内容展示）
  addIsMailNotifyConfig: async (data: MailNotifyConfigVO): Promise<void> => {
    await request.post({
      url: '/iscs/mail-notify-config/insertMailNotifyConfig',
      data,
    });
    return;
  },

  // 修改系统邮件提醒周期配置
  updateIsMailNotifyConfig: async (data: MailNotifyConfigVO): Promise<void> => {
    await request.put({
      url: '/iscs/mail-notify-config/updateMailNotifyConfig',
      data,
    });
  },

  // 删除系统邮件提醒周期配置（DELETE /admin-api/iscs/mail-notify-config/deleteMailNotifyConfigList?ids=7）
  deleteIsMailNotifyConfig: async (ids: number | number[]): Promise<void> => {
    const idStr = Array.isArray(ids) ? ids.join(',') : String(ids);
    await request.delete({
      url: '/iscs/mail-notify-config/deleteMailNotifyConfigList',
      params: { ids: idStr },
    });
  },
};

