<div fxLayout="column" fxFlex="100" fxLayoutAlign="start stretch" class="padding-gap">
  <form *ngIf="calledFrom === 'home'" #addInvoiceForm="ngForm" fxLayout="row wrap" fxLayoutAlign="stretch start" fxFlex="100">
    <mat-form-field fxLayout="column" fxFlex="100" fxLayoutAlign="space-between stretch">
      <mat-label>Description</mat-label>
      <input matInput tabindex="2" name="description" [(ngModel)]="description">
    </mat-form-field>
    <mat-form-field fxLayout="column" fxFlex="100" fxLayoutAlign="start end">
      <mat-label>Amount</mat-label>
      <input matInput name="invoiceValue" type="number" tabindex="3" [step]="100" [min]="1" [(ngModel)]="invoiceValue" (keyup)="onInvoiceValueChange()">
      <span matSuffix>  Sats </span>
      <mat-hint>{{invoiceValueHint}}</mat-hint>
    </mat-form-field>
    <div  fxLayout="row" class="mt-1">
      <button class="mr-1" mat-stroked-button color="primary" tabindex="9" type="reset" (click)="resetData()">Clear Field</button>
      <button mat-flat-button color="primary" tabindex="10" (click)="onAddInvoice(addInvoiceForm)">Create Invoice</button>
    </div>
  </form>
  <div *ngIf="calledFrom === 'transactions'" fxLayout="row">
    <button class="mr-1" mat-stroked-button color="warn" tabindex="7" type="button" (click)="onDeleteExpiredInvoices()">Delete Expired</button>
    <button mat-flat-button color="primary" tabindex="8" (click)="openCreateInvoiceModal()">Create Invoice</button>
  </div>
  <div *ngIf="calledFrom === 'transactions'" 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">Invoices 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]="invoices" [ngClass]="{'error-border': errorMessage !== ''}">
        <ng-container matColumnDef="status">
          <th *matHeaderCellDef mat-header-cell mat-sort-header arrowPosition="before" matTooltip="Status"></th>
          <td *matCellDef="let invoice" mat-cell>
            <span *ngIf="invoice?.status === 'paid'" class="dot green" matTooltip="Paid" matTooltipPosition="right" [ngClass]="{'mr-0': screenSize === screenSizeEnum.XS}"></span>
            <span *ngIf="invoice?.status === 'unpaid'" class="dot yellow" matTooltip="Unpaid" matTooltipPosition="right" [ngClass]="{'mr-0': screenSize === screenSizeEnum.XS}"></span>
            <span *ngIf="invoice?.status === 'expired'" class="dot red" matTooltip="Expired" matTooltipPosition="right" [ngClass]="{'mr-0': screenSize === screenSizeEnum.XS}"></span>
          </td>
        </ng-container>
        <ng-container matColumnDef="expires_at">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Expiry Date</th>
          <td *matCellDef="let invoice" mat-cell>
            {{(invoice?.expires_at * 1000) | date:'dd/MMM/y HH:mm'}}
          </td>
        </ng-container>
        <ng-container matColumnDef="paid_at">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Date Settled</th>
          <td *matCellDef="let invoice" mat-cell>{{((invoice?.paid_at * 1000) | date:'dd/MMM/y HH:mm') || '-'}}</td>
        </ng-container>
        <ng-container matColumnDef="type">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Type</th>
          <td *matCellDef="let invoice" mat-cell>{{ invoice?.bolt12 ? 'Bolt12' : (invoice?.bolt11 && invoice.label.includes('keysend-')) ? 'Keysend' : 'Bolt11' }}</td>
        </ng-container>
        <ng-container matColumnDef="description">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Description</th>
          <td *matCellDef="let invoice" mat-cell>
            <div class="ellipsis-parent" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
              <span class="ellipsis-child">{{invoice?.description}}</span>
            </div>
          </td>
        </ng-container>
        <ng-container matColumnDef="label">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Label</th>
          <td *matCellDef="let invoice" mat-cell>
            <div class="ellipsis-parent" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
              <span class="ellipsis-child">{{invoice?.label}}</span>
            </div>
          </td>
        </ng-container>
        <ng-container matColumnDef="payment_hash">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Payment Hash</th>
          <td *matCellDef="let invoice" mat-cell>
            <div class="ellipsis-parent" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
              <span class="ellipsis-child">{{invoice?.payment_hash}}</span>
            </div>
          </td>
        </ng-container>
        <ng-container matColumnDef="bolt11">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Invoice</th>
          <td *matCellDef="let invoice" mat-cell>
            <div class="ellipsis-parent" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
              <span class="ellipsis-child">{{invoice?.bolt11}}</span>
            </div>
          </td>
        </ng-container>
        <ng-container matColumnDef="msatoshi">
          <th *matHeaderCellDef mat-header-cell mat-sort-header arrowPosition="before">Amount (Sats)</th>
          <td *matCellDef="let invoice" mat-cell><span fxLayoutAlign="end center">{{invoice?.amount_msat/1000 | number:invoice?.amount_msat < 1000 ? '1.0-4' : '1.0-0'}}</span></td>
        </ng-container>
        <ng-container matColumnDef="msatoshi_received">
          <th *matHeaderCellDef mat-header-cell mat-sort-header arrowPosition="before">Amount Settled (Sats)</th>
          <td *matCellDef="let invoice" mat-cell><span fxLayoutAlign="end center">{{invoice?.amount_received_msat/1000 | number:invoice?.amount_received_msat < 1000 ? '1.0-4' : '1.0-0'}}</span></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 invoice" 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)="onInvoiceClick(invoice)">View Info</mat-option>
                <mat-option (click)="onRefreshInvoice(invoice)">Refresh</mat-option>
              </mat-select>
            </div>
          </td>
        </ng-container>
        <ng-container matColumnDef="no_invoice">
          <td *matFooterCellDef  mat-footer-cell colspan="4">
            <p *ngIf="(!invoices?.data || invoices?.data?.length<1) && apiCallStatus?.status === apiCallStatusEnum.COMPLETED">No invoice available.</p>
            <p *ngIf="(!invoices?.data || invoices?.data?.length<1) && apiCallStatus?.status === apiCallStatusEnum.INITIATED">Getting invoices...</p>
            <p *ngIf="(!invoices?.data || invoices?.data?.length<1) && apiCallStatus?.status === apiCallStatusEnum.ERROR">{{errorMessage}}</p>
          </td>
        </ng-container>
        <tr *matFooterRowDef="['no_invoice']" mat-footer-row [ngClass]="{'display-none': invoices?.data && invoices?.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>