import axios, { AxiosResponse } from 'axios';
import { UserState } from '@/store/modules/user/types';
import type {
  LoginData,
  UpdatePwdData,
  UserListPageParam,
  UserListPageRes,
  UserUpdateParam,
  ResetPasswordParam,
  EnableUserParam,
  BatchCreateUserParam,
} from './types/user';

// 登录
export function login(data: LoginData): Promise<UserState> {
  return axios.post('/api/user/login', data);
}
// 修改密码
export function updatePwd(datas: UpdatePwdData): Promise<UserState> {
  return axios.post('/api/user/password/modify', {}, { params: datas });
}
// 退出登录
export function userLogout() {
  return axios.post('/api/user/logout', {});
}

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

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