<main class="main-container animated fadeIn">

  <app-btn-navigate [navigate]="'/sam/GEAD'" [nameButton]="'Menú de Despacho'" />

  <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 Despachos</mat-card-title>
        <div class="override-actions">

          <app-search-card
            [dataSource]="dataSource"
            [disabled]="isLoading || isLoadingForm"
            (dataValue)="dataSource = $event"
          />

          <div class="override-buttons">
            <app-btn-resize
              class="ml-8"
              (click)="openForm('REG')"
              [disabled]="isLoading || isLoadingForm"
              [backgroundColor]="'primary'"
              [textColor]="'white_font'"
              [nameButton]="'Nuevo'"
              [icon]="'add'"
            />
            <button
              mat-mini-fab
              matTooltip="Actualizar Datos"
              class="cyan_dark_background white_font override_no_shadow ml-8"
              (click)="getDistpach()"
            >
              <mat-icon>refresh</mat-icon>
            </button>
          </div>
        </div>
      </nav>

      <app-loading-card
        [isLoading]="isLoading"
        [isLoadingForm]="isLoadingForm"
        [txtLoading]="'Cargando los despachos'"
      />

      <div class="override-table">
        <table mat-table [dataSource]="dataSource" matSort class="animated fadeIn" [style.display]="isLoading ? 'none' : 'revert'">
          <ng-container matColumnDef="NUMERO_DESPACHO">
            <th mat-header-cell *matHeaderCellDef mat-sort-header> Número de Despacho </th>
            <td mat-cell *matCellDef="let element"> Despacho #{{ element.DESP_IDDE }} </td>
          </ng-container>
          <ng-container matColumnDef="DESPACHO">
            <th mat-header-cell *matHeaderCellDef mat-sort-header> Despacho </th>
            <td mat-cell *matCellDef="let element"> {{ element.DESP_NODE }} </td>
          </ng-container>
          <ng-container matColumnDef="DIRECCION">
            <th mat-header-cell *matHeaderCellDef mat-sort-header> Dirección </th>
            <td mat-cell *matCellDef="let element">
              {{ setAddress( element.DESP_CALL, element.DESP_NUEX, element.DESP_NUIN, element.DESP_COPO, element.DESP_COLO, element.DESP_MUNI, element.DESP_ENTI ) }}
            </td>
          </ng-container>
          <ng-container matColumnDef="FECHA_REGISTRA">
            <th mat-header-cell *matHeaderCellDef mat-sort-header> Fecha de Modificación </th>
            <td mat-cell *matCellDef="let element"> {{ resourcesService.formatDate(element.DESP_FERE, element.DESP_FEMO) }} </td>
          </ng-container>
          <ng-container matColumnDef="USUARIO_REGISTRA">
            <th mat-header-cell *matHeaderCellDef mat-sort-header> Usuario Modificación </th>
            <td mat-cell *matCellDef="let element"> {{ element.DESP_USMO === null ? element.DESP_USRE : element.DESP_USMO }} </td>
          </ng-container>
          <ng-container matColumnDef="ESTADO">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Estado</th>
            <td mat-cell *matCellDef="let element">
              <mat-chip
                class="teal_dark_background white_font"
                *ngIf="element.DESP_ESTA === 'Activo'">
                {{ element.DESP_ESTA }}
              </mat-chip>
              <mat-chip
                class="red_dark_background"
                *ngIf="element.DESP_ESTA === 'Eliminado'">
                {{ element.DESP_ESTA }}
              </mat-chip>
            </td>
          </ng-container>
          <ng-container matColumnDef="ACCIONES">
            <th mat-header-cell *matHeaderCellDef>Acciones</th>
            <td mat-cell *matCellDef="let element">
              <div *ngIf="!resourcesService.onResize()">
                <button
                  mat-mini-fab
                  color="primary"
                  class="override_no_shadow mr-4"
                  matTooltip="Editar"
                  [disabled]="element.DESP_ESTA === 'Eliminado'"
                  (click)="openForm('UPD', element)">
                  <mat-icon>edit</mat-icon>
                </button>
                <button
                  mat-mini-fab
                  color="warn"
                  class="override_no_shadow mr-4"
                  matTooltip="Eliminar"
                  [disabled]="element.DESP_ESTA === 'Eliminado'"
                  (click)="alert('ELI', element)">
                  <mat-icon>delete</mat-icon>
                </button>
              </div>
              <div *ngIf="resourcesService.onResize()">
                <button mat-mini-fab color="primary" [matMenuTriggerFor]="menu" #menuTrigger matTooltip="Click para desplegar las opciones" 
                  class="override_no_shadow" [disabled]="element.DESP_ESTA === 'Eliminado'">
                  <mat-icon>settings</mat-icon>
                </button>
                <mat-menu #menu="matMenu">
                  <button mat-menu-item (click)="openForm('UPD', element)">
                    <mat-icon>edit</mat-icon> Editar
                  </button>
                  <button mat-menu-item (click)="alert('ELI', element)">
                    <mat-icon>delete</mat-icon> Eliminar
                  </button>
                </mat-menu>
              </div>
            </td>
          </ng-container>
          <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
          <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
          <!-- CONTENIDO CUANDO NO EXISTAN DATOS -->
          <tr class="mat-row" *matNoDataRow>
            <td class="p-80 centre" colspan="12"><object data="assets/img/empty_data.svg" width="150"></object> <h2>Sin datos</h2></td>
          </tr>
        </table>
      </div>
    </section>

    <!-- PAGINADOR -->
    <mat-paginator [pageSizeOptions]="[10, 50, 100]" class="override-paginator"></mat-paginator>
  </mat-card>
</main>
