<h1 class="d-flex justify-content-between">
  <span>
    <my-global-icon iconName="cloud-download" aria-hidden="true"></my-global-icon>
    <ng-container i18n>My imports</ng-container>
  </span>

  <a routerLink="/my-library/video-channel-syncs" class="button-link">
    <my-global-icon iconName="repeat" aria-hidden="true"></my-global-icon>
    <ng-container i18n>My synchronizations</ng-container>
  </a>
</h1>

<div class="mb-4 d-flex justify-content-between">
  <my-advanced-input-filter (search)="onSearch($event)"></my-advanced-input-filter>
</div>

<p-table
  [value]="videoImports" [lazy]="true" [paginator]="totalRecords > 0" [totalRecords]="totalRecords" [rows]="rowsPerPage" [first]="pagination.start"
  [rowsPerPageOptions]="rowsPerPageOptions" [sortField]="sort.field" [sortOrder]="sort.order" (onLazyLoad)="loadLazy($event)" [lazyLoadOnInit]="false" dataKey="id"
  [showCurrentPageReport]="true" [currentPageReportTemplate]="getPaginationTemplate()"
  [expandedRowKeys]="expandedRows"
>
  <ng-template pTemplate="header">
    <tr>
      <th scope="col" style="width: 40px;">
        <span i18n class="visually-hidden">More information</span>
      </th>
      <th scope="col" style="width: 200px">Action</th>
      <th scope="col" style="width: 45%" i18n>Target</th>
      <th scope="col" style="width: 55%" i18n>Video</th>
      <th scope="col" style="width: 150px" i18n>State</th>
      <th scope="col" style="width: 150px" i18n [ngbTooltip]="sortTooltip" container="body" pSortableColumn="createdAt">Created <p-sortIcon field="createdAt"></p-sortIcon></th>
    </tr>
  </ng-template>

  <ng-template pTemplate="body" let-expanded="expanded" let-videoImport>
    <tr>
      <td class="expand-cell">
        <my-table-expander-icon *ngIf="videoImport.error" [pRowToggler]="videoImport" [expanded]="expanded"></my-table-expander-icon>
      </td>

      <td class="action-cell">
        <my-button *ngIf="isVideoImportPending(videoImport)" i18n-label label="Cancel" icon="no" (click)="cancelImport(videoImport)"></my-button>
        <my-delete-button *ngIf="isVideoImportFailed(videoImport) || isVideoImportCancelled(videoImport) || !videoImport.video" (click)="deleteImport(videoImport)"></my-delete-button>
        <my-edit-button *ngIf="isVideoImportSuccess(videoImport) && videoImport.video" [ptRouterLink]="getEditVideoUrl(videoImport.video)"></my-edit-button>
      </td>

      <td>
        <a *ngIf="videoImport.targetUrl; else torrent" [href]="videoImport.targetUrl" target="_blank" rel="noopener noreferrer">{{ videoImport.targetUrl }}</a>
        <ng-template #torrent>
          <span [title]="videoImport.torrentName || videoImport.magnetUri">{{ videoImport.torrentName || videoImport.magnetUri }}</span>
        </ng-template>
      </td>

      <td>
        <ng-container *ngIf="isVideoImportPending(videoImport)">{{ videoImport.video?.name }}</ng-container>
        <ng-container *ngIf="isVideoImportSuccess(videoImport) && videoImport.video">
          <a [href]="getVideoUrl(videoImport.video)" target="_blank" rel="noopener noreferrer">{{ videoImport.video?.name }}</a>
        </ng-container>
        <ng-container *ngIf="isVideoImportSuccess(videoImport) && !videoImport.video" i18n>This video was deleted</ng-container>
        <ng-container *ngIf="isVideoImportFailed(videoImport)"></ng-container>
      </td>

      <td>
        <span class="pt-badge" [ngClass]="getVideoImportStateClass(videoImport.state.id)">
          {{ videoImport.state.label }}
        </span>
      </td>

      <td>{{ videoImport.createdAt | date: 'short' }}</td>
    </tr>
  </ng-template>

  <ng-template pTemplate="rowexpansion" let-videoImport>
    <tr class="video-import-error" *ngIf="videoImport.error">
      <td myAutoColspan>
        <pre>{{ videoImport.error }}</pre>
      </td>
    </tr>
  </ng-template>
</p-table>
