import { httpApp } from "@/plugins/axiosApp";
import {
  Course_Type,
  ImportResponse,
  Privilege_Type,
  ServerPageResponse,
  User,
} from "@/types";
import { responseToFile } from "@/utils/utils";
import QueryString from "qs";

/** 用户分页查询 */
export function getUserList(params: {
  id?: number;
  loginName?: string;
  name?: string;
  enable?: boolean;
  role?: string;
  rootOrgId?: number;
  pageNo?: number;
  pageSize?: number;
}) {
  return httpApp.post<any, ServerPageResponse<User>>(
    "/api/ess/user/page",
    params
  );
}

/** 用户id查询 */
export function getUser(id: number) {
  return httpApp.post("/api/ess/user/info?id=" + id);
}

/** 更新用户 */
export function updateUser(params: {
  loginName?: string;
  password?: string;
  enable?: boolean;
  id?: number;
  roleId?: number;
  name?: string;
  rootOrgId?: number;
}) {
  return httpApp.post("/api/ess/user/save", params);
}

/** 禁用、启用用户 */
export function toggleUsers(enable: boolean, ids: number[]) {
  return httpApp.post(
    `/api/ess/user/${enable ? "enable" : "disable"}`,
    new URLSearchParams([["ids", ids.join(",")]])
  );
}

/** 重置密码 */
export function resetPasswords(ids: number[]) {
  return httpApp.post(
    `/api/ess/user/reset`,
    new URLSearchParams([["ids", ids.join(",")]])
  );
}

/** 用户自行修改密码 */
export function changePassword(password: string) {
  return httpApp.post(
    `/api/ess/user/password`,
    new URLSearchParams([["password", password]])
  );
}

/** 导入用户 */
export function importUsers(rootOrgId: number, file: File) {
  const f = new FormData();
  f.append("rootOrgId", rootOrgId + "");
  f.append("file", file);
  return httpApp.post<any, ImportResponse>(`/api/ess/user/import`, f);
}

/** 导出用户 */
export function exportUsers(params: {
  rootOrgId: number;
  role?: string;
  loginName?: string;
  name?: string;
  enable?: boolean;
}) {
  return httpApp
    .get(`/api/ess/user/export?` + QueryString.stringify(params), {
      responseType: "blob",
    })
    .then(function (response) {
      return responseToFile(response);
    });
}

/** 用户科目/机构数据权限分页查询 */
export function getUserPrivilegegList(params: {
  ptype: Privilege_Type;
  type?: Course_Type;
  userId?: number;
  pageNo?: number;
  pageSize?: number;
}) {
  return httpApp.post(
    `/api/ess/user/data/rule/list/for/${params.ptype.toLowerCase()}`,
    params
  );
}

/** 用户数据权限默认全部查询 */
export function getUserPrivilegegDefaultAll(params: {
  type: Privilege_Type;
  userId: number;
}) {
  const f = new FormData();
  f.append("userId", params.userId + "");
  f.append("type", params.type);
  return httpApp.post(`/api/ess/user/data/rule/default/status`, f);
}

/** 修改用户数据权限默认全部 */
export function modifyUserPrivilegegDefaultAll(params: {
  type: Privilege_Type;
  userId: number;
  enabled: boolean;
}) {
  const f = new FormData();
  f.append("userId", params.userId + "");
  f.append("type", params.type);
  f.append("enabled", params.enabled + "");
  return httpApp.post(`/api/ess/user/data/rule/default/status/update`, f);
}

/** 添加科目权限-分页查询科目 添加机构权限-分页查询机构 */
export function getToAddListForUserPrivilege(params: {
  ptype: Privilege_Type;
  rootOrgId: number;
  userId?: number;
  enable?: boolean;
  name?: string;
  pageNo?: number;
  pageSize?: number;
}) {
  return httpApp.post(
    `/api/ess/${params.ptype.toLowerCase()}/page/data/rule`,
    params
  );
}

/** 新增用户数据权限 */
export function addListForUserPrivilege(params: {
  type: Privilege_Type;
  userId: number;
  refIds: number[];
}) {
  return httpApp.post("/api/ess/user/data/rule/add", params);
}

/** 删除用户数据权限 */
export function deleteListForUserPrivilege(params: {
  type: Privilege_Type;
  userId: number;
  refIds: number[];
}) {
  return httpApp.post("/api/ess/user/data/rule/delete", params);
}
