<main class="main-container animated fadeIn prevent-select" (window:resize)="onResize()">

  <header id="navigation" class="btn-navigate mb-8">
    <div class="prev-page prevent-select raised-button-background  mat-elevation-z8" (click)="goBack(1)" matRipple>
      <mat-icon style="color: white;">arrow_back</mat-icon>
      <div class="page-name ml-4">Gestión de mantenimiento correctivo</div>
    </div>
  </header>

  <mat-card class="override-card override-elevation-z8">
    <section class="override-section">
      <nav class="override-nav">
        <mat-card-title class="mat-card-title centre">Planes de mantenimiento correctivo</mat-card-title>
        <div class="override-actions">
          <mat-form-field appearance="outline" class="w-300p mr-8">
            <mat-label>Buscador</mat-label>
            <input matInput [(ngModel)]="txtBuscador" (keyup)="applyFilter($event,'INP')"
            placeholder="Ingrese la palabra a buscar"/>
            <mat-icon *ngIf="txtBuscador.length === 0" matSuffix>search</mat-icon>
            <button mat-icon-button *ngIf="txtBuscador.length > 0" matTooltip="Eliminar filtro"
            (click)="applyFilter('', 'EST')" matSuffix>
              <mat-icon>close</mat-icon>
            </button>
          </mat-form-field>
          <div class="override-buttons">
            <button mat-flat-button  class="mr-10 animated fadeIn raised-button-background " (click)="openMaintenancePlan('nuevo', null)">
              <mat-icon>add</mat-icon> Registrar plan de mantenimiento
            </button>
            <button mat-mini-fab class="orange_primary_background white_font override_no_shadow mr-10" matTooltip="Actualizar datos" 
            (click)="getMaintenancePlans()">
              <mat-icon>refresh</mat-icon>
            </button>
          </div>
        </div>
      </nav>

      <div class="is-loading animated fadeIn fast" *ngIf="isLoading">
        <mat-spinner align="center"></mat-spinner>
        <h3>Cargando datos ...</h3>
      </div>

      <div class="has-error animated fadeIn fast" *ngIf="!isLoading && hasError">
        <mat-icon  class="red_primary_font">error</mat-icon>
        <h3>{{ errorStr }}</h3>
      </div>

      <div class="override-table animated fadeIn" [ngClass]="{ hidden: isLoading || hasError }">
        <table mat-table [dataSource]="dataSource!" matSort 
        [style.display]="!isLoading && !hasError ? 'revert' : 'none'">
          <ng-container matColumnDef="ID">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>#</th>
            <td mat-cell *matCellDef="let row">Plan #{{ row.ID_PLAN }}</td>
          </ng-container>
          
          <ng-container matColumnDef="EQUIP">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Equipamiento / LRU</th>
            <td mat-cell *matCellDef="let row">{{ row.LRU == null ? row.EQUIPAMIENTO : row.LRU }}</td>
          </ng-container>
          
          <ng-container matColumnDef="TIEINM">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Tiempo de inmovilización estimado</th>
            <td mat-cell *matCellDef="let row">{{ row.TIEMPO_INMOVILIZACION_ESTIMADO }} h</td>
          </ng-container>
          
          <ng-container matColumnDef="DURATOT">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Duración total</th>
            <td mat-cell *matCellDef="let row">{{ row.DURACION_TOTAL }} h</td>
          </ng-container>
          
          <ng-container matColumnDef="CLASIF">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Clasificación</th>
            <td mat-cell *matCellDef="let row">{{ row.CLASIFICACION }}</td>
          </ng-container>
          
          <ng-container matColumnDef="CONTADOR">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Contador</th>
            <td mat-cell *matCellDef="let row">Contador #{{ row.CONTADOR }}</td>
          </ng-container>
          
          <ng-container matColumnDef="ACTIVADOR">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Activador</th>
            <td mat-cell *matCellDef="let row">Activador #{{ row.ACTIVADOR }}</td>
          </ng-container>
          
          <ng-container matColumnDef="PRIORIDAD">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Prioridad</th>
            <td mat-cell *matCellDef="let row">
              <span style="font-weight: 500;" [style.color]="row.PRIORIDAD_OBJ.color">
                {{ row.PRIORIDAD_OBJ.label }}
              </span>
            </td>
          </ng-container>
          
          <ng-container matColumnDef="ESTADO">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Estado</th>
            <td mat-cell *matCellDef="let row">{{ row.ESTADO }}</td>
          </ng-container>
          
          <ng-container matColumnDef="ACCIONES">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Acciones</th>
            <td mat-cell *matCellDef="let row">
              <button mat-mini-fab color="primary" class="override_no_shadow transparent_background gray_dark_font" matTooltip="Ver opciones" [matMenuTriggerFor]="menu" 
              [disabled]="row.ESTADO == 'Eliminado'">
                <mat-icon>more_vert</mat-icon>
              </button>
              <mat-menu #menu>
                <button mat-menu-item (click)="openMaintenancePlan('editar', row.ID_PLAN)">
                  <mat-icon>edit</mat-icon>
                  <span>Editar plan</span>
                </button>
                <button mat-menu-item (click)="openPlanDetails(row.ID_PLAN)">
                  <mat-icon>visibility</mat-icon>
                  <span>Ver detalles</span>
                </button>
                <button mat-menu-item (click)="openConfirmDelete(row.ID_PLAN)">
                  <mat-icon>delete</mat-icon>
                  <span>Eliminar plan</span>
                </button>
              </mat-menu>
            </td>
          </ng-container>

          <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
          <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>

          <tr class="mat-row" *matNoDataRow>
            <td class="mat-cell p-80 centre" colspan="100%">
              <object data="assets/img/empty_data.svg" width="300"></object>
              <h2>Sin datos</h2>
            </td>
          </tr>
        </table>
      </div>
    </section>
    <mat-paginator [pageSizeOptions]="[10, 50, 100]" class="override-paginator"></mat-paginator>
  </mat-card>
</main>