import axios, { AxiosResponse } from 'axios';
import type {
  LoginData,
  UserItem,
  UpdatePasswordParam,
  UserListPageParam,
  UserListPageRes,
  UserUpdateParam,
  ResetPasswordParam,
  EnableUserParam,
  BatchCreateUserParam,
  SystemInfo,
  RoleListPageRes,
  RolePrivilegeItem,
  RolePrivilegeUpdateParam,
  RoleListPageParam,
  RoleItem,
  UserMenuItem,
} from './types/user';
import { paramsSerializer } from '../utils/utils';

// 登录
export function login(data: LoginData): Promise<UserItem> {
  return axios.post('/api/admin/sys/login', data);
}

// 修改密码
export function updatePwd(datas: UpdatePasswordParam): Promise<boolean> {
  return axios.post('/api/user/password/modify', datas);
}

// 退出登录
export function userLogout() {
  return axios.post('/api/admin/sys/logout', {});
}

// 选择考试
export function switchExam(examId: number): Promise<boolean> {
  return axios.post('/api/admin/sys/select/exam', { examId });
}
// 选做分组
export function switchGroup(groupId: number): Promise<boolean> {
  return axios.post('/api/admin/sys/select/group', { groupId });
}

// 首次登录修改用户信息
export function updateFirstUserInfo(data: UserUpdateParam): Promise<boolean> {
  return axios.post('/api/admin/user/resetName', data);
}

// 系统信息
// 系统版本
export function systemVersion(): Promise<SystemInfo> {
  return axios.post('/api/admin/sys/version', {});
}

// 用户管理
// 获取用户列表 (分页)
export function userListPage(
  params: UserListPageParam
): Promise<UserListPageRes> {
  return axios.post('/api/admin/user/query', params);
}
// 获取指定用户
export function getUser(id: number): Promise<UserItem> {
  return axios.post(`/api/admin/user/find`, {}, { params: { id } });
}
// 新增或编辑用户
export function updateUser(data: UserUpdateParam): Promise<boolean> {
  const url = data.id ? '/api/admin/user/update' : '/api/admin/user/save';
  return axios.post(url, data, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
}
// 批量新增用户
export function batchAddUser(datas: BatchCreateUserParam): Promise<boolean> {
  const params = { ...datas };
  params.subjectCodeString = datas.subjectCodes.join();
  params.subjectCodes = undefined;
  return axios.post('/api/admin/user/batchSave', params);
}
// 删除用户: 暂时没有此功能
export function deleteUser(id: number): Promise<boolean> {
  return axios.post(`/api/admin/user/delete/${id}`);
}
// 重置用户密码
export function resetUserPassword(data: ResetPasswordParam): Promise<boolean> {
  return axios.post('/api/admin/user/resetPwd', paramsSerializer(data));
}
// 启用禁用用户
export function enableUser(datas: EnableUserParam): Promise<boolean> {
  return axios.post(`/api/admin/user/enable`, paramsSerializer(datas));
}
// 导入评卷员班级-导入模板下载
export function markerClassTemplate(): Promise<AxiosResponse<Blob>> {
  return axios.post(
    '/api/admin/user/class/template',
    {},
    {
      responseType: 'blob',
    }
  );
}
// 导出用户
export function exportUser(
  params: Record<string, any>
): Promise<AxiosResponse<Blob>> {
  return axios.post('/api/admin/user/export', params, {
    responseType: 'blob',
  });
}

// 按考试导出用户
export function exportUserByExam(
  params: Record<string, any>
): Promise<AxiosResponse<Blob>> {
  return axios.post('/api/admin/user/exportExam', params, {
    responseType: 'blob',
  });
}
// 按考试导出科目分表用户
export function exportUserBySubject(
  params: Record<string, any>
): Promise<AxiosResponse<Blob>> {
  return axios.post('/api/admin/user/exportSubject', params, {
    responseType: 'blob',
  });
}
// 绑定考生导入模板下载
export function bindStudentTemplate(): Promise<AxiosResponse<Blob>> {
  return axios.post(
    '/api/admin/user/student/template',
    {},
    {
      responseType: 'blob',
    }
  );
}
// 科组长-复核员导入模板下载
export function headerInspectorTemplate(params: {
  isHeader: boolean;
}): Promise<AxiosResponse<Blob>> {
  return axios.post('/api/admin/user/subject/template', params, {
    responseType: 'blob',
  });
}

// 导出列表表头获取
export function getExportFields(type: 'STUDENT' | 'SCORE'): Promise<string[]> {
  return axios.post('/api/admin/user/export/cols/info', { type });
}
// 导出列表表头保存
export function saveExportFields(data: {
  type: 'STUDENT' | 'SCORE';
  cols: string[];
}): Promise<boolean> {
  return axios.post('/api/admin/user/export/cols/save', data, {
    headers: {
      'Content-Type': 'application/json',
    },
  });
}

// 角色权限 ------->
// 获取角色列表
export function getRoleList(
  params: RoleListPageParam
): Promise<RoleListPageRes> {
  return axios.post('/api/admin/role/page', params);
}
// 获取所有角色
export function getAllRoleList(schoolId: number): Promise<RoleItem[]> {
  return axios.post('/api/admin/role/list/all', { schoolId });
}

// 获取权限列表
export function getRolePrivilegeList(params: {
  schoolId: number;
  role: string;
}): Promise<RolePrivilegeItem[]> {
  return axios.post('/api/admin/role/privilege/all', params);
}
// 保存角色权限
export function updateRolePrivilege(
  data: RolePrivilegeUpdateParam
): Promise<boolean> {
  return axios.post('/api/admin/role/privilege/save', data);
}

// 获取用户权限菜单
export function getUserPrivilegeMenu(): Promise<UserMenuItem[]> {
  return axios.post('/api/admin/role/privilege/menu', {});
}
