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

  <app-btn-navigate [navigate]="'goback'" [nameButton]="'Regresar'" />

  <mat-card class="override-card override-elevation-z8">
    <section class="override-section">
      <nav class="override-nav">
        <mat-card-title class="mat-card-title centre">Documentos Relacionados</mat-card-title>
        <div class="override-actions" [formGroup]="formGroup">
          <div style="display: flex;">
            <app-search-card
              [dataSource]="dataSource"
              [disabled]="isLoading || isLoadingForm"
              (dataValue)="dataSource = $event"
              class="mr-4"
            />
            <mat-form-field appearance="outline" class="w-300p">
              <mat-label>Línea de Solicitud</mat-label>
              <input matInput formControlName="REQUEST_LINE" [matAutocomplete]="autocompleteSetting">
              <mat-autocomplete autoActiveFirstOption #autocompleteSetting>
                <mat-option *ngFor="let orderStr of filteredLineRequest | async" [value]="orderStr">{{ orderStr }}</mat-option>
              </mat-autocomplete>
              <mat-icon matSuffix>shopping_bag</mat-icon>
              <mat-error *ngIf="formGroup.controls['REQUEST_LINE'].hasError('required')" title="El campo Número de Línea de Solicitud es obligatorio.">El campo <b>Número de Línea de Solicitud</b> es obligatorio.</mat-error>
            </mat-form-field>
          </div>
          <div class="override-buttons">

            <app-btn-resize 
              class="mr-8" 
              (click)="openDialog('REG')"
              *ngIf="formDocumentEnabled"
              [disabled]="isLoading || isLoadingForm"
              [backgroundColor]="'primary'" 
              [textColor]="'white_font'" 
              [nameButton]="'Nuevo'" 
              [icon]="'add'"
            />

            <button mat-mini-fab class="cyan_dark_background white_font override_no_shadow mr-10" matTooltip="Actualizar Datos" (click)="getDocuments()">
              <mat-icon>refresh</mat-icon>
            </button>
          </div>
        </div>
      </nav>

      <!-- ALERTA - CARGANDO FORMULARIOS -->
      <app-loading-card 
        [isLoading]="isLoading" 
        [isLoadingForm]="isLoadingForm" 
        [txtLoading]="'Cargando documentos'"
      />

      <!-- CONTENEDOR DE LA TABLA -->
      <div class="override-table" *ngIf="getDocumentEnabled">
        <table mat-table matSort [dataSource]="dataSource" class="animated fadeIn" [style.display]="isLoading ? 'none' : 'revert'">
          <!-- <ng-container matColumnDef="DLSC_IDDO">
            <th mat-header-cell *matHeaderCellDef mat-sort-header> No </th>
            <td mat-cell *matCellDef="let element"> #{{ element.DLSC_IDDO }} </td>
          </ng-container> -->
          
          <ng-container matColumnDef="DLSC_IDLI">
            <th mat-header-cell *matHeaderCellDef mat-sort-header> Número Linea </th>
            <td mat-cell *matCellDef="let element"> #{{ element.DLSC_IDLI }} </td>
          </ng-container>

          <ng-container matColumnDef="DLSC_TIPO">
            <th mat-header-cell *matHeaderCellDef mat-sort-header> Tipo de Documento </th>
            <td mat-cell *matCellDef="let element"> 
              <span *ngIf="element.DLSC_TIPO === 'Cotización'">Factura de Cotización</span>
              <span *ngIf="element.DLSC_TIPO === 'Orden'">Factura de Orden de Compra</span>
              <span *ngIf="element.DLSC_TIPO === 'Otro'">General</span>
            </td>
          </ng-container>

          <ng-container matColumnDef="DLSC_DESC">
            <th mat-header-cell *matHeaderCellDef mat-sort-header> Descripción </th>
            <td mat-cell *matCellDef="let element"> {{ element.DLSC_DESC === null ? 'NA' : this.resourcesService.resizeText(element.DLSC_DESC, 37) }} </td>
          </ng-container>

          <ng-container matColumnDef="DLSC_TIAR">
            <th mat-header-cell *matHeaderCellDef mat-sort-header> Tipo de Archivo </th>
            <td mat-cell *matCellDef="let element"> {{ resourcesService.getTypeFile(element.DLSC_TIAR) }} </td>
          </ng-container>

          <ng-container matColumnDef="DLSC_USMO">
            <th mat-header-cell *matHeaderCellDef mat-sort-header> Usuario Modificación </th>
            <td mat-cell *matCellDef="let element"> {{ resourcesService.selectUser( element.DLSC_USRE, element.DLSC_USMO ) }} </td>
          </ng-container>

          <ng-container matColumnDef="DOOR_FEMO">
            <th mat-header-cell *matHeaderCellDef mat-sort-header> Fecha de Modificación </th>
            <td mat-cell *matCellDef="let element"> {{ resourcesService.formatDate( element.DLSC_FERE, element.DLSC_FEMO ) }} </td>
          </ng-container>

          <ng-container matColumnDef="DLSC_ESTA">
            <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.DLSC_ESTA === 'Activo'">
                {{ element.DLSC_ESTA }}
              </mat-chip>
              <mat-chip class="red_dark_background white_font" *ngIf="element.DLSC_ESTA === 'Eliminado'">
                {{ element.DLSC_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" [matMenuTriggerFor]="menu" #menuTrigger
                  matTooltip="Click para desplegar las opciones" class="no-shadow">
                  <mat-icon>settings</mat-icon>
                </button>
                <mat-menu #menu="matMenu">

                  <button mat-menu-item (click)="alert('DOWNLOAD', element)" [disabled]="element.DLSC_ESTA == 'Eliminado' || isLoadingForm">
                    <mat-icon>file_download</mat-icon>
                    Descargar Documento
                  </button>

                  <button mat-menu-item *ngIf="formDocumentEnabled" (click)="openDialog('UPD', element)" [disabled]="element.DLSC_ESTA == 'Eliminado' || isLoadingForm">
                    <mat-icon>edit</mat-icon>
                    Modificar Documento
                  </button>
                  
                  <button mat-menu-item (click)="alert('ELIM', element)" [disabled]="element.DLSC_ESTA == 'Eliminado' || isLoadingForm">
                    <mat-icon>delete</mat-icon>
                    Eliminar Documento
                  </button>
                </mat-menu>
              </div>
              <div *ngIf="!resourcesService.onResize()">
                <button 
                  mat-mini-fab 
                  class="teal_dark_background white_font override_no_shadow ml-4" 
                  (click)="alert('DOWNLOAD', element)"
                  [disabled]="isLoadingForm" 
                  matTooltip="Descargar Documento">
                  <mat-icon>file_download</mat-icon>
                </button>
                <button 
                  mat-mini-fab 
                  color="primary" 
                  class="override_no_shadow ml-4"
                  (click)="openDialog('UPD', element)" 
                  *ngIf="formDocumentEnabled"
                  [disabled]="element.DLSC_ESTA == 'Eliminado' || isLoadingForm || element.DOOR_DESC == 'Ficha'"
                  matTooltip="Modificar Documento">
                  <mat-icon>edit</mat-icon>
                </button>
                <button 
                  mat-mini-fab 
                  color="warn" 
                  class="override_no_shadow ml-4"                   
                  (click)="alert('ELIM', element)" 
                  [disabled]="element.DLSC_ESTA == 'Eliminado' || isLoadingForm"
                  matTooltip="Eliminar Documento">
                  <mat-icon>delete</mat-icon>
                </button>
              </div>

            </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-20 align-center" 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, 20, 50]" class="override-paginator" showFirstLastButtons></mat-paginator>
  </mat-card>
</main>
