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

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

  <mat-card class="override-card override-elevation-z8">
    <nav class="override-nav">
      <mat-card-title class="mat-card-title align-center">Carrito de Compra {{ titleCard }}</mat-card-title>
      <div class="override-actions">
        <app-search-card
          [dataSource]="dataSource"
          [disabled]="isLoading || isLoadingForm"
          (dataValue)="dataSource = $event"
        />
        <div class="override-buttons">
          @if (selectedArtitleEnable) {
            <app-btn-resize
              class="ml-8"
              (click)="goShopping()"
              [disabled]="isLoading || isLoadingForm"
              [backgroundColor]="'teal_dark_background'"
              [textColor]="'white_font'"
              [nameButton]="'Ir a Catálogos de Artículos'"
              [icon]="'category'"
            />
          }
          @if (createGroupEnable) {
            <app-btn-resize
              class="ml-8"
              (click)="alert('CREATE')"
              [disabled]="isLoading || isLoadingForm"
              backgroundColor="raised-button-background"
              [textColor]="'white_font'"
              [nameButton]="'Registrar Solicitud'"
              [icon]="'add'"
            />
          }
        </div>
      </div>
    </nav>
    <app-loading-card
      [isLoading]="isLoading"
      [isLoadingForm]="isLoadingForm"
      [txtLoading]="'Cargando la información del carrito de compra'"
    />
    @if (cartShoppingEnabled) {
      <div class="override-table">
        <table mat-table class="animated fadeIn w-100" matSort [dataSource]="dataSource" [style.display]="isLoading ? 'none': 'revert'">
          <ng-container matColumnDef="IMAGENES">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Imagen</th>
            <td mat-cell *matCellDef="let element">
              <img [src]="element.IMAGENES[0]" width="70px" />
            </td>
          </ng-container>
  
          <ng-container matColumnDef="PROVEEDOR">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Proveedor</th>
            <td mat-cell *matCellDef="let element"> {{ element.NUMERO_PROVEEDOR }} </td>
          </ng-container>
  
          <ng-container matColumnDef="ARTICULO">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Artículo</th>
            <td mat-cell *matCellDef="let element"> {{ element.ARTICULO }} </td>
          </ng-container>
  
          <ng-container matColumnDef="MODELO">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Modelo</th>
            <td mat-cell *matCellDef="let element"> {{ element.MODELO }} </td>
          </ng-container>
  
          <ng-container matColumnDef="CODIGO">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Código</th>
            <td mat-cell *matCellDef="let element"> {{ element.CODIGO }} </td>
          </ng-container>
  
          <ng-container matColumnDef="PRECIO">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Precio Unitario</th>
            <td mat-cell *matCellDef="let element"> {{ resourcesService.moneyFormat(element.PRECIO) }} {{ element.MONEDA }}</td>
          </ng-container>
  
          <ng-container matColumnDef="CANTIDAD">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Cantidad</th>
            <td mat-cell *matCellDef="let element"> 
              <div class="content-input">
                @if (createGroupEnable) {
                  <button 
                    mat-mini-fab
                    class="override_no_shadow"
                    color="primary" 
                    (click)="quantityProducts('resta', element)" 
                    [disabled]="element.CANTIDAD <= 1 ">
                    <mat-icon>remove</mat-icon>
                  </button>
                  <input 
                    type="number" 
                    class="input_cantidad centre" 
                    (keyup)="setQuantity(element.CANTIDAD, element)" 
                    [(ngModel)]="element.CANTIDAD" 
                    [value]="element.CANTIDAD">
                  <button 
                    mat-mini-fab
                    class="override_no_shadow"
                    color="primary" 
                    [disabled]="element.CANTIDAD >= 9999"
                    (click)="quantityProducts('suma', element)">
                    <mat-icon>add</mat-icon>
                  </button>
                }
              </div>
            </td>
          </ng-container>
  
          <ng-container matColumnDef="PRECIOTOTAL">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Precio Total</th>
            <td mat-cell *matCellDef="let element">
              <b> {{ resourcesService.moneyFormat(element.CANTIDAD * element.PRECIO) }} {{ element.MONEDA }} </b>
            </td>
          </ng-container>
  
          <ng-container matColumnDef="ACCIONES">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Acciones</th>
            <td mat-cell *matCellDef="let element">
              <button mat-mini-fab color="primary" class="override_no_shadow" [matMenuTriggerFor]="menu" matTooltip="Abrir Acciones">
                <mat-icon>more_vert</mat-icon>
              </button>
              <mat-menu #menu="matMenu">
                <button mat-menu-item (click)="openDialog('DETAILS', element )">
                  <mat-icon>info</mat-icon>
                  Ver información
                </button>
                <button mat-menu-item (click)="alert('DELETE', element )">
                  <mat-icon>delete</mat-icon>
                  Eliminar artículo
                </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 centre" style="padding: 99px 80px;" colspan="12">
              <object data="assets/img/empty_cart.svg" width="350"></object>
              <h2>No hay productos en el carrito</h2>
            </td>
          </tr>
        </table>
      </div>
    }
  </mat-card>
</body>
