<main class="main-container animated fadeIn prevent-select">
  <app-btn-navigate navigate="/sam/GPRS" nameButton="Gestión de personal, recursos y subcontratistas"/>
  <mat-card class="override-card override-elevation-z8" [style]="changeOnTab && cardSmall ? 'width: calc(100% - 40%)!important' : ''">
    <section class="override-section">
      <nav class="override-nav">
        <mat-card-title class="mat-card-title centre">Gestión de subcontratistas</mat-card-title>
        <div class="override-actions">
          <app-search-card
            [dataSource]="dataSourceSubcontratists"
            [disabled]="isLoading || isLoadingForm"
            (dataValue)="dataSourceSubcontratists = $event"
          />
          <div class="override-buttons">
            <app-btn-resize 
              class="ml-8 " 
              (click)="openDialog('REG')"
              [disabled]="isLoading || isLoadingForm"
              backgroundColor="raised-button-background" 
              textColor="white_font" 
              nameButton="Nuevo" 
              icon="add"
            ></app-btn-resize>
            <button 
              mat-mini-fab 
              (click)="getSubcontratists()" 
              [disabled]="isLoading" 
              matTooltip="Actualizar datos" 
              class="orange_primary_background white_font override_no_shadow ml-8">
              <mat-icon>refresh</mat-icon>
            </button>
          </div>
        </div>
      </nav>
      
      @if (!isLoading && hasError) {
        <div class="is-loading animated fadeIn fast">
          <mat-icon style="transform: scale(4.5); margin: 45px 0;" class="red_primary_font">error</mat-icon>
          <h3>{{ errorStr }}</h3>
        </div>
      }

      <app-loading-card [isLoading]="isLoading" txtLoading="Cargando la información de los subcontratistas"/>
      <div class="override-table">
        <table mat-table [dataSource]="dataSourceSubcontratists!" matSort class="animated fadeIn" [ngClass]="{ hidden: isLoading || hasError }">
          <ng-container matColumnDef="NAME">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Nombre</th>
            <td mat-cell *matCellDef="let element">{{ element.NAME }}</td>
          </ng-container>

          <ng-container matColumnDef="TIPO">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Tipo subcontratista</th>
            <td mat-cell *matCellDef="let element">{{ element.TIPO }}</td>
          </ng-container>

          <ng-container matColumnDef="INTERVENTIONS_COUNT">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Cantidad de intervenciones</th>
            <td mat-cell *matCellDef="let element">{{ element.INTERVENTIONS_COUNT }}</td>
          </ng-container>
          
          <ng-container matColumnDef="UPDATE_DATE">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Fecha de registro</th>
            <td mat-cell *matCellDef="let element"> 
              {{element.UPDATE_DATE != null ? element.UPDATE_DATE : element.REGISTER_DATE}}
            </td>
          </ng-container>
          
          <ng-container matColumnDef="UPDATED_BY_USER">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Usuario registró</th>
            <td mat-cell *matCellDef="let element">
              {{element.UPDATED_BY_USER != null ? element.UPDATED_BY_USER : element.REGISTERED_BY_USER}}
            </td>
          </ng-container>
          
          <ng-container matColumnDef="STATUS">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Estado</th>
            <td mat-cell *matCellDef="let element">
              <button mat-flat-button class="white_font" [class]="resourcesService.setChipList(element.STATUS)">
                {{ element.STATUS }}
              </button>
            </td>
          </ng-container>
          
          <ng-container matColumnDef="ACCIONES">
            <th mat-header-cell *matHeaderCellDef>Acciones</th>
            <td mat-cell *matCellDef="let element">
              <button 
                mat-mini-fab 
                color="primary" 
                [disabled]="isLoading || element.STATUS === 'Eliminado'" 
                [matMenuTriggerFor]="menu" 
                #menuTrigger 
                class="no_shadow transparent_background gray_dark_font">
                <mat-icon>more_vert</mat-icon>
              </button>
              <mat-menu #menu="matMenu">
                <button mat-menu-item (click)="openDialog('DET', element)">
                  <mat-icon>info</mat-icon> Detalles
                </button>
                <button mat-menu-item (click)="openDialog('UPD', element)">
                  <mat-icon>edit</mat-icon> Editar
                </button>
                <button mat-menu-item (click)="openDialog('DEL', element)">
                  <mat-icon>delete</mat-icon> Eliminar
                </button>
              </mat-menu>
            </td>
          </ng-container>

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

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