<app-loading [isLoading]="loading"></app-loading>

<h2 mat-dialog-title>Gestionar imágenes en {{data.repositoryName}}</h2>

<mat-dialog-content>
  <div class="header-container">
    <button mat-icon-button color="primary" (click)="iniciarTour()">
      <mat-icon>help</mat-icon>
    </button>
    <div class="header-container-title">
    </div>
    <div class="images-button-row">
      <button class="action-button" (click)="openImageInfoDialog()">Ver Información</button>
    </div>
    <div class="images-button-row">
      <button class="action-button" (click)="syncRepository()">Sincronizar base de datos</button>
    </div>
    <div class="images-button-row">
      <button class="action-button" (click)="importImage()">
        {{ 'importImageButton' | translate }}
      </button>
    </div>
    <div class="images-button-row">
      <button class="action-button" (click)="convertImage()">
        {{ 'convertImageButton' | translate }}
      </button>
    </div>
  </div>

  <div class="search-container">
    <mat-form-field appearance="fill" class="search-string" joyrideStep="searchImagesField" text="Busca una imagen por nombre. Pulsa 'enter' para iniciar la búsqueda.">
      <mat-label>{{ 'searchLabel' | translate }}</mat-label>
      <input matInput placeholder="Búsqueda" [(ngModel)]="filters['name']" (keyup.enter)="loadData()" i18n-placeholder="@@searchPlaceholder">
      <mat-icon matSuffix>search</mat-icon>
      <mat-hint>{{ 'searchHint' | translate }}</mat-hint>
    </mat-form-field>
    <mat-form-field appearance="fill" class="search-boolean">
      <mat-label i18n="@@searchLabel">Estado</mat-label>
      <mat-select [(ngModel)]="filters['status']" (selectionChange)="loadData()" placeholder="Seleccionar opción" >
        <mat-option [value]="'failed'">Fallido</mat-option>
        <mat-option [value]="'pending'">Pendiente</mat-option>
        <mat-option [value]="'in-progress'">Transfiriendo</mat-option>
        <mat-option [value]="'success'">Creado con éxito</mat-option>
        <mat-option [value]="'transferring'">En progreso</mat-option>
        <mat-option [value]="'trash'">Papelera</mat-option>
        <mat-option [value]="'aux-files-pending'">Creando archivos auxiliares</mat-option>
      </mat-select>
    </mat-form-field>
  </div>

  <table mat-table [dataSource]="dataSource" class="mat-elevation-z8" joyrideStep="imagesTable" text="Esta tabla muestra las imágenes disponibles.">
    <ng-container *ngFor="let column of columns" [matColumnDef]="column.columnDef">
      <th mat-header-cell *matHeaderCellDef> {{ column.header }} </th>
      <td mat-cell *matCellDef="let image">
        <ng-container *ngIf="column.columnDef === 'remotePc' || column.columnDef === 'isGlobal'">
          <mat-icon [color]="image.image[column.columnDef] ? 'primary' : 'warn'">
            {{ image.image[column.columnDef] ? 'check_circle' : 'cancel' }}
          </mat-icon>
        </ng-container>
        <ng-container *ngIf="column.columnDef === 'status'">
          <mat-chip [ngClass]="{
                'chip-failed': image.status === 'failed',
                'chip-success': image.status === 'success',
                'chip-pending': image.status === 'pending',
                'chip-in-progress': image.status === 'in-progress',
                'chip-transferring': image.status === 'transferring',
              }">
            {{ getStatusLabel(image[column.columnDef]) }}
          </mat-chip>
        </ng-container>
        <ng-container *ngIf="column.columnDef !== 'remotePc' && column.columnDef !== 'status' && column.columnDef !== 'isGlobal'">
          {{ column.cell(image) }}
        </ng-container>
      </td>
    </ng-container>

    <ng-container matColumnDef="actions">
      <th mat-header-cell *matHeaderCellDef i18n="@@columnActions" style="text-align: center;">Acciones</th>
      <td mat-cell *matCellDef="let image" style="text-align: center;">
        <button mat-icon-button color="info" (click)="showImageInfo($event, image)"><mat-icon i18n="@@deleteElementTooltip">visibility</mat-icon></button>
        <button mat-icon-button color="warn" (click)="toggleAction(image, 'delete-trash')">
          <mat-icon i18n="@@deleteElementTooltip">delete</mat-icon>
        </button>
        <button mat-icon-button [matMenuTriggerFor]="menu">
          <mat-icon>menu</mat-icon>
        </button>
        <mat-menu #menu="matMenu">
          <button mat-menu-item (click)="toggleAction(image, 'get-aux')">Obtener ficheros auxiliares</button>
          <button mat-menu-item [disabled]="!image.imageFullsum || image.status !== 'success'" (click)="toggleAction(image, 'delete-permanent')">Eliminar permanentemente</button>
          <button mat-menu-item [disabled]="!image.imageFullsum || image.status !== 'trash'" (click)="toggleAction(image, 'recover')">Recuperar imagen de la papelera</button>
          <button mat-menu-item [disabled]="!image.imageFullsum || image.status !== 'success'" (click)="toggleAction(image, 'transfer')">Transferir imagen</button>
          <button mat-menu-item [disabled]="!image.imageFullsum || image.status !== 'success'" (click)="toggleAction(image, 'transfer-global')">Transferir imagen globalmente </button>
          <button mat-menu-item [disabled]="!image.imageFullsum || image.status !== 'success'" (click)="toggleAction(image, 'backup')">Realizar backup </button>
          <button mat-menu-item [disabled]="!image.imageFullsum || image.status !== 'success'" (click)="toggleAction(image, 'status')">Checkear estado imagen </button>
          <button mat-menu-item [disabled]="!image.imageFullsum || image.status !== 'success'" (click)="toggleAction(image, 'convert-image-to-virtual')">Convertir imagen en virtual </button>
        </mat-menu>
      </td>
    </ng-container>
    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
  </table>

  <div class="paginator-container">
    <mat-paginator [length]="length"
                   [pageSize]="itemsPerPage"
                   [pageIndex]="page"
                   [pageSizeOptions]="[5, 10, 20, 40, 100]"
                   (page)="onPageChange($event)">
    </mat-paginator>
  </div>

</mat-dialog-content>
<mat-dialog-actions class="action-container">
  <button class="ordinary-button" (click)="onNoClick()">Cerrar</button>
</mat-dialog-actions>


