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

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

// 新增的获取用户详细信息的API
export function getUserDetailApi(p: object): AxiosPromise {
  return request({
    url: "/api/v1/sys/user/get",
    method: "post",
    data: p,
  });
}
/**
 * 获取用户分页列表
 *
 * @param queryParams
 */
export function getUserList(queryParams: any): AxiosPromise<any[]> {
  return request({
    url: "/api/v1/sys/user/list",
    method: "post",
    data: queryParams,
  });
}

export function getPostOptions(queryParams?: any): AxiosPromise<OptionType[]> {
  return request({
    url: "/api/v1/sys/post/list",
    method: "post",
    data: queryParams,
  });
}

/**
 * 获取用户分页列表
 *
 * @param queryParams
 */
export function getUserPage(queryParams: any): AxiosPromise<PageResult<any[]>> {
  return request({
    url: "/api/v1/sys/user/page",
    method: "post",
    data: queryParams,
  });
}

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

/**
 * 修改用户
 *
 * @param id
 * @param data
 */
export function updateUser(id: number, data: any) {
  return request({
    url: "/api/v1/sys/user/update",
    method: "post",
    data: { id: id, ...data },
  });
}

/**
 * 修改用户密码
 *
 * @param id
 * @param password
 */
export function updateUserPassword(obj: any) {
  return request({
    url: "/api/v1/sys/user/resetPwd",
    method: "post",
    data: obj,
  });
}

/**
 * 删除用户
 *
 * @param ids
 */
export function deleteUsers(ids: any) {
  return request({
    url: "/api/v1/sys/user/del",
    method: "post",
    data: { id: ids },
  });
}

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

/**
 * 导出用户
 *
 * @param queryParams
 * @returns
 */
export function exportUser(queryParams: any) {
  return request({
    url: "/api/v1/sys/user/export",
    method: "post",
    data: 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/sys/user/import",
    method: "post",
    params: { deptId: deptId },
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
}
