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

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

  <mat-card class="override-card override-elevation-z8">
    <section class="override-section">
      <mat-progress-bar mode="indeterminate" *ngIf="isLoadingForm"></mat-progress-bar>
      <nav class="override-nav">
        <mat-card-title class="mat-card-title centre">Formulario - {{ typeForm.type === 'REGI' ? 'Registro' : 'Modificación' }} de Artículo</mat-card-title>
      </nav>
      <div>
        <div class="pl-16 pr-16">
          <mat-grid-list cols="24" rowHeight="82px" [formGroup]="formGroup">
            <mat-grid-tile colspan="4">
              <mat-form-field appearance="outline">
                <mat-label>Código de Barras</mat-label>
                <input matInput formControlName="CODIGO" maxlength="50" />
                <button mat-icon-button matSuffix><mat-icon>qr_code_scanner</mat-icon></button>
              </mat-form-field>
            </mat-grid-tile>
            <mat-grid-tile colspan="10">
              <mat-form-field appearance="outline">
                <mat-label>Nombre del Artículo</mat-label>
                <input matInput formControlName="ARTICULO" maxlength="100" />
              </mat-form-field>
            </mat-grid-tile>
            <mat-grid-tile colspan="5">
              <mat-form-field appearance="outline">
                <mat-label>Familia</mat-label>
                <mat-select formControlName="FAMILIA" (ngModelChange)="getSubfamily()">
                  <mat-option *ngFor="let family of arrFamilies" [value]="family"> {{ family.FAMILIA }} </mat-option>
                </mat-select>
              </mat-form-field>
            </mat-grid-tile>
            <mat-grid-tile colspan="5">
              <mat-form-field appearance="outline">
                <mat-label>Subfamilia</mat-label>
                <mat-select formControlName="SUBFAMILIA">
                  <mat-option *ngFor="let subfamily of arrSubfamilies" [value]="subfamily"> {{ subfamily.SUBFAMILIA }} </mat-option>
                </mat-select>
              </mat-form-field>
            </mat-grid-tile>
          </mat-grid-list>
        </div>

        <div class="align-end pr-16">
          <button
            mat-raised-button
            [disabled]="isLoading || isLoadingForm"
            class="blue_dark_background white_font override_no_shadow ml-5 animated fadeIn fast"
            (click)="openDialog('FORM_INFO')">
            <mat-icon>add</mat-icon> Nuevo
          </button>
        </div>

        <app-loading-card
          [isLoading]="isLoading"
          [txtLoading]="'Cargando las información del artículo'"
        />

        <div style="overflow: auto;">
          <table mat-table [dataSource]="dataSourceInformation" matSort [style.display]="isLoading ? 'none' : 'revert'">
            <ng-container matColumnDef="IMAGEN">
              <th mat-header-cell *matHeaderCellDef>Imagén</th>
              <td mat-cell *matCellDef="let element">
                <div style="width: 100px; height: 87px; overflow-x: auto; overflow-y: hidden;">
                  <img [src]="setImage(element)" [alt]="setAlt(element)" class="url-img" />
                </div>
              </td>
            </ng-container>
            <ng-container matColumnDef="PROVEEDOR">
              <th mat-header-cell *matHeaderCellDef>Proveedor</th>
              <td mat-cell *matCellDef="let element"> {{ resourcesService.resizeText(element.PROVEEDOR.PROV_NOCO, 80) }} </td>
            </ng-container>
            <ng-container matColumnDef="DESCRIPCION">
              <th mat-header-cell *matHeaderCellDef>Descripción</th>
              <td mat-cell *matCellDef="let element"> {{ resourcesService.resizeText(element.DESCRIPCION, 42) }} </td>
            </ng-container>
            <ng-container matColumnDef="CARACTERISTICAS">
              <th mat-header-cell *matHeaderCellDef>Características</th>
              <td mat-cell *matCellDef="let element"> {{ element.CARACTERISTICAS === '' ? 'N/A' : resourcesService.resizeText(element.CARACTERISTICAS, 42) }} </td>
            </ng-container>
            <ng-container matColumnDef="CANTIDAD_DETALLES">
              <th mat-header-cell *matHeaderCellDef>Cantidad de Detalles</th>
              <td mat-cell *matCellDef="let element"> {{ countDetails(element.DETAILS) }} </td>
            </ng-container>
            <ng-container matColumnDef="ESTADO">
              <th mat-header-cell *matHeaderCellDef>Estado</th>
              <td mat-cell *matCellDef="let element">
                <mat-chip class="teal_primary_background" 
                  *ngIf="element.ESTADO === 'Activo' || element.ESTADO === 'Nuevo' || element.ESTADO === 'Modificado'">Activo</mat-chip>
                <mat-chip class="red_dark_background" *ngIf="element.ESTADO === 'Eliminado'">{{ element.ESTADO }}</mat-chip>
              </td>
            </ng-container>
            <ng-container matColumnDef="ACCIONES">
              <th mat-header-cell *matHeaderCellDef>Acciones</th>
              <td mat-cell *matCellDef="let element" class="align-center">
                <button
                  mat-mini-fab
                  color="primary"
                  matTooltip="Agregar Detalles"
                  class="override_no_shadow ml-4"
                  (click)="openDialog('DETAILS', element)"
                  [disabled]="element.ESTADO === 'Eliminado' || isLoading || isLoadingForm">
                  <mat-icon>post_add</mat-icon>
                </button>
                <button
                  mat-mini-fab
                  matTooltip="Editar"
                  class="teal_dark_background white_font override_no_shadow ml-4"
                  (click)="openDialog('FORM_INFO', element)"
                  [disabled]="element.ESTADO === 'Eliminado' || isLoading || isLoadingForm">
                  <mat-icon>edit</mat-icon>
                </button>
                <button
                  mat-mini-fab
                  color="warn"
                  matTooltip="Eliminar"
                  class="override_no_shadow ml-4"
                  [disabled]="isLoading || isLoadingForm"
                  (click)="openDialog('ELIM_INFO', element)"
                  *ngIf="element.ESTADO !== 'Eliminado'">
                  <mat-icon>close</mat-icon>
                </button>
                <button
                  mat-mini-fab
                  matTooltip="Restaurar"
                  [disabled]="isLoading || isLoadingForm"
                  class="cyan_dark_background white_font override_no_shadow ml-4"
                  (click)="openDialog('REST_INFO', element)" 
                  *ngIf="element.ESTADO === 'Eliminado'">
                  <mat-icon>autorenew</mat-icon>
                </button>
              </td>
            </ng-container>
            <tr mat-header-row *matHeaderRowDef="displayedColumnsInformation"></tr>
            <tr mat-row *matRowDef="let row; columns: displayedColumnsInformation"></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>
        
      </div>
    </section>
    <div class="align-end">
      <mat-divider></mat-divider>
      <!-- (click)="createObjectResponse()" -->
      <button
        mat-raised-button
        color="primary"
        (click)="openDialog(typeForm.type)"
        [disabled]="checkValidation() || isLoading || isLoadingForm"
        class="override_no_shadow mt-16 mr-16 mb-16" >
        <mat-icon>save</mat-icon>
        {{ typeForm.type === 'REGI' ? 'Registrar' : 'Modificar' }}
      </button>
    </div>
  </mat-card>
</main>
