<div class="groups-container">
  <!-- HEADER -->
  <div class="header-container" joyrideStep="tabsStep" text="{{ 'tabsStepText' | translate }}">
    <button mat-icon-button color="primary" (click)="iniciarTour()">
      <mat-icon>help</mat-icon>
    </button>
    <div class="header-container-title">
      <h2 joyrideStep="groupsTitleStepText" text="{{ 'groupsTitleStepText' | translate }}">
        {{ 'adminGroupsTitle' | translate }}
      </h2>
    </div>
    <div class="groups-button-row" joyrideStep="addStep" text="{{ 'groupsAddStepText' | translate }}">
      <button class="action-button" (click)="addOU($event)"
        matTooltip="{{ 'newOrganizationalUnitTooltip' | translate }}" matTooltipShowDelay="1000">
        {{ 'newOrganizationalUnitButton' | translate }}
      </button>
      <button class="action-button" [matMenuTriggerFor]="menuClients">{{ 'newClientButton' | translate }}</button>
      <mat-menu #menuClients="matMenu">
        <button mat-menu-item (click)="addClient($event)">{{ 'newSingleClientButton' | translate }}</button>
        <button mat-menu-item (click)="addMultipleClients($event)">{{ 'newMultipleClientButton' | translate }}</button>
      </mat-menu>

      <button class="ordinary-button" (click)="openBottomSheet()" joyrideStep="keyStep"
        text="{{ 'keyStepText' | translate }}" matTooltipShowDelay="1000">
        {{ 'legendButton' | translate }}
      </button>
    </div>
  </div>

  <div *ngIf="initialLoading; else contentTemplate">
    <app-loading [isLoading]="initialLoading"></app-loading>
  </div>

  <!-- MAIN VIEW -->
  <ng-template #contentTemplate>
    <div class="main-container">

      <!-- FILTERS AND TREE VIEW -->
      <div class="filters-and-tree-container">

        <!-- Filters -->
        <div class="filters-panel" joyrideStep="filtersPanelStep" text="{{ 'filtersPanelStepText' | translate }}">
          <div class="filters-container">
            <mat-form-field class="filter-form-field" appearance="outline">
              <mat-label>{{ 'searchTree' | translate }}</mat-label>
              <input matInput #treeSearchInput (input)="onTreeFilterInput($event)"
                placeholder="Centro, aula, grupos ..." />
              <button *ngIf="treeSearchInput.value" mat-icon-button matSuffix aria-label="Clear tree search"
                (click)="clearTreeSearch(treeSearchInput)">
                <mat-icon>close</mat-icon>
              </button>
            </mat-form-field>
            <mat-form-field class="filter-form-field" appearance="outline">
              <mat-label>{{ 'searchClient' | translate }}</mat-label>
              <input matInput #clientSearchInput (input)="onClientFilterInput($event)"
                placeholder="Nombre, IP, estado o MAC" />
              <button *ngIf="clientSearchInput.value" mat-icon-button matSuffix aria-label="Clear client search"
                (click)="clearClientSearch(clientSearchInput)">
                <mat-icon>close</mat-icon>
              </button>
            </mat-form-field>
            <mat-form-field class="form-field search-select" appearance="outline">
              <mat-select placeholder="Buscar por estado..." #clientSearchStatusInput
                (selectionChange)="onClientFilterStatusInput($event.value)">
                <mat-option *ngFor="let option of status" [value]="option.value">
                  {{ option.name }}
                </mat-option>
              </mat-select>
              <button *ngIf="clientSearchStatusInput.value" mat-icon-button matSuffix aria-label="Clear tree search"
                (click)="clearStatusFilter($event, clientSearchStatusInput)">
                <mat-icon>close</mat-icon>
              </button>
            </mat-form-field>

            <mat-divider style="padding-top: 10px;"></mat-divider>

            <!-- Funcionalidad actualmente deshabilitada-->
            <!-- <mat-form-field appearance="outline">
            <mat-select (selectionChange)="loadSelectedFilter($event.value)" placeholder="Cargar filtro" disabled>
              <mat-option *ngFor="let savedFilter of savedFilterNames" [value]="savedFilter">
                {{ savedFilter[0] }}
              </mat-option>
            </mat-select>
          </mat-form-field>
          <mat-form-field appearance="outline">
            <mat-label>{{ 'filterByType' | translate }}</mat-label>
            <mat-select [(value)]="selectedTreeFilter" (selectionChange)="filterTree(searchTerm, $event.value)" disabled>
              <mat-option [value]=""> {{ 'all' | translate }} </mat-option>
              <mat-option value="classrooms-group">{{ 'classroomsGroup' | translate }}</mat-option>
              <mat-option value="classroom">{{ 'classrooms' | translate }}</mat-option>
              <mat-option value="group">{{ 'computerGroups' | translate }}</mat-option>
            </mat-select>
          </mat-form-field> -->
          </div>
        </div>

        <!-- Tree -->
        <div class="tree-container">
          <mat-tree [dataSource]="treeDataSource" [treeControl]="treeControl">
            <mat-tree-node [ngClass]="{'selected-node': selectedNode?.id === node.id}"
              *matTreeNodeDef="let node; when: hasChild" matTreeNodePadding (click)="onNodeClick($event, node)">
              <button mat-icon-button matTreeNodeToggle [disabled]="!node.expandable"
                [ngClass]="{'disabled-toggle': !node.expandable}">
                <mat-icon>{{ treeControl.isExpanded(node) ? 'expand_more' : 'chevron_right' }}</mat-icon>
              </button>
              <mat-icon class="node-icon {{ node.type }}">
                {{
                node.type === 'organizational-unit' ? 'apartment'
                : node.type === 'classrooms-group' ? 'meeting_room'
                : node.type === 'classroom' ? 'school'
                : node.type === 'clients-group' ? 'lan'
                : node.type === 'client' ? 'computer'
                : 'group'
                }}
              </mat-icon>
              <span>{{ node.name }}</span>
              <button mat-icon-button [matMenuTriggerFor]="menuNode" (click)="onMenuClick($event, node)">
                <mat-icon>more_vert</mat-icon>
              </button>
            </mat-tree-node>
            <mat-tree-node [ngClass]="{'selected-node': selectedNode?.id === node.id}"
              *matTreeNodeDef="let node; when: isLeafNode" matTreeNodePadding (click)="onNodeClick($event, node)">
              <button mat-icon-button matTreeNodeToggle [disabled]="true" class="disabled-toggle"></button>
              <mat-icon style="color: green;">
                {{
                node.type === 'organizational-unit' ? 'apartment'
                : node.type === 'classrooms-group' ? 'meeting_room'
                : node.type === 'classroom' ? 'school'
                : node.type === 'clients-group' ? 'lan'
                : node.type === 'client' ? 'computer'
                : 'group'
                }}
              </mat-icon>
              <span>{{ node.name }}</span>
              <ng-container *ngIf="node.type === 'client'">
                <span> - IP: {{ node.ip }}</span>
              </ng-container>
              <button mat-icon-button [matMenuTriggerFor]="menuNode" (click)="onMenuClick($event, node)">
                <mat-icon>more_vert</mat-icon>
              </button>
            </mat-tree-node>
          </mat-tree>
        </div>

        <!-- Tree node actions -->
        <mat-menu restoreFocus=false #commandMenu="matMenu">
          <button mat-menu-item *ngFor="let command of commands" (click)="executeCommand(command, selectedNode)">
            <span>{{ command.name }}</span>
          </button>
        </mat-menu>
        <mat-menu #menuNode="matMenu">
          <button mat-menu-item (click)="onShowDetailsClick($event, selectedNode)">
            <mat-icon matTooltip="{{ 'viewUnitTooltip' | translate }}" matTooltipHideDelay="0">visibility</mat-icon>
            <span>{{ 'viewUnitMenu' | translate }}</span>
          </button>
          <button *ngIf="selectedNode?.type === 'classroom'" mat-menu-item (click)="onRoomMap(selectedNode)">
            <mat-icon>map</mat-icon>
            <span>{{ 'roomMap' | translate }}</span>
          </button>
          <button mat-menu-item (click)="addClient($event, selectedNode)">
            <mat-icon>add</mat-icon>
            <span>{{ 'newSingleClientButton' | translate }}</span>
          </button>
          <button mat-menu-item (click)="addMultipleClients($event, selectedNode)">
            <mat-icon>playlist_add</mat-icon>
            <span>{{ 'newMultipleClientButton' | translate }}</span>
          </button>
          <button mat-menu-item (click)="addOU($event, selectedNode)">
            <mat-icon>account_tree</mat-icon>
            <span>{{ 'addOrganizationalUnit' | translate }}</span>
          </button>
          <button mat-menu-item (click)="onEditNode($event, selectedNode)">
            <mat-icon>edit</mat-icon>
            <span>{{ 'edit' | translate }}</span>
          </button>
          <button mat-menu-item (click)="onDeleteClick($event, selectedNode)">
            <mat-icon>delete</mat-icon>
            <span>{{ 'delete' | translate }}</span>
          </button>
        </mat-menu>

      </div>

      <mat-divider [vertical]="true"></mat-divider>

      <!-- CLIENTS -->
      <div class="clients-container">

        <!-- CLIENTS HEADER -->
        <div class="clients-view-header">
          <div>
            <span [ngStyle]="{ visibility: isLoadingClients ? 'hidden' : 'visible' }" class="clients-title-name">
              {{ 'clients' | translate }}
              <strong>{{ selectedNode?.name }}</strong>
            </span>
          </div>
          <div class="view-type-container">
            <app-execute-command [clientData]="selection.selected" [buttonType]="'text'"
              [buttonText]="'Ejecutar comandos'" [disabled]="selection.selected.length === 0"></app-execute-command>
            <mat-button-toggle-group name="viewType" aria-label="View Type" [hideSingleSelectionIndicator]="true"
              (change)="toggleView($event.value)">
              <mat-button-toggle value="list" [disabled]="currentView === 'list'">
                <mat-icon>list</mat-icon> {{ 'Vista Lista' | translate }}
              </mat-button-toggle>
              <mat-button-toggle value="card" [disabled]="currentView === 'card'">
                <mat-icon>grid_view</mat-icon> {{ 'Vista Tarjeta' | translate }}
              </mat-button-toggle>
            </mat-button-toggle-group>
          </div>
        </div>

        <app-loading [isLoading]="isLoadingClients"></app-loading>

        <!-- CLIENTS VIEWS-->
        <div class="clients-view" *ngIf="!isLoadingClients">
          <div *ngIf="hasClients; else noClientsTemplate">

            <!-- Cards view -->
            <div *ngIf="currentView === 'card'">
              <section class="cards-view">
                <mat-checkbox class="cards-select-all" (change)="toggleAllCards()"
                  [checked]="selection.hasValue() && isAllSelected()"
                  [indeterminate]="selection.hasValue() && !isAllSelected()">
                </mat-checkbox>
                <div class="clients-grid">
                  <div *ngFor="let client of arrayClients" class="client-item">
                    <div class="client-card">
                      <mat-checkbox (click)="$event.stopPropagation()" (change)="toggleRow(client)"
                        [checked]="selection.isSelected(client)" [disabled]="client.status === 'busy'">
                      </mat-checkbox>
                      <img style="margin-top: 0.5em;" [src]="'assets/images/ordenador_' + client.status + '.png'" alt="Client Icon"
                        class="client-image" />

                      <div class="client-details">
                        <span class="client-name">{{ client.name }}</span>
                        <span class="client-ip">{{ client.ip }}</span>
                        <span class="client-ip">{{ client.mac }}</span>
                        <div class="action-icons">

                          <app-execute-command [clientData]="[client]" [buttonType]="'icon'" [icon]="'terminal'"
                            [disabled]="selection.selected.length > 1 || (selection.selected.length === 1 && !selection.isSelected(client))"></app-execute-command>

                          <button
                            [disabled]="selection.selected.length > 1 || (selection.selected.length === 1 && !selection.isSelected(client))"
                            mat-icon-button [matMenuTriggerFor]="clientMenu" color="primary">
                            <mat-icon>more_vert</mat-icon>
                          </button>

                          <span class="sync-spinner" *ngIf="syncStatus && syncingClientId === client.uuid">
                            <mat-spinner diameter="24"></mat-spinner>
                          </span>

                          <mat-menu #clientMenu="matMenu">
                            <button mat-menu-item (click)="onEditClick($event, client.type, client.uuid)">
                              <mat-icon>edit</mat-icon>
                              <span>{{ 'edit' | translate }}</span>
                            </button>
                            <button mat-menu-item (click)="onShowClientDetail($event, client)">
                              <mat-icon>visibility</mat-icon>
                              <span>{{ 'viewDetails' | translate }}</span>
                            </button>
                            <button mat-menu-item *ngIf="(!syncStatus || syncingClientId !== client.uuid)"
                              (click)="getStatus(client, selectedNode)">
                              <mat-icon>sync</mat-icon>
                              <span>{{ 'sync' | translate }}</span>
                            </button>
                            <button mat-menu-item (click)="onDeleteClick($event, client)">
                              <mat-icon>delete</mat-icon>
                              <span>{{ 'delete' | translate }}</span>
                            </button>
                          </mat-menu>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </section>
              <div class="paginator-container">
                <mat-paginator class="cards-paginator" [length]="length" [pageSize]="itemsPerPage" [pageIndex]="page"
                  [pageSizeOptions]="[5, 10, 20, 50, 100]" (page)="onPageChange($event)">
                </mat-paginator>
              </div>
            </div>

            <!-- List view -->
            <div *ngIf="currentView === 'list'" class="list-view">
              <section class="clients-table" tabindex="0">
                <table mat-table matSort [dataSource]="selectedClients">
                  <ng-container matColumnDef="select">
                    <th mat-header-cell *matHeaderCellDef>
                      <mat-checkbox (change)="$event ? toggleAllRows() : null"
                        [checked]="selection.hasValue() && isAllSelected()"
                        [indeterminate]="selection.hasValue() && !isAllSelected()">
                      </mat-checkbox>
                    </th>
                    <td mat-cell *matCellDef="let row">
                      <mat-checkbox (click)="$event.stopPropagation()" (change)="toggleRow(row)"
                        [checked]="selection.isSelected(row)" [disabled]="row.status === 'busy'">
                      </mat-checkbox>
                    </td>
                  </ng-container>
                  <ng-container matColumnDef="status">
                    <th mat-header-cell *matHeaderCellDef mat-sort-header> {{ 'status' | translate }} </th>
                    <td mat-cell *matCellDef="let client" matTooltip="{{ getClientPath(client) }}"
                      matTooltipPosition="left" matTooltipShowDelay="500">
                      <div class="client-status-container">
                        <img [src]="'assets/images/ordenador_' + client.status + '.png'" alt="Client Icon"
                          class="client-image" />
                        <span *ngIf="syncStatus && syncingClientId === client.uuid">
                          <mat-spinner diameter="24"></mat-spinner>
                        </span>
                      </div>
                    </td>
                  </ng-container>

                  <ng-container matColumnDef="name">
                    <th mat-header-cell *matHeaderCellDef mat-sort-header> {{ 'name' | translate }} </th>
                    <td mat-cell *matCellDef="let client" matTooltip="{{ getClientPath(client) }}"
                      matTooltipPosition="left" matTooltipShowDelay="500">
                      <p style="font-weight: 500;">{{ client.name }}</p>
                    </td>
                  </ng-container>
                  <ng-container matColumnDef="ip">
                    <th mat-header-cell *matHeaderCellDef mat-sort-header>IP </th>
                    <td mat-cell *matCellDef="let client" matTooltip="{{ getClientPath(client) }}"
                      matTooltipPosition="left" matTooltipShowDelay="500">
                      {{ client.ip }}
                    </td>
                  </ng-container>
                  <ng-container matColumnDef="oglive">
                    <th mat-header-cell *matHeaderCellDef mat-sort-header> OG Live </th>
                    <td mat-cell *matCellDef="let client"> {{ client.ogLive?.date | date }} </td>
                  </ng-container>

                  <ng-container matColumnDef="maintenace">
                    <th mat-header-cell *matHeaderCellDef mat-sort-header> {{ 'maintenance' | translate }} </th>
                    <td mat-cell *matCellDef="let client"> {{ client.maintenance }} </td>
                  </ng-container>
                  <ng-container matColumnDef="subnet">
                    <th mat-header-cell *matHeaderCellDef mat-sort-header> {{ 'subnet' | translate }} </th>
                    <td mat-cell *matCellDef="let client"> {{ client.subnet }} </td>
                  </ng-container>
                  <ng-container matColumnDef="pxeTemplate">
                    <th mat-header-cell *matHeaderCellDef mat-sort-header> {{ 'pxeTemplate' | translate }} </th>
                    <td mat-cell *matCellDef="let client"> {{ client.template?.name }} </td>
                  </ng-container>

                  <ng-container matColumnDef="parentName">
                    <th mat-header-cell *matHeaderCellDef mat-sort-header> {{ 'parent' | translate }} </th>
                    <td mat-cell *matCellDef="let client"> {{ client.parentName }} </td>
                  </ng-container>

                  <ng-container matColumnDef="actions">
                    <th mat-header-cell *matHeaderCellDef mat-sort-header> {{ 'actions' | translate }} </th>
                    <td mat-cell *matCellDef="let client">
                      <button
                        [disabled]="selection.selected.length > 1 || (selection.selected.length === 1 && !selection.isSelected(client))"
                        mat-icon-button [matMenuTriggerFor]="clientMenu" color="primary">
                        <mat-icon>more_vert</mat-icon>
                      </button>
                      <app-execute-command [clientData]="[client]" [buttonType]="'icon'" [icon]="'terminal'"
                        [disabled]="selection.selected.length > 1 || (selection.selected.length === 1 && !selection.isSelected(client))"></app-execute-command>
                      <mat-menu #clientMenu="matMenu">
                        <button mat-menu-item (click)="onEditClick($event, client.type, client.uuid)">
                          <mat-icon>edit</mat-icon>
                          <span>{{ 'edit' | translate }}</span>
                        </button>
                        <button mat-menu-item (click)="onShowClientDetail($event, client)">
                          <mat-icon>visibility</mat-icon>
                          <span>{{ 'viewDetails' | translate }}</span>
                        </button>
                        <button mat-menu-item (click)="getStatus(client, selectedNode)">
                          <mat-icon>sync</mat-icon>
                          <span>{{ 'sync' | translate }}</span>
                        </button>
                        <button mat-menu-item (click)="onDeleteClick($event, client)">
                          <mat-icon>delete</mat-icon>
                          <span>{{ 'delete' | translate }}</span>
                        </button>
                      </mat-menu>
                    </td>
                  </ng-container>
                  <tr mat-header-row style="background-color: #f3f3f3;"
                    *matHeaderRowDef="displayedColumns; sticky: true"></tr>
                  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
                </table>
              </section>
              <mat-paginator class="list-paginator" [length]="length" [pageSize]="itemsPerPage" [pageIndex]="page"
                [pageSizeOptions]="pageSizeOptions" (page)="onPageChange($event)">
              </mat-paginator>
            </div>

          </div>

          <!-- No clients view -->
          <ng-template #noClientsTemplate>
            <div *ngIf="!initialLoading" class="no-clients-info">
              <span>{{ 'noClients' | translate }}</span>
              <mat-icon>error_outline</mat-icon>
            </div>
          </ng-template>
        </div>
      </div>

    </div>
  </ng-template>
</div>
