import request from '@/utils/request';
import { useHttp } from '@crami/http';
export type LoginData = 'account' | 'telephone';
export type LoginStatus = 'ok' | 'error';
import { commonApi } from '@/api/commonApi';

export interface LoginParams {
  username: string;
  password: string;
  code: string;
  uuid: string;
  loginMode?: string;
}

export interface LoginResp {
  code: number;
  msg: string;
  data: {
    [key: string]: any;
  };
  // currentAuthority: string;
}

export interface UserInfo {
  companyId: string;
  connect: string;
  avatar: string;
  lang: string;
  user_id: string;
  user_name: string;
  email: string;
  group: string;
  name: string;
  phone: string;
  signature: string;
  user_type: string;
  role: {
    [key: string]: any;
  };
}

export interface CaptchaResp {
  captcha: number;
}

export interface SmsCaptchaRequest {
  mobile: string;
}

// 后端的结构体定义
export type RouteItem = {
  id: number | string;
  parentId: number | string;
  name: string;
  path: string;
  redirect: string;
  component: string;
  meta: {
    title: string | false;
    icon?: string;
    target?: '_blank' | '_self';
    hideInMenu?: boolean;
    hideChildrenInMenu?: boolean;
    authority?: string | string[];
    [key: string]: any;
  };
};

export interface PermissionRequest {
  lang: string;
  objectId: string;
  productLine: string;
  companyId: string;
  connect: string;
  userType: string;
  userName: string;
  orgId: string;
  projectCode: string;
  permissionType: string;
  userId?: string;
}

export async function getLoginCode() {
  return request.get<any, any>('/code');
}

export async function getTenant() {
  return request.get<any, any>('/system/tenant/getTenant');
}

export async function postAccountLogin(params: LoginParams) {
  return request.post<LoginParams, LoginResp>('/auth/login', params);
}

// 获取组织列表接口
export async function getOrgByUserName(params) {
  return request.post<any, any>('/system/org/getOrgByUserName', params);
}

export async function getLoginOrg() {
  return request.get<any, any>('/system/org/getOrg');
}

export async function getCurrentUser(info: any) {
  return request.get<any, UserInfo>('/system/user/getUseInfo', { params: info });
}

export async function getProjectRoutes(info: any) {
  const method = commonApi().studioCache.getByProjectAndType.method;
  const url = commonApi().studioCache.getByProjectAndType.url;
  return request[method]<any, any>(url.replace('/api', ''), { params: info });
}

export async function getCurrentUserNav() {
  return request.get<any, RouteItem[]>('/currentUserNav');
}

// export async function postLogout() {
//   return request.delete<any, any>('/auth/logout');
// }

export async function postLogout(isClear) {
  const url = isClear ? '/api/auth/logout/1' : '/api/auth/logout/0';
  return await useHttp({
    method: 'delete',
    url: url,
  });
}

export async function getSmsCaptcha(params: SmsCaptchaRequest) {
  return request.get<SmsCaptchaRequest, CaptchaResp>('/message/captcha/sms', {
    params,
  });
}

// 获取当前用户的导航和menu权限
export async function getPermissionData(params: PermissionRequest) {
  return request.post<any, any>('/menuperservice/function/getPermissionData', params);
}

// 获取当前用户的导航和menu权限
export async function changeOrgIdOfUserInRedis(params: any) {
  return request.post<any, any>('/auth/changeOrgIdOfUserInRedis', params);
}

// 根据当前用户获取默认的登录方式
export async function getLoginModeByUserName(params: any) {
  return request.post<any, any>('/system/org/getLoginModeByUserName', params);
}
