<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>
                @for (orderStr of filteredLineRequest | async; track orderStr) {
                  <mat-option [value]="orderStr">{{ orderStr }}</mat-option>
                }
              </mat-autocomplete>
              <mat-icon matSuffix>shopping_bag</mat-icon>
              @if (formGroup.controls['REQUEST_LINE'].hasError('required')) {
                <mat-error 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">
            @if (formDocumentEnabled) {
              <app-btn-resize 
                class="mr-8 " 
                (click)="openDialog('REG')"
                [disabled]="isLoading || isLoadingForm || selectedRequest === null"
                [backgroundColor]="'primary '" 
                [textColor]="'white_font'" 
                [nameButton]="'Nuevo'" 
                [icon]="'add'"
              />
            }
            <button mat-mini-fab class="orange_primary_background white_font override_no_shadow mr-10" matTooltip="Actualizar datos" (click)="getDocuments()">
              <mat-icon>refresh</mat-icon>
            </button>
          </div>
        </div>
      </nav>

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

      @if (getDocumentEnabled) {
        <div class="override-table">
          <table mat-table matSort [dataSource]="dataSource" class="animated fadeIn" [style.display]="isLoading ? 'none' : 'revert'">
            
            <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"> 
                @if (element.DLSC_TIPO === 'Cotización') {
                  <span>Factura de Cotización</span>
                }
                @if (element.DLSC_TIPO === 'Orden') {
                  <span>Factura de Orden de Compra</span>
                }
                @if (element.DLSC_TIPO === 'Abono') {
                  <span>Factura de Abono de Pedido</span>
                }
                @if (element.DLSC_TIPO === 'Otro') {
                  <span>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 actualizació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 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">
                @if (element.DLSC_ESTA === 'Activo') {
                  <button mat-flat-button class="teal_dark_background white_font">
                    {{ element.DLSC_ESTA }}
                  </button>
                }
                @if (element.DLSC_ESTA === 'Eliminado') {
                  <button mat-flat-button class="red_dark_background white_font">
                    {{ element.DLSC_ESTA }}
                  </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" 
                  [matMenuTriggerFor]="menu" 
                  [disabled]="element.DLSC_ESTA == 'Eliminado' || isLoadingForm"
                  #menuTrigger
                  matTooltip="Desplegar opciones" 
                  class="no_shadow transparent_background gray_dark_font">
                  <mat-icon>more_vert</mat-icon>
                </button>
                <mat-menu #menu="matMenu">
                  <button mat-menu-item (click)="alert('DOWNLOAD', element)">
                    <mat-icon>file_download</mat-icon>
                    Descargar Documento
                  </button>
                  @if (formDocumentEnabled) {
                    <button mat-menu-item (click)="openDialog('UPD', element)" [disabled]="element.DOOR_DESC == 'Ficha'">
                      <mat-icon>edit</mat-icon>
                      Modificar Documento
                    </button>
                  }
                  <button mat-menu-item (click)="alert('ELIM', element)">
                    <mat-icon>delete</mat-icon>
                    Eliminar Documento
                  </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-20 align-center" colspan="12">
                <object data="assets/img/empty_data.svg" width="150"></object>
                <h2>Sin datos</h2>
              </td>
            </tr>
          </table>
        </div>
      }
    </section>
    <mat-paginator [pageSizeOptions]="[10, 20, 50]" class="override-paginator" showFirstLastButtons></mat-paginator>
  </mat-card>
</main>
