import { baseRequestClient, requestClient } from '#/api/request';

export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    account: string;
    pwd: string;
    code: string;
    uuid: string;
  }

  /** 登录接口返回值 */
  export interface LoginResult {
    /**
     * 用户名
     */
    account: string;
    /**
     * 用户id
     */
    accountid: string;
    /**
     * 部门
     */
    bm: null | string;
    /**
     * 部门id
     */
    bmid: number;
    /**
     * 角色列表
     */
    grouplist: string;
    /**
     * 昵称
     */
    nickname: string;
    /**
     * 权限列表
     */
    popedom: string;
    /**
     * 过期时间
     */
    timeout: number;
    /**
     * 令牌
     */
    token: string;
    /**
     * 职员id
     */
    workerid: string;
  }

  export interface RefreshTokenResult {
    data: string;
    status: number;
  }
}

/**
 * 获取登陆验证码
 */
export async function getLoginCaptchaApi(data: { uuid: string }) {
  return requestClient.get<AuthApi.LoginResult>('/api/sys/getloginvifcode', {
    params: data,
    responseType: 'arraybuffer',
  });
}

/**
 * 登录
 */
export async function loginApi(data: AuthApi.LoginParams) {
  return requestClient.post<AuthApi.LoginResult>('/api/sys/login', data);
}

/**
 * 刷新accessToken
 */
export async function refreshTokenApi() {
  return baseRequestClient.post<AuthApi.RefreshTokenResult>('/auth/refresh', {
    withCredentials: true,
  });
}

/**
 * 退出登录
 */
export async function logoutApi() {
  return baseRequestClient.post('/auth/logout', {
    withCredentials: true,
  });
}

/**
 * 获取用户权限码
 */
export async function getAccessCodesApi() {
  return requestClient.get<string[]>('/auth/codes');
}
