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

  <div class="row-tools-bar card-view-padding">
    <button mat-raised-button class="raised-button-background mb-16" [routerLink]="['/sam/USPE']">
      Usuarios y perfiles<mat-icon>arrow_back</mat-icon>
    </button>
  </div>

  <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-flat-button color="primary" class="mr-10 animated fadeIn raised-button-background" (click)="openUser('insert', '')" *ngIf="!btnSmall" 
            [disabled]="!regEditUserEnabled">
              <mat-icon>add</mat-icon> Crear nueva cuenta
            </button>
            <button mat-mini-fab color="primary" class="override_no_shadow mr-10 animated fadeIn raised_button_background " matTooltip="Crear nueva cuenta" (click)="openUser('insert', '')"
            *ngIf="btnSmall" class="raised-button-background mr-4" [disabled]="!regEditUserEnabled">
              <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="!usersConsultEnabled && !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 animated fadeIn" [ngClass]="{ hidden: isLoading || hasError }">
        <table mat-table [dataSource]="dataSource!" matSort class="animated fadeIn" 
        [style.display]="!isLoading && usersConsultEnabled && !hasError ? 'revert' : 'none'">
          <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 white_font orange_primary_background" *ngIf="!btnSmall" [disabled]="!regEditUserEnabled">
                <mat-icon>edit</mat-icon>
              </button>
              <button mat-mini-fab #tooltip="matTooltip" matTooltip="{{ row.ESTATUS == 'Activo' ? 'Bloquear acceso' : 'Permitir acceso' }}" *ngIf="!btnSmall" 
              (click)="blockUser(row.IDUSUARIO)" [disabled]="row.ESTATUS == 'Eliminado' || !blockUnblockEnabled" class="no_shadow ml-4 animated fadeIn "
              [ngClass]="{ red_primary_background: row.ESTATUS != 'Eliminado' && blockUnblockEnabled, white_font: row.ESTATUS != 'Eliminado' && blockUnblockEnabled }">
                <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' || !changePasswordEnabled" (click)="openDialog(row.IDUSUARIO)" *ngIf="!btnSmall"
              [ngClass]="{ orange_primary_background: row.ESTATUS != 'Eliminado' && changePasswordEnabled, white_font: row.ESTATUS != 'Eliminado' && changePasswordEnabled }" >
                <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', white_font: 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" class="no_shadow gray_dark_font transparent_background" [matMenuTriggerFor]="menu">
                <mat-icon>more_vert</mat-icon>
              </button>
              <mat-menu #menu="matMenu">
                <button mat-menu-item (click)="openUser('edit', row.IDUSUARIO)" [disabled]="!regEditUserEnabled">
                  <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-40 centre" colspan="100%">
              <object data="assets/img/empty_data.svg" class="no-data-image"></object>
              <h2>Sin datos</h2>
            </td>
          </tr>
        </table>
      </div>
    </section>
    <mat-paginator [pageSizeOptions]="[10, 50, 100]" class="override-paginator"></mat-paginator>
  </mat-card>

</main>