<div fxLayout="column" fxFlex="100" fxLayoutAlign="start stretch" class="padding-gap">
  <div fxLayout="row">
    <button mat-flat-button color="primary" tabindex="1" (click)="openCreateOfferModal()">Create Offer</button>
  </div>
  <div fxLayout="column" fxLayoutAlign="start stretch">
    <div fxLayout="column" fxLayoutAlign="start stretch" fxLayout.gt-sm="row wrap" class="page-sub-title-container mt-1">
      <div fxFlex="70" fxLayoutAlign="start start" fxLayoutAlign.gt-sm="start center">
        <fa-icon class="page-title-img mr-1" [icon]="faHistory"></fa-icon>
        <span class="page-title">Offers History</span>
      </div>
      <div fxFlex.gt-xs="30" fxLayoutAlign.gt-xs="space-between center" fxLayout="row" fxLayoutAlign="space-between stretch">
        <mat-form-field fxLayout="column" fxFlex="49">
          <mat-label>Filter By</mat-label>
        <mat-select tabindex="1" name="filterBy" [(ngModel)]="selFilterBy" (selectionChange)="selFilter=''; applyFilter()">
            <perfect-scrollbar><mat-option *ngFor="let column of ['all'].concat(displayedColumns.slice(0, -1))" [value]="column">{{getLabel(column)}}</mat-option></perfect-scrollbar>
          </mat-select>
        </mat-form-field>
        <mat-form-field fxLayout="column" fxFlex="49">
          <mat-label>Filter</mat-label>
        <input matInput name="filter" [(ngModel)]="selFilter" (input)="applyFilter()" (keyup)="applyFilter()">
        </mat-form-field>
      </div>
    </div>
    <div fxLayout="column" fxFlex="100" class="table-container" [perfectScrollbar]>
      <mat-progress-bar *ngIf="apiCallStatus?.status === apiCallStatusEnum.INITIATED" mode="indeterminate"></mat-progress-bar>      
      <table #table mat-table fxFlex="100" matSort [matSortActive]="tableSetting.sortBy" [matSortDirection]="tableSetting.sortOrder" [dataSource]="offers" [ngClass]="{'error-border': errorMessage !== ''}">
        <ng-container matColumnDef="active">
          <th *matHeaderCellDef mat-header-cell mat-sort-header arrowPosition="before" matTooltip="Active"></th>
          <td *matCellDef="let offer" mat-cell>
            <span *ngIf="offer.active" class="dot green" matTooltip="Active" matTooltipPosition="right" [ngClass]="{'mr-0': screenSize === screenSizeEnum.XS}"></span>
            <span *ngIf="!offer.active" class="dot red" matTooltip="Inactive" matTooltipPosition="right" [ngClass]="{'mr-0': screenSize === screenSizeEnum.XS}"></span>
          </td>
        </ng-container>
        <ng-container matColumnDef="offer_id">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Offer ID</th>
          <td *matCellDef="let offer" mat-cell>
            <div class="ellipsis-parent" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
              <span class="ellipsis-child">
                {{offer.offer_id}}
              </span>
            </div>
          </td>
        </ng-container>
        <ng-container matColumnDef="single_use">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Single Use</th>
          <td *matCellDef="let offer" mat-cell>{{offer.single_use ? 'Yes' : 'No'}}</td>
        </ng-container>
        <ng-container matColumnDef="used">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Used</th>
          <td *matCellDef="let offer" mat-cell>
            {{offer.used ? 'Yes' : 'No'}}
          </td>
        </ng-container>
        <ng-container matColumnDef="bolt12">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Invoice</th>
          <td *matCellDef="let offer" mat-cell>
            <div class="ellipsis-parent" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
              <span class="ellipsis-child">
                {{offer.bolt12}}
              </span>
            </div>
          </td>
        </ng-container>
        <ng-container matColumnDef="actions">
          <th *matHeaderCellDef mat-header-cell>
            <div class="bordered-box table-actions-select" fxLayoutAlign="center center">
              <mat-select placeholder="Actions" tabindex="1" class="mr-0">
                <mat-select-trigger></mat-select-trigger>
                <mat-option (click)="onDownloadCSV()">Download CSV</mat-option>
              </mat-select>
            </div>
         </th>
          <td *matCellDef="let offer" mat-cell fxLayoutAlign="end center">
            <div class="bordered-box table-actions-select" fxLayoutAlign="center center">
              <mat-select placeholder="Actions" tabindex="4" class="mr-0">
                <mat-select-trigger></mat-select-trigger>
                <mat-option (click)="onOfferClick(offer)">View Info</mat-option>
                <mat-option *ngIf="offer.active" (click)="onDisableOffer(offer)">Disable Offer</mat-option>
                <mat-option *ngIf="offer.active" (click)="onPrintOffer(offer)">Export QR code</mat-option>
              </mat-select>
            </div>
          </td>
        </ng-container>
        <ng-container matColumnDef="no_offer">
          <td *matFooterCellDef  mat-footer-cell colspan="4">
            <p *ngIf="(!offers?.data || offers?.data?.length<1) && apiCallStatus?.status === apiCallStatusEnum.COMPLETED">No offer available.</p>
            <p *ngIf="(!offers?.data || offers?.data?.length<1) && apiCallStatus?.status === apiCallStatusEnum.INITIATED">Getting offers...</p>
            <p *ngIf="(!offers?.data || offers?.data?.length<1) && apiCallStatus?.status === apiCallStatusEnum.ERROR">{{errorMessage}}</p>
          </td>
        </ng-container>
        <tr *matFooterRowDef="['no_offer']" mat-footer-row [ngClass]="{'display-none': offers?.data && offers?.data?.length>0}"></tr>
        <tr *matHeaderRowDef="displayedColumns" mat-header-row></tr>
        <tr *matRowDef="let row; columns: displayedColumns;" mat-row></tr>
      </table>
    </div>
    <mat-paginator class="mb-1" [pageSize]="pageSize" [pageSizeOptions]="pageSizeOptions" [showFirstLastButtons]="screenSize === screenSizeEnum.XS ? false : true"></mat-paginator>
  </div>
</div>