import { http } from '@/utils/http/axios';
import { Role, RolePageRequest, RolePageResponse, RoleForm, AssignedPermissions } from '@/types/role/type';

/**
 * @description: 添加管理角色
 */
export function addAdminRole(params) {
  return http.request({
    url: '/role/addAdmin',
    method: 'POST',
    params,
  });
}

export interface RoleType<T> {
  permissionList?: T[];
  remark?: string;
  roleName?: string;
  roleCode?: string;
  roleId?: number;
}

export interface PermissionType {
  workshopCode: string;
  permissionId: string;
}

/**
 * @description: 添加用户角色
 */
export function addRole(params: RoleType<PermissionType>) {
  return http.request({
    url: '/role/add',
    method: 'POST',
    params,
  });
}

/**
 * @description: 修改用户角色
 */
export function updateRole(params: RoleType<PermissionType>) {
  return http.request({
    url: '/role/update',
    method: 'POST',
    params,
  });
}

/**
 * @description: 查询管理角色信息
 */
export function roleAdminInfo(params) {
  return http.request({
    url: '/role/infoAdmin',

    params,
  });
}

/**
 * @description: 查询用户角色信息
 */
export function roleUserInfo(params) {
  return http.request({
    url: '/role/info',

    params,
  });
}

/**
 * @description: 编辑管理角色
 */
export function editAdminRole(params) {
  return http.request({
    url: '/role/updateAdmin',
    method: 'POST',
    params,
  });
}

/**
 * @description: 删除管理角色
 */
export function delAdminRole(params) {
  return http.request({
    url: '/role/deleteAdmin',
    method: 'POST',
    params,
  });
}

/**
 * @description: 删除用户角色
 */
export function delUserRole(params) {
  return http.request({
    url: '/role/delete',
    method: 'POST',
    params,
  });
}

/**
 * @description: 角色列表
 */
export function roleList(params?) {
  return http.request({
    url: '/role/pageList',
    params,
  });
}

/**
 * @description: 所有角色列表
 */
export function roleAllList(params?) {
  return http.request({
    url: '/role/getAllRoles',

    params,
  });
}

/**
 * @description: 获取所有权限
 */
export function permissionList() {
  return http.request({
    url: '/role/listAllPermission',
  });
}

/** @description: 获取功能权限 */
export function getFeaturePermissions() {
  return http.request({
    url: '/role/listFunctionPermission',
  });
}

/**
 * v4: 获取自己拥有的和自己创建的角色(分页)
 */
export function getRolesPage(params: RolePageRequest) {
  return http.request<RolePageResponse>({
    url: '/admin/role/queryRolePage',
    method: 'POST',
    data: params,
  });
}

/**
 * v4: 获取自己拥有和自己创建的角色（全量）
 */
export function getRoles() {
  return http.request<Role[]>({
    url: '/admin/role/getRoleList',
    method: 'GET',
  });
}

/**
 * v4： 创建角色
 */
export function createRole(data: RoleForm) {
  return http.request({
    url: '/admin/role/addRole',
    method: 'POST',
    data,
  });
}

/**
 * v4: 编辑角色
 */
export function editRole(data: RoleForm) {
  return http.request({
    url: '/admin/role/updateRole',
    method: 'POST',
    data,
  });
}

/**
 * v4：删除菜单角色
 */
export function deleteRole(roleId: number) {
  return http.request({
    url: `/admin/role/deleteRole?roleId=${roleId}`,
    method: 'DELETE',
  });
}

/**
 * v4：获取角色已分配的权限（相机、菜单、功能）
 */
export function getAssignedPerms(roleId: number) {
  return http.request<AssignedPermissions>({
    url: `/admin/role/getSelectedPerm?roleId=${roleId}`,
    method: 'GET',
  });
}
