import { http } from '@/utils/http/axios';
import {
  AddPersonGroupParams,
  EditPersonGroupParams,
  QueryPersonGroupPageParams,
  QueryAvailablePersonPageParams,
  QueryAvailablePersonPageParamsAll,
  QueryPersonGroupPageRes,
  QueryPersonGroupDetailRes,
  QueryAvailablePersonPageRes,
  UserGroupOption,
  PersonGroupItem,
  QueryUserInfoByUserNameRes,
} from '@/types/person-group/type';

/**
 * @description: 新增用户组
 */
export function addUserGroup(params: AddPersonGroupParams) {
  return http.request({
    url: `/userGroup/addUserGroup`,
    method: 'POST',
    data: params,
  });
}

/**
 * @description: 编辑用户组
 */
export function modifyUserGroup(params: EditPersonGroupParams) {
  return http.request({
    url: `/userGroup/modifyUserGroup`,
    method: 'PUT',
    data: params,
  });
}

/**
 * @description: 删除用户组
 */
export function deleteUserGroup(userGroupId: number) {
  return http.request({
    url: `/userGroup/deleteUserGroup?userGroupId=${userGroupId}`,
    method: 'DELETE',
  });
}

/**
 * @description: 查询用户组分页
 */
export function queryUserGroupPage(params: QueryPersonGroupPageParams): Promise<QueryPersonGroupPageRes> {
  return http.request({
    url: `/userGroup/queryUserGroupPage`,
    method: 'POST',
    data: params,
  });
}

/**
 * 全量查询用户组
 */
export const getAllUserGroup = () => {
  return http.request<{ groupVOList: UserGroupOption[] }>({
    url: '/userGroup/ToPushObjectQueryUserGroupList',
    method: 'post',
  });
};

/**
 * @description: 查询用户组详情
 */
export function queryUserGroupDetail(userGroupId: number): Promise<QueryPersonGroupDetailRes> {
  return http.request({
    url: `/userGroup/queryUserGroupDetail?userGroupId=${userGroupId}`,
    method: 'GET',
  });
}

/**
 * 批量查询用户组情况
 */
export const getUserGroupDetailByIds = (userGroupList: number[]) => {
  return http.request<QueryPersonGroupDetailRes[]>({
    url: '/userGroup/queryUserGroupDetailByIds',
    method: 'post',
    data: { userGroupList },
  });
};

/**
 * @description: 查询可添加到用户组的用户
 */
// 分页
export function queryAvailableUserList(params: QueryAvailablePersonPageParams): Promise<QueryAvailablePersonPageRes> {
  return http.request({
    url: `/admin/user/queryAvailableUserList`,
    method: 'POST',
    data: params,
  });
}
// 不分页
export function queryAvailableUserListAll(params: QueryAvailablePersonPageParamsAll): Promise<PersonGroupItem[]> {
  return http.request<PersonGroupItem[]>({
    url: `/admin/user/queryUserListByFilter`,
    method: 'post',
    data: params,
  });
}

/**
 * 查询用户所在一级部门的组织树
 */
export const getUserFirstLevelTree = () => {
  return http.request({
    url: '/admin/dept/queryFirstLevelDeptTreeById',
    method: 'get',
  });
};

/**
 * 根据ids查询用户信息
 */
export const queryUserInfoByIds = (userIds: number[]) => {
  return http.request<PersonGroupItem[]>({
    url: `/admin/user/queryUserList?userIds=${userIds.join(',')}`,
    method: 'get',
  });
};

/**
 * 根据用户名查询用户列表
 */
export const queryUserInfoByUserName = (username: string) => {
  return http.request<QueryUserInfoByUserNameRes[]>({
    url: `/admin/user/queryUserListByUsername?username=${username}`,
    method: 'post',
  });
};

/**
 * 导出用户组名单
 */
export const exportUserGroupList = (userGroupId: number) => {
  return http.request(
    {
      url: `/userGroup/exportGroupUserList?userGroupId=${userGroupId}`,
      method: 'get',
      responseType: 'blob',
    },
    { isTransformResponse: false },
  );
};
