import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Router } from '@angular/router'; import { MatDialog } from '@angular/material/dialog'; import { MatBottomSheet } from '@angular/material/bottom-sheet'; import { ToastrService } from 'ngx-toastr'; import { JoyrideService } from 'ngx-joyride'; import { FlatTreeControl } from '@angular/cdk/tree'; import { MatTreeFlatDataSource, MatTreeFlattener } from '@angular/material/tree'; import { Subscription } from 'rxjs'; import { DataService } from './services/data.service'; import { UnidadOrganizativa, Client, TreeNode, FlatNode, Command } from './model/model'; import { CreateOrganizationalUnitComponent } from './shared/organizational-units/create-organizational-unit/create-organizational-unit.component'; import { CreateClientComponent } from './shared/clients/create-client/create-client.component'; import { EditOrganizationalUnitComponent } from './shared/organizational-units/edit-organizational-unit/edit-organizational-unit.component'; import { EditClientComponent } from './shared/clients/edit-client/edit-client.component'; import { ShowOrganizationalUnitComponent } from './shared/organizational-units/show-organizational-unit/show-organizational-unit.component'; import { LegendComponent } from './shared/legend/legend.component'; import { DeleteModalComponent } from '../../shared/delete_modal/delete-modal/delete-modal.component'; import { ClassroomViewDialogComponent } from './shared/classroom-view/classroom-view-modal'; import { MatSort } from '@angular/material/sort'; import { MatTableDataSource } from '@angular/material/table'; import { MatPaginator } from '@angular/material/paginator'; import { CreateMultipleClientComponent } from "./shared/clients/create-multiple-client/create-multiple-client.component"; import { SelectionModel } from "@angular/cdk/collections"; enum NodeType { OrganizationalUnit = 'organizational-unit', ClassroomsGroup = 'classrooms-group', Classroom = 'classroom', ClientsGroup = 'clients-group', Client = 'client', } @Component({ selector: 'app-groups', templateUrl: './groups.component.html', styleUrls: ['./groups.component.css'], }) export class GroupsComponent implements OnInit, OnDestroy { baseUrl: string = import.meta.env.NG_APP_BASE_API_URL; organizationalUnits: UnidadOrganizativa[] = []; selectedUnidad: UnidadOrganizativa | null = null; selectedDetail: UnidadOrganizativa | null = null; initialLoading: boolean = true; isLoadingClients: boolean = false; searchTerm = ''; treeControl: FlatTreeControl; treeFlattener: MatTreeFlattener; treeDataSource: MatTreeFlatDataSource; selectedNode: TreeNode | null = null; hasClients: boolean = false; commands: Command[] = []; commandsLoading = false; selectedClients = new MatTableDataSource([]); selection = new SelectionModel(true, []); cols = 4; currentView: 'card' | 'list' = 'list'; savedFilterNames: [string, string][] = []; selectedTreeFilter = ''; syncStatus = false; syncingClientId: string | null = null; private originalTreeData: TreeNode[] = []; arrayClients: any[] = []; displayedColumns: string[] = ['select', 'status', 'sync', 'name', 'oglive', 'subnet', 'pxeTemplate', 'actions']; private _sort!: MatSort; private _paginator!: MatPaginator; @ViewChild(MatSort) set matSort(ms: MatSort) { this._sort = ms; if (this.selectedClients) { this.selectedClients.sort = this._sort; } } @ViewChild(MatPaginator) set matPaginator(mp: MatPaginator) { this._paginator = mp; if (this.selectedClients) { this.selectedClients.paginator = this._paginator; } } private subscriptions: Subscription = new Subscription(); constructor( private http: HttpClient, private router: Router, private dataService: DataService, public dialog: MatDialog, private bottomSheet: MatBottomSheet, private joyrideService: JoyrideService, private toastr: ToastrService ) { this.treeFlattener = new MatTreeFlattener( this.transformer, (node) => node.level, (node) => node.expandable, (node) => node.children ); this.treeControl = new FlatTreeControl( (node) => node.level, (node) => node.expandable ); this.treeDataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener); } ngOnInit(): void { this.updateGridCols(); this.refreshData(); window.addEventListener('resize', this.updateGridCols); this.selectedClients.filterPredicate = (client: Client, filter: string): boolean => { const lowerTerm = filter.toLowerCase(); return ( client.name.toLowerCase().includes(lowerTerm) || client.ip?.toLowerCase().includes(lowerTerm) || client.status?.toLowerCase().includes(lowerTerm) || client.mac?.toLowerCase().includes(lowerTerm) ); }; } ngOnDestroy(): void { window.removeEventListener('resize', this.updateGridCols); this.subscriptions.unsubscribe(); } private transformer = (node: TreeNode, level: number): FlatNode => ({ id: node.id, name: node.name, type: node.type, level, expandable: !!node.children?.length, hasClients: node.hasClients, ip: node.ip, '@id': node['@id'], }); toggleView(view: 'card' | 'list'): void { this.currentView = view; } updateGridCols = (): void => { const width = window.innerWidth; this.cols = width <= 600 ? 1 : width <= 960 ? 2 : width <= 1280 ? 3 : 4; }; clearSelection(): void { this.selectedUnidad = null; this.selectedDetail = null; this.selectedClients.data = []; this.selectedNode = null; } // Función para obtener los filtros guardados actualmente deshabilitada // getFilters(): void { // this.subscriptions.add( // this.dataService.getFilters().subscribe( // (data) => { // this.savedFilterNames = data.map((filter: { name: string; uuid: string; }) => [filter.name, filter.uuid]); // }, // (error) => { // console.error('Error fetching filters:', error); // } // ) // ); // } getFilters(): void { this.subscriptions.add( this.dataService.getFilters().subscribe( (data) => { this.savedFilterNames = data.map((filter: { name: string; uuid: string; }) => [filter.name, filter.uuid]); }, (error) => { console.error('Error fetching filters:', error); } ) ); } private convertToTreeData(data: UnidadOrganizativa): TreeNode { const processNode = (node: UnidadOrganizativa): TreeNode => { const children = node.children?.map(processNode) || []; const hasClients = (node.clients?.length ?? 0) > 0 || children.some(child => child.hasClients); return { id: node.id, uuid: node.uuid, name: node.name, type: node.type, '@id': node['@id'], children: children, hasClients: hasClients, }; }; return processNode(data); } private refreshData(selectedNodeIdOrUuid?: string): void { this.dataService.getOrganizationalUnits().subscribe({ next: (data) => { this.originalTreeData = data.map((unidad) => this.convertToTreeData(unidad)); this.treeDataSource.data = [...this.originalTreeData]; if (selectedNodeIdOrUuid) { this.selectedNode = this.findNodeByIdOrUuid(this.treeDataSource.data, selectedNodeIdOrUuid); if (this.selectedNode) { this.treeControl.collapseAll(); this.expandPathToNode(this.selectedNode); this.fetchClientsForNode(this.selectedNode); } } else { this.treeControl.collapseAll(); if (this.treeDataSource.data.length > 0) { this.selectedNode = this.treeDataSource.data[0]; this.fetchClientsForNode(this.selectedNode); } else { this.selectedNode = null; this.selectedClients.data = []; this.initialLoading = false; } } }, error: (error) => { console.error('Error fetching organizational units', error); this.toastr.error('Ocurrió un error al cargar las unidades organizativas'); }, }); } expandPathToNode(node: TreeNode): void { const path: TreeNode[] = []; let currentNode: TreeNode | null = node; while (currentNode) { path.unshift(currentNode); currentNode = currentNode.id ? this.findParentNode(this.treeDataSource.data, currentNode.id) : null; } path.forEach((pathNode) => { const flatNode = this.treeControl.dataNodes?.find((n) => n.id === pathNode.id); if (flatNode) { this.treeControl.expand(flatNode); } }); } private findParentNode(treeData: TreeNode[], childId: string): TreeNode | null { for (const node of treeData) { if (node.children?.some((child) => child.id === childId)) { return node; } if (node.children && node.children.length > 0) { const parent = this.findParentNode(node.children, childId); if (parent) { return parent; } } } return null; } private findNodeByIdOrUuid(treeData: TreeNode[], identifier: string): TreeNode | null { const search = (nodes: TreeNode[]): TreeNode | null => { for (const node of nodes) { if (node.id === identifier || node.uuid === identifier) return node; if (node.children && node.children.length > 0) { const found = search(node.children); if (found) return found; } } return null; }; return search(treeData); } onNodeClick(node: TreeNode): void { this.selectedNode = node; this.fetchClientsForNode(node); } public fetchClientsForNode(node: TreeNode): void { this.isLoadingClients = true; this.http.get(`${this.baseUrl}/clients?organizationalUnit.id=${node.id}`).subscribe({ next: (response) => { this.selectedClients.data = response['hydra:member']; this.hasClients = node.hasClients ?? false; this.isLoadingClients = false; this.initialLoading = false; }, error: () => { this.isLoadingClients = false; this.initialLoading = false; } }); } addOU(event: MouseEvent, parent: TreeNode | null = null): void { event.stopPropagation(); const dialogRef = this.dialog.open(CreateOrganizationalUnitComponent, { data: { parent }, width: '900px', }); dialogRef.afterClosed().subscribe((newUnit) => { if (newUnit?.uuid) { console.log('Unidad organizativa creada:', newUnit); this.refreshData(newUnit.uuid); } }); } addClient(event: MouseEvent, organizationalUnit: TreeNode | null = null): void { event.stopPropagation(); const targetNode = organizationalUnit || this.selectedNode; const dialogRef = this.dialog.open(CreateClientComponent, { data: { organizationalUnit: targetNode }, width: '900px', }); dialogRef.afterClosed().subscribe((result) => { if (result?.client && result?.organizationalUnit) { const organizationalUnitUrl = result.organizationalUnit; const uuid = organizationalUnitUrl.split('/')[2]; const parentNode = this.findNodeByIdOrUuid(this.treeDataSource.data, uuid); if (parentNode) { this.refreshData(parentNode.uuid); } } }); } addMultipleClients(event: MouseEvent, organizationalUnit: TreeNode | null = null): void { event.stopPropagation(); const targetNode = organizationalUnit || this.selectedNode; const dialogRef = this.dialog.open(CreateMultipleClientComponent, { data: { organizationalUnit: targetNode }, width: '900px', }); dialogRef.afterClosed().subscribe((result) => { if (result?.success) { const organizationalUnitUrl = result.organizationalUnit; const uuid = organizationalUnitUrl.split('/')[2]; const parentNode = this.findNodeByIdOrUuid(this.treeDataSource.data, uuid); if (parentNode) { console.log('Nodo padre encontrado para actualización:', parentNode); this.refreshData(parentNode.uuid); } else { console.error('No se encontró el nodo padre después de la creación masiva.'); } } }); } onEditNode(event: MouseEvent, node: TreeNode | null): void { event.stopPropagation(); const uuid = node ? this.extractUuid(node['@id']) : null; if (!uuid) return; const dialogRef = node?.type !== NodeType.Client ? this.dialog.open(EditOrganizationalUnitComponent, { data: { uuid }, width: '900px' }) : this.dialog.open(EditClientComponent, { data: { uuid }, width: '900px' }); dialogRef.afterClosed().subscribe(() => { if (node) { this.refreshData(node.id); } }); } onDeleteClick(event: MouseEvent, node: TreeNode | null): void { event.stopPropagation(); const uuid = node ? this.extractUuid(node['@id']) : null; if (!uuid) return; if (!node) return; const dialogRef = this.dialog.open(DeleteModalComponent, { width: '400px', data: { name: node.name }, }); dialogRef.afterClosed().subscribe((result) => { if (result === true) { this.deleteEntityorClient(uuid, node?.type); } }); } private deleteEntityorClient(uuid: string, type: string): void { if (!this.selectedNode) return; const parentNode = this.selectedNode?.id ? this.findParentNode(this.treeDataSource.data, this.selectedNode.id) : null; this.dataService.deleteElement(uuid, type).subscribe({ next: () => { const entityType = type === NodeType.Client ? 'Cliente' : 'Entidad'; const verb = type === NodeType.Client ? 'eliminado' : 'eliminada'; this.toastr.success(`${entityType} ${verb} exitosamente`); if (type === NodeType.Client) { this.refreshData(this.selectedNode?.id); } else if (parentNode) { this.refreshData(parentNode.id); } else { this.refreshData(); } }, error: (error) => { console.error('Error deleting entity:', error); const entityType = type === NodeType.Client ? 'cliente' : 'entidad'; this.toastr.error(`Error al eliminar el ${entityType}`); }, }); } onEditClick(event: MouseEvent, type: string, uuid: string): void { event.stopPropagation(); const dialogRef = type !== NodeType.Client ? this.dialog.open(EditOrganizationalUnitComponent, { data: { uuid }, width: '900px' }) : this.dialog.open(EditClientComponent, { data: { uuid }, width: '900px' }); dialogRef.afterClosed().subscribe(() => { this.refreshData(this.selectedNode?.id); }); } onRoomMap(room: TreeNode | null): void { if (!room || !room['@id']) return; this.subscriptions.add( this.http.get<{ clients: Client[] }>(`${this.baseUrl}${room['@id']}`).subscribe( (response) => { this.dialog.open(ClassroomViewDialogComponent, { width: '90vw', data: { clients: response.clients }, }); }, (error) => { console.error('Error fetching room data:', error); } ) ); } executeCommand(command: Command, selectedNode: TreeNode | null): void { if (!selectedNode) { this.toastr.error('No hay un nodo seleccionado.'); return; } else { this.toastr.success(`Ejecutando comando: ${command.name} en ${selectedNode.name}`); } } onShowClientDetail(event: MouseEvent, client: Client): void { event.stopPropagation(); this.router.navigate(['clients', client.uuid], { state: { clientData: client } }); } onShowDetailsClick(event: MouseEvent, data: TreeNode | null): void { event.stopPropagation(); if (data && data.type !== NodeType.Client) { this.dialog.open(ShowOrganizationalUnitComponent, { data: { data }, width: '700px' }); } else { if (data) { this.router.navigate(['clients', this.extractUuid(data['@id'])], { state: { clientData: data } }); } } } openBottomSheet(): void { this.bottomSheet.open(LegendComponent); } iniciarTour(): void { this.joyrideService.startTour({ steps: ['groupsTitleStepText', 'filtersPanelStep', 'addStep', 'keyStep', 'tabsStep'], showPrevButton: true, themeColor: '#3f51b5', }); } hasChild = (_: number, node: FlatNode): boolean => node.expandable; isLeafNode = (_: number, node: FlatNode): boolean => !node.expandable; filterTree(searchTerm: string): void { const expandPaths: TreeNode[][] = []; const filterNodes = (nodes: TreeNode[], parentPath: TreeNode[] = []): TreeNode[] => { return nodes .map((node) => { const matchesName = node.name.toLowerCase().includes(searchTerm.toLowerCase()); const filteredChildren = node.children ? filterNodes(node.children, [...parentPath, node]) : []; if (matchesName) { expandPaths.push([...parentPath, node]); return { ...node, children: node.children, } as TreeNode; } else if (filteredChildren.length > 0) { return { ...node, children: filteredChildren, } as TreeNode; } return null; }) .filter((node): node is TreeNode => node !== null); }; if (!searchTerm) { this.treeDataSource.data = [...this.originalTreeData]; this.treeControl.collapseAll(); } else { this.treeDataSource.data = filterNodes(this.originalTreeData); expandPaths.forEach((path) => this.expandPath(path)); } } private expandPath(path: TreeNode[]): void { path.forEach((pathNode) => { const flatNode = this.treeControl.dataNodes?.find((n) => n.id === pathNode.id); if (flatNode) { this.treeControl.expand(flatNode); } }); } onTreeFilterInput(event: Event): void { const input = event.target as HTMLInputElement; const searchTerm = input?.value.trim() || ''; this.filterTree(searchTerm); } onClientFilterInput(event: Event): void { const input = event.target as HTMLInputElement; const searchTerm = input?.value || ''; this.filterClients(searchTerm); } filterClients(searchTerm: string): void { this.searchTerm = searchTerm.trim().toLowerCase(); this.selectedClients.filter = this.searchTerm; } public setSelectedNode(node: TreeNode): void { this.selectedNode = node; } getStatus(client: Client, node: any): void { if (!client.uuid || !client['@id']) return; this.syncingClientId = client.uuid; this.syncStatus = true; this.subscriptions.add( this.http.post(`${this.baseUrl}${client['@id']}/agent/status`, {}).subscribe( () => { this.toastr.success('Cliente actualizado correctamente'); this.syncStatus = false; this.syncingClientId = null; this.refreshData() }, () => { this.toastr.error('Error de conexión con el cliente'); this.syncStatus = false; this.syncingClientId = null; this.refreshData() } ) ); } isAllSelected() { const numSelected = this.selection.selected.length; const numRows = this.selectedClients.data.length; return numSelected === numRows; } toggleAllRows() { if (this.isAllSelected()) { this.selection.clear(); this.arrayClients = [] return; } this.selection.select(...this.selectedClients.data); this.arrayClients = [...this.selection.selected]; } toggleRow(row: any) { this.selection.toggle(row); this.updateSelectedClients(); } updateSelectedClients() { this.arrayClients = [...this.selection.selected]; } getClientPath(client: Client): string { const path: string[] = []; let currentNode: TreeNode | null = this.findNodeByIdOrUuid(this.treeDataSource.data, client.organizationalUnit.uuid); while (currentNode) { path.unshift(currentNode.name); currentNode = currentNode.id ? this.findParentNode(this.treeDataSource.data, currentNode.id) : null; } return path.join(' / '); } private extractUuid(idPath: string | undefined): string | null { return idPath ? idPath.split('/').pop() || null : null; } clearTreeSearch(inputElement: HTMLInputElement): void { inputElement.value = ''; this.filterTree(''); } clearClientSearch(inputElement: HTMLInputElement): void { inputElement.value = ''; this.filterClients(''); } }