<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">Regresar</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">Gestión de las familias</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>
            <mat-icon *ngIf="txtBuscador.length > 0" class="clickable" matTooltip="Eliminar filtro"
            (click)="applyFilter('', 'EST')" matSuffix>close</mat-icon>
          </mat-form-field>
          <div class="override-buttons">
            <button mat-flat-button color="primary " class="mr-10 animated fadeIn " (click)="openFamilyForm('new')" 
            [disabled]="!familyRegisterEnabled" *ngIf="!btnSmall "  >
              <mat-icon>add</mat-icon> Registrar familia
            </button>
            <button mat-mini-fab color="primary" class="override_no_shadow mr-10 animated fadeIn raised-button-background" (click)="openFamilyForm('new')" matTooltip="Registrar familia" 
            [disabled]="!familyRegisterEnabled" *ngIf="btnSmall">
              <mat-icon>add</mat-icon>
            </button>
            <button mat-mini-fab class="orange_primary_background white_font override_no_shadow mr-10" matTooltip="Actualizar datos" 
            (click)="getData()">
              <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="animated fadeIn" *ngIf="!familyConsultEnabled && !isLoading && !hasError">
        <h3 class="ml-20"><b>Su perfil no cuenta con los permisos necesarios para visualizar la información de esta sección.</b></h3>
      </div>

      <div class="override-table" [ngClass]="{ hidden: isLoading || hasError }">
        <table mat-table [dataSource]="dataSource!" matSort class="animated fadeIn" 
        [style.display]="!isLoading && familyConsultEnabled && !hasError ? 'revert' : 'none'">
          <ng-container matColumnDef="CODIGO">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Código</th>
            <td mat-cell *matCellDef="let row">{{ row.CODIGOFAMILIA }}</td>
          </ng-container>
          
          <ng-container matColumnDef="NOMBRE">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Nombre</th>
            <td mat-cell *matCellDef="let row">{{ row.NOMBREFAMILIA }}</td>
          </ng-container>
          
          <ng-container matColumnDef="COLOR">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Color</th>
            <td mat-cell *matCellDef="let row">
              <div class="w-100 center_horizontal">
                <div class="family-color-indicator" [style.background]="row.COLORFAMILIA"></div>
              </div>
            </td>
          </ng-container>
          
          <ng-container matColumnDef="SUBFAMILIAS">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Cantidad de subfamilias</th>
            <td mat-cell *matCellDef="let row">{{ row.SUBFAMILIAS }}</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="USRMOD">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Usuario última modificación</th>
            <td mat-cell *matCellDef="let row">{{ row.USRMOD == null ? row.USRREG : row.USRMOD }}</td>
          </ng-container>
          
          <ng-container matColumnDef="FECMOD">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Fecha última modificación</th>
            <td mat-cell *matCellDef="let row">{{ row.FECMOD == null ? row.FECREG : row.FECMOD }}</td>
          </ng-container>
          
          <ng-container matColumnDef="ACCIONES">
            <th mat-header-cell *matHeaderCellDef>Acciones</th>
            <td mat-cell *matCellDef="let row">
              <button mat-mini-fab color="primary grey_dark_font transparent_background" matTooltip="Configuración" class="override_no_shadow transparent_background gray_dark_font" [matMenuTriggerFor]="menu" 
              [disabled]="row.ESTADO == 'Eliminado'">
                <mat-icon>more_vert</mat-icon>
              </button>
              <mat-menu #menu>
                <button mat-menu-item class="override_no_shadow mr-4" color="accent" (click)="openFamilyForm('edit', row)" 
                [disabled]="row.ESTADO == 'Eliminado'">
                  <mat-icon>edit</mat-icon> Editar familia
                </button>
                <button mat-menu-item class="override_no_shadow mr-4" color="primary" [disabled]="row.ESTADO == 'Eliminado'" 
                (click)="manageSubfamilies(row.CODIGOFAMILIA, row.NOMBREFAMILIA)">
                  <mat-icon>visibility</mat-icon> Ver subfamilias
                </button>
                <button mat-menu-item class="override_no_shadow" color="primary" (click)="alertDelete(row.CODIGOFAMILIA)" 
                [disabled]="row.ESTADO == 'Eliminado'">
                  <mat-icon>delete</mat-icon> Eliminar familia
                </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>