import axiosInstance from '../utils/axios';

// 登录表单类型
export interface LoginFormParams {
  tenantName?: string;
  username: string;
  password: string;
  captchaVerification?: string;
  rememberMe?: boolean;
}

// 登录响应类型
export interface LoginResponse {
  userId: string;
  accessToken: string;
  token?: string; // 兼容旧格式
  user?: any;
}

// 租户信息类型
export interface TenantInfo {
  id: string | number;
  name: string;
}

// 验证码相关类型
export interface CaptchaGetParams {
  captchaType?: string;
}

export interface CaptchaCheckParams {
  captchaVerification: string;
  captchaType?: string;
}

// 登录相关 API
export const loginApi = {
  // 登录
  login: (params: LoginFormParams) => {
    return axiosInstance.post<LoginResponse>('/system/auth/login', params);
  },

  // 根据租户名称获取租户ID
  getTenantIdByName: (name: string) => {
    return axiosInstance.get<string | number>('/system/tenant/get-id-by-name?name=' + encodeURIComponent(name));
  },

  // 根据域名获取租户信息
  getTenantByWebsite: (website: string) => {
    return axiosInstance.get<TenantInfo>('/system/tenant/get-by-website?website=' + encodeURIComponent(website));
  },

  // 社交登录重定向
  socialAuthRedirect: (type: number, redirectUri: string) => {
    return axiosInstance.get<string>('/system/auth/social-auth-redirect?type=' + type + '&redirectUri=' + encodeURIComponent(redirectUri));
  },

  // 刷新Token
  refreshToken: (refreshToken: string) => {
    return axiosInstance.post<LoginResponse>('/system/auth/refresh-token?refreshToken=' + encodeURIComponent(refreshToken));
  },

  // 登出
  logout: () => {
    return axiosInstance.post('/system/auth/logout');
  },

  // 获取用户权限信息
  getInfo: () => {
    return axiosInstance.get('/system/auth/get-permission-info');
  },

  // 获取验证图片以及 token（返回原始响应）
  getCode: (data?: CaptchaGetParams) => {
    return axiosInstance.post('/system/captcha/get', data || {}, {
      // 对于验证码接口，可能需要返回原始响应
      transformResponse: [(data) => data],
    });
  },

  // 滑动或者点选验证（返回原始响应）
  reqCheck: (data: CaptchaCheckParams) => {
    return axiosInstance.post('/system/captcha/check', data, {
      // 对于验证码接口，可能需要返回原始响应
      transformResponse: [(data) => data],
    });
  },

  // 发送短信验证码
  sendSmsCode: (params: {
    tenantName?: string;
    mobile: string;
    captchaVerification?: string;
    scene: number; // 场景值，23 表示重置密码
  }) => {
    return axiosInstance.post('/system/auth/send-sms-code', params);
  },

  // 短信重置密码
  smsResetPassword: (params: {
    tenantName?: string;
    mobile: string;
    code: string;
    password: string;
  }) => {
    return axiosInstance.post('/system/auth/reset-password', params);
  },

  // 重置密码（通过短信，兼容旧接口）
  resetPassword: (params: {
    company?: string;
    phone: string;
    code: string;
    newPassword: string;
  }) => {
    return axiosInstance.post('/system/auth/reset-password', params);
  },

  // 发送验证码（兼容旧接口）
  sendVerificationCode: (params: { phone: string; company: string }) => {
    return axiosInstance.post('/auth/send-code', params);
  },

  // 获取首页统计数据
  getHomePage: () => {
    return axiosInstance.get('/iscs/statistics-api/getHomePage');
  },

  // 根据key获取系统属性
  getSystemAttributeByKey: (sysAttrKey: string) => {
    return axiosInstance.get(`/iscs/attribute/getSystemAttributeByKey?sysAttrKey=${encodeURIComponent(sysAttrKey)}`);
  },

  // 获取未读消息数量
  getUnreadCount: () => {
    return axiosInstance.get('/system/notify-message/get-unread-count');
  },

  // 获取我的代理工单
  getMyAgent: (params?: {
    pageNum?: number;
    pageSize?: number;
    queryType?: number;
    startTime?: string;
    endTime?: string;
  }) => {
    const queryParams = new URLSearchParams();
    if (params?.pageNum !== undefined) queryParams.append('pageNum', String(params.pageNum));
    if (params?.pageSize !== undefined) queryParams.append('pageSize', String(params.pageSize));
    if (params?.queryType !== undefined) queryParams.append('queryType', String(params.queryType));
    if (params?.startTime) queryParams.append('startTime', params.startTime);
    if (params?.endTime) queryParams.append('endTime', params.endTime);
    const queryString = queryParams.toString();
    return axiosInstance.get(`/iscs/job-ticket/getMyAgent${queryString ? '?' + queryString : ''}`);
  },

  // 获取字典数据简单列表
  getDictDataSimpleList: () => {
    return axiosInstance.get('/system/dict-data/simple-list');
  },
};

