<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 indigo_primary_background mat-elevation-z8" [routerLink]="['/sam/USPE']" matRipple>
      <mat-icon style="color: white;">arrow_back</mat-icon>
      <div class="page-name ml-4">Usuarios y perfiles</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">Administración de usuarios</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-raised-button color="primary" class="override-no-shadow mr-10 animated fadeIn" (click)="openUser('insert', '')" *ngIf="!btnSmall">
              <mat-icon>add</mat-icon> Crear nueva cuenta
            </button>
            <button mat-mini-fab color="primary" class="override-no-shadow mr-10 animated fadeIn" matTooltip="Crear nueva cuenta" (click)="openUser('insert', '')"
            *ngIf="btnSmall">
              <mat-icon>add</mat-icon>
            </button>
            <button mat-mini-fab color="info" class="mr-10 override-no-shadow" matTooltip="Actualizar Datos" (click)="getData()">
              <mat-icon>refresh</mat-icon>
            </button>
          </div>
        </div>
      </nav>

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

      <div class="override-table">
        <table mat-table [dataSource]="dataSource!" matSort class="animated fadeIn" [style.display]="isLoading ? 'none' : 'revert'">
          <ng-container matColumnDef="ID">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>ID</th>
            <td mat-cell *matCellDef="let row">{{ row.IDUSUARIO }}</td>
          </ng-container>

          <ng-container matColumnDef="NOMBRE">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Nombre</th>
            <td mat-cell *matCellDef="let row">{{ row.NOMBRE }}</td>
          </ng-container>

          <ng-container matColumnDef="APEPAT">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Primer Apellido</th>
            <td mat-cell *matCellDef="let row">{{ row.APEPAT }}</td>
          </ng-container>

          <ng-container matColumnDef="APEMAT">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Segundo Apellido</th>
            <td mat-cell *matCellDef="let row">{{ row.APEMAT }}</td>
          </ng-container>

          <ng-container matColumnDef="PERFIL">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Perfil</th>
            <td mat-cell *matCellDef="let row">{{ row.PERFIL }}</td>
          </ng-container>

          <ng-container matColumnDef="ESTATUS">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Estatus</th>
            <td mat-cell *matCellDef="let row">{{ row.ESTATUS }}</td>
          </ng-container>

          <ng-container matColumnDef="EMAIL">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Correo Electrónico</th>
            <td mat-cell *matCellDef="let row">{{ row.EMAIL }}</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" #tooltip="matTooltip" matTooltip="Editar registro" (click)="openUser('edit', row.IDUSUARIO)"
              class="no-shadow animated fadeIn" *ngIf="!btnSmall">
                <mat-icon>edit</mat-icon>
              </button>
              <button mat-mini-fab #tooltip="matTooltip" matTooltip="{{ row.ESTATUS == 'Activo' ? 'Bloquear acceso' : 'Permitir acceso' }}"
              (click)="blockUser(row.IDUSUARIO)" [disabled]="row.ESTATUS == 'Eliminado'" class="no-shadow ml-4 animated fadeIn"
              [ngClass]="{ amber_primary_background: row.ESTATUS != 'Eliminado' }" *ngIf="!btnSmall">
                <mat-icon *ngIf="row.ESTATUS == 'Activo' || row.ESTATUS == 'Eliminado'">block</mat-icon>
                <mat-icon *ngIf="row.ESTATUS == 'Inactivo'">check_circle</mat-icon>
              </button>
              <button mat-mini-fab #tooltip="matTooltip" matTooltip="Reasignar contraseña" class="no-shadow ml-4 animated fadeIn" [disabled]="row.ESTATUS == 'Eliminado'"
              [ngClass]="{ blue_dark_background: row.ESTATUS != 'Eliminado' }" (click)="openDialog(row.IDUSUARIO)" *ngIf="!btnSmall">
                <mat-icon>lock</mat-icon>
              </button>
              <button mat-mini-fab #tooltip="matTooltip" matTooltip="Eliminar usuario" class="no-shadow ml-4 animated fadeIn" [disabled]="row.ESTATUS == 'Eliminado'"
              [ngClass]="{ red_primary_background: row.ESTATUS != 'Eliminado' }" (click)="openAlert(row.IDUSUARIO)" *ngIf="!btnSmall">
                <mat-icon>delete</mat-icon>
              </button>
              <button mat-mini-fab class="no-shadow animated fadeIn" color="primary" *ngIf="btnSmall" [matMenuTriggerFor]="menu">
                <mat-icon>settings</mat-icon>
              </button>
              <mat-menu #menu="matMenu">
                <button mat-menu-item (click)="openUser('edit', row.IDUSUARIO)">
                  <mat-icon>edit</mat-icon>
                  <span>Editar registro</span>
                </button>
                <button mat-menu-item [disabled]="row.ESTATUS == 'Eliminado'" (click)="blockUser(row.IDUSUARIO)">
                  <mat-icon *ngIf="row.ESTATUS == 'Activo' || row.ESTATUS == 'Eliminado'">block</mat-icon>
                  <mat-icon *ngIf="row.ESTATUS == 'Inactivo'">check_circle</mat-icon>
                  <span>{{ row.ESTATUS == 'Activo' ? 'Bloquear acceso' : 'Permitir acceso' }}</span>
                </button>
                <button mat-menu-item [disabled]="row.ESTATUS == 'Eliminado'" (click)="openDialog(row.IDUSUARIO)">
                  <mat-icon>lock</mat-icon>
                  <span>Reasignar contraseña</span>
                </button>
                <button mat-menu-item [disabled]="row.ESTATUS == 'Eliminado'" (click)="openAlert(row.IDUSUARIO)">
                  <mat-icon>delete</mat-icon>
                  <span>Eliminar usuario</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>