import request from '/@/utils/request';

export const list = (params?: Object) => {
	return request({
		url: '/admin/role/list',
		method: 'get',
		params,
	});
};

export const pageList = (params?: Object) => {
	return request({
		url: '/admin/role/page',
		method: 'get',
		params,
	});
};

export const deptRoleList = () => {
	return request({
		url: '/admin/role/list',
		method: 'get',
	});
};

export const getObj = (id: string) => {
	return request({
		url: '/admin/role/details/' + id,
		method: 'get',
	});
};

export const getObjDetails = (obj: object) => {
	return request({
		url: '/admin/role/details',
		method: 'get',
		params: obj,
	});
};

export const addObj = (obj: Object) => {
	return request({
		url: '/admin/role',
		method: 'post',
		data: obj,
	});
};

export const putObj = (obj: Object) => {
	return request({
		url: '/admin/role',
		method: 'put',
		data: obj,
	});
};

export const delObj = (ids: Object) => {
	return request({
		url: '/admin/role',
		method: 'delete',
		data: ids,
	});
};
/**
 * 批量设置角色分组
 * @param roleIds 角色ID列表
 * @param roleGroup 分组名称（空表示未分组）
 */
export const batchUpdateRoleGroup = (roleIds: string[], roleGroup: string) => {
	return request({
		url: '/admin/role/batchGroup',
		method: 'put',
		data: { roleIds, roleGroup: roleGroup || '' },
	});
};

/**
 * 批量指定角色关联用户
 * @param roleId 角色ID
 * @param userIds 用户ID列表
 */
export const assignUsersToRole = (roleId: string, userIds: string[]) => {
	return request({
		url: '/admin/role/assignUsers',
		method: 'post',
		data: { roleId, userIds },
	});
};

/**
 * 根据角色ID查询该角色下绑定的用户列表（含部门、姓名、工号）
 * @param roleId 角色ID
 */
export const getUsersByRoleId = (roleId: string) => {
	return request({
		url: '/admin/role/users/' + roleId,
		method: 'get',
	});
};

/**
 * 解除指定用户与该角色的关联
 * @param roleId 角色ID
 * @param userId 用户ID
 */
export const unassignUserFromRole = (roleId: string, userId: string) => {
	return request({
		url: `/admin/role/users/${roleId}/${userId}`,
		method: 'delete',
	});
};

export const permissionUpd = (roleId: string, menuIds: string) => {
	return request({
		url: '/admin/role/menu',
		method: 'put',
		data: {
			roleId: roleId,
			menuIds: menuIds,
		},
	});
};

export const fetchRoleTree = (roleId: string) => {
	return request({
		url: '/admin/menu/tree/' + roleId,
		method: 'get',
	});
};

export function validateRoleCode(rule: any, value: any, callback: any, isEdit: boolean) {
	if (isEdit) {
		return callback();
	}

	getObjDetails({ roleCode: value }).then((response) => {
		const result = response.data;
		if (result !== null) {
			callback(new Error('角色标识已经存在'));
		} else {
			callback();
		}
	});
}

export function validateRoleName(rule: any, value: any, callback: any, isEdit: boolean) {
	if (isEdit) {
		return callback();
	}

	getObjDetails({ roleName: value }).then((response) => {
		const result = response.data;
		if (result !== null) {
			callback(new Error('角色名称已经存在'));
		} else {
			callback();
		}
	});
}


export const listAllRole = () => {
	return request({
		url: '/admin/role/listAllRole',
		method: 'get',
	});
};