import request from "@/utils/request";
import { AxiosPromise } from "axios";
import { UserForm, UserPageVO, UserQuery } from "./types";

/**
 * 登录成功后获取用户信息（昵称、头像、权限集合和角色集合）
 */
export function getUserInfoApi(): AxiosPromise {
  return request({
    url: "/api/auth",
    method: "get",
  });
}

/**
 * 获取用户分页列表
 *
 * @param queryParams
 */
export function getUserPage(
  queryParams: UserQuery
): AxiosPromise<PageResult<UserPageVO[]>> {
  return request({
    url: "/api/v1/users/page",
    method: "get",
    params: queryParams,
  });
}

/**
 * 获取用户表单详情
 *
 * @param userId
 */
export function getUserForm(userId: number): AxiosPromise<UserForm> {
  return request({
    url: "/api/v1/users/" + userId + "/form",
    method: "get",
  });
}

/**
 * 添加用户
 *
 * @param data
 */
export function addUser(data: any) {
  return request({
    url: "/api/v1/users",
    method: "post",
    data: data,
  });
}

/**
 * 修改用户
 *
 * @param id
 * @param data
 */
export function updateUser(id: number, data: UserForm) {
  return request({
    url: "/api/v1/users/" + id,
    method: "put",
    data: data,
  });
}

/**
 * 获取用户详情
 *
 */
export function getUserDetail(id: number) {
  return request({
    url: "/api/v1/syn/user/info/get/" + id,
    method: "get",
  });
}
/**
 * 获取用户分页列表
 *
 * @param queryParams
 */
export function getUserList(): AxiosPromise<any[]> {
  return request({
    url: "api/v1/syn/user/info/listAll",
    method: "post",
  });
}

export function getUserTree(): AxiosPromise {
  return request({
    url: "api/v1/syn/user/info/tree",
    method: "get",
  });
}

/**
 * 修改用户密码
 *
 * @param id
 * @param password
 */
export function updateUserPassword(id: number, password: string) {
  return request({
    url: "/api/v1/users/" + id + "/password",
    method: "patch",
    params: { password: password },
  });
}

/**
 * 修改用户密码
 *
 * @param id
 * @param password
 */
export function updatePassword(data: any) {
  return request({
    url: "/api/v1/syn/user/info/update-password",
    method: "post",
    data: data,
  });
}
/**
 * 删除用户
 *
 * @param ids
 */
export function deleteUsers(ids: string) {
  return request({
    url: "/api/v1/users/" + ids,
    method: "delete",
  });
}

/**
 * 下载用户导入模板
 *
 * @returns
 */
export function downloadTemplateApi() {
  return request({
    url: "/api/v1/users/template",
    method: "get",
    responseType: "arraybuffer",
  });
}

/**
 * 导出用户
 *
 * @param queryParams
 * @returns
 */
export function exportUser(queryParams: UserQuery) {
  return request({
    url: "/api/v1/users/_export",
    method: "get",
    params: queryParams,
    responseType: "arraybuffer",
  });
}

/**
 * 导入用户
 *
 * @param file
 */
export function importUser(deptId: number, file: File) {
  const formData = new FormData();
  formData.append("file", file);
  return request({
    url: "/api/v1/users/_import",
    method: "post",
    params: { deptId: deptId },
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
}

// 综合管理平台菜单管理列表查询
export function systemMenuList() {
  return request({
    url: "/api/v1/syn/menu/list",
    method: "post",
    data: {},
  });
}
// 角色管理列表查询
export function allRoleList() {
  return request({
    url: "/api/v1/synRoleInfo/list",
    method: "post",
    data: {},
  });
}
// 用户已经选择的角色
export function bindedUserRoleList(id: string) {
  return request({
    url: `/api/v1/synUserRole/get/${id}`,
    method: "get",
  });
}
export function bindRoleUserList(obj: Object) {
  return request({
    url: "/api/v1/synUserRole/add",
    method: "post",
    data: obj,
  });
}

// 更新综合管理平台菜单管理
export function updateSystemMenuList(list: any[]) {
  return request({
    url: "/api/v1/syn/menu/update",
    method: "post",
    data: list,
  });
}

// 综合管理平台用户菜单权限列表查询
export function systemUserMenuList(id: string) {
  return request({
    url: "/api/v1/syn/user/menu/list",
    method: "post",
    data: { userId: id },
  });
}

// 综合管理平台用户菜单权限列表查询
export function bindedRoleMenuList(roleId: string) {
  return request({
    url: `/api/v1/synRoleMenu/get/${roleId}`,
    method: "get",
  });
}
export function bindRoleMenuList(obj: Object) {
  return request({
    url: "/api/v1/synRoleMenu/add",
    method: "post",
    data: obj,
  });
}

// 新增综合管理平台用户菜单权限
export function addSystemUserMenu(obj: Object) {
  return request({
    url: "/api/v1/syn/user/menu/add",
    method: "post",
    data: obj,
  });
}

/**
 * 部门树形列表
 * @param queryParams
 */
export function treeList(): AxiosPromise<any[]> {
  return request({
    url: "/api/v1/syn/dept/info/orgTree",
    method: "get",
  });
}
