<div fxLayout="column" fxFlex="100" fxLayoutAlign="start stretch" class="padding-gap">
  <div fxLayout="row">
    <button mat-flat-button color="primary" (click)="openCreateOfferModal()" tabindex="1">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 [icon]="faHistory" class="page-title-img mr-1"></fa-icon>
        <span class="page-title">Offers History</span>
      </div>
      <mat-form-field fxFlex="30" fxLayoutAlign="start end">
        <input matInput (keyup)="applyFilter()" [(ngModel)]="selFilter" placeholder="Filter">
      </mat-form-field>
    </div>
    <div [perfectScrollbar] fxLayout="column" fxLayoutAlign="start center" fxFlex="100" class="table-container">
      <mat-progress-bar *ngIf="apiCallStatus?.status === apiCallStatusEnum.INITIATED" mode="indeterminate"></mat-progress-bar>      
      <table mat-table #table fxFlex="100" [dataSource]="offers" matSort [ngClass]="{'overflow-auto error-border': errorMessage !== '','overflow-auto': true}">
        <ng-container matColumnDef="offer_id">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> Offer ID </th>
          <td mat-cell *matCellDef="let offer">
            <div class="ellipsis-parent" [ngStyle]="{'max-width': (screenSize === screenSizeEnum.XS) ? '25rem' : '45rem'}">
              <span class="ellipsis-child">
                <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>
                {{offer.offer_id}}
              </span>
            </div>
          </td>
        </ng-container>
        <ng-container matColumnDef="single_use">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> Single Use </th>
          <td mat-cell *matCellDef="let offer">{{offer.single_use ? 'Yes' : 'No'}}</td>
        </ng-container>
        <ng-container matColumnDef="used">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> Used </th>
          <td mat-cell *matCellDef="let offer">
            {{offer.used ? 'Yes' : 'No'}}
          </td>
        </ng-container>
        <ng-container matColumnDef="actions">
          <th mat-header-cell *matHeaderCellDef class="px-3">
            <div class="bordered-box table-actions-select">
              <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 mat-cell *matCellDef="let offer" [ngClass]="{'px-3': screenSize !== screenSizeEnum.XS}" 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 mat-footer-cell *matFooterCellDef 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 mat-footer-row *matFooterRowDef="['no_offer']" [ngClass]="{'display-none': offers?.data && offers?.data?.length>0}"></tr>
        <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: flgSticky;"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
      </table>
    </div>
    <mat-paginator [pageSize]="pageSize" [pageSizeOptions]="pageSizeOptions" [showFirstLastButtons]="screenSize === screenSizeEnum.XS ? false : true" class="mb-1"></mat-paginator>
  </div>
</div>