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

export namespace UserApi {
  export interface DeptResult {
    id: number;
    name: string;
  }
  export interface RoleResult {
    id: number;
    name: string;
  }
  export interface CsrfTokenResult {
    token: string;
    headerName: string;
    parameterName: string;
  }

  export interface UserInfo {
    id?: number;
    username: string;
    dept: DeptResult;
    role: RoleResult;
    permissions: string[];
  }
  export interface UserResult {
    user: string | UserInfo;
    csrf: CsrfTokenResult;
  }

  export interface Res {
    code: number;
    data: object;
    msg: string;
  }

  // 重置密码数据
  export interface PasswordReSetModel {
    oldPassword: string;
    password: string;
    confirmPassword: string;
  }

  export interface PasswordModel {
    password: string;
  }

  export interface RoleCreateRecord {
    name: string;
    dataScope: string;
    permissionIds: (number | undefined)[];
    remark: string;
    authorities: (number | undefined)[];
  }

  export interface RoleRecord extends RoleCreateRecord {
    id: string;
  }

  // 添加用户数据
  export interface CreateRecord {
    value: any;
    code: any;
    username: string;
    nickName: string;
    password: string;
    phone: string;
    email: string;
    enabled: string;
    address: string;
    roleIds: RoleRecord | string[] | undefined;
    permissionIds: (number | undefined)[];
    authorities: string[];
  }

  // 用户数据
  export interface UserRecord extends CreateRecord {
    value: any;
    id?: string;
    avatar?: string;
    createAt?: string;
  }

  export interface User {
    username?: string;
    nickName?: string;
    name: string;
    avatar?: string;
    email?: string;
    phone?: string;
    address?: string;
    createAt?: string;
    remark?: string;
    id?: number;
    role?: RoleRecord;
    roles?: RoleRecord[];
    // permissions?: string[] | '' | '*' | 'admin' | 'user' | 'auditor';
    permissions?: string[] | undefined;
    authorities?: string[];
  }

  export interface UserUpdateRecord {
    name?: string;
    email?: string;
    phone?: string;
    address?: string;
    avatar?: string;
  }
}

/**
 * 获取用户信息
 */
export async function getUserInfoApi() {
  return requestClient.get<UserApi.UserResult>('/rest/user/me');
}

// 更新密码
export function resetPassword(data: UserApi.PasswordReSetModel) {
  return requestClient.patch('/rest/user/self/update-password', data);
}

// 重置密码
export function resetPasswordApi(id: any, data: UserApi.PasswordModel) {
  return requestClient.patch(
    `/rest/user/${id}/update-password`,
    {},
    {
      params: data,
    },
  );
}

// 注册用户
export function register(data: UserApi.CreateRecord) {
  return requestClient.post('/rest/user/register', data);
}

// 新建用户
export function createUser(data: UserApi.CreateRecord) {
  return requestClient.post('/rest/user', data);
}

// 用户切换角色
export function switchRole(id: number) {
  return requestClient.patch(`/rest/user/switch/${id}`);
}

// 模糊查询用户列表
export function queryUserList(params: any) {
  return requestClient.get('/rest/user', { params });
}

// 根据id查询用户信息
export function userDetail(id: any) {
  return requestClient.get(`/rest/user/${id}`);
}

// 是否启用
export function enabledUser(id: any) {
  return requestClient.patch(`/rest/user/${id}/toggle`);
}

// 删除用户
export function removeUser(id: string) {
  return requestClient.delete(`/rest/user/${id}`);
}

// 更新用户信息
export function updateUser(id: any, data: UserApi.UserRecord) {
  return requestClient.patch<UserApi.Res>(`/rest/user/${id}`, data);
}

export function selfUpdate(data: UserApi.UserUpdateRecord) {
  return requestClient.patch<UserApi.Res>(`/rest/user/self`, data);
}

// 获取个人用户信息
export function getUserInfo() {
  return requestClient.get<UserApi.User>('/rest/user/self');
}

// 部门的审核员
export function deptAudit(id: string, roleId: string) {
  return requestClient.get(`/rest/user/dept/${id}?roleId=${roleId}`);
}

export function getUserDetail(id: number) {
  return requestClient.get<UserApi.User>(`/user/${id}`);
}
