<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="end start">
      <mat-label>Memo</mat-label>
      <input matInput tabindex="1" name="memo" [(ngModel)]="memo">
    </mat-form-field>
    <mat-form-field fxLayout="column" fxFlex="100" fxLayoutAlign="start end">
      <mat-label>Amount</mat-label>
      <input matInput type="number" tabindex="2" name="invValue" [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="3" type="reset" (click)="resetData()">Clear Field</button>
      <button mat-flat-button color="primary" tabindex="4" (click)="onAddInvoice(addInvoiceForm)">Create Invoice</button>
    </div>
  </form>
  <div *ngIf="calledFrom === 'transactions'" fxLayout="row">
    <button mat-flat-button color="primary" tabindex="5" (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" />
        <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" fxLayoutAlign="start end" fxFlex="100" class="table-container" [perfectScrollbar]>
      <mat-progress-bar *ngIf="apiCallStatus.status === apiCallStatusEnum.INITIATED" mode="indeterminate" />
      <table #table mat-table fxFlex="100" matSort [matSortActive]="tableSetting.sortBy" [matSortDirection]="tableSetting.sortOrder" [dataSource]="invoices" [ngClass]="{'error-border': errorMessage !== ''}">
        <ng-container matColumnDef="state">
          <th *matHeaderCellDef mat-header-cell mat-sort-header arrowPosition="before" matTooltip="State"></th>
          <td *matCellDef="let invoice" mat-cell>
            <span *ngIf="invoice?.state === 'OPEN'" class="dot grey" matTooltip="Open" matTooltipPosition="right" [ngClass]="{'mr-0': screenSize === screenSizeEnum.XS}"></span>
            <span *ngIf="invoice?.state === 'SETTLED'" class="dot green" matTooltip="Settled" matTooltipPosition="right" [ngClass]="{'mr-0': screenSize === screenSizeEnum.XS}"></span>
            <span *ngIf="invoice?.state === 'ACCEPTED'" class="dot yellow" matTooltip="Accepted" matTooltipPosition="right" [ngClass]="{'mr-0': screenSize === screenSizeEnum.XS}"></span>
            <span *ngIf="invoice?.state === 'CANCELED'" class="dot red" matTooltip="Canceled" matTooltipPosition="right" [ngClass]="{'mr-0': screenSize === screenSizeEnum.XS}"></span>
        </ng-container>
        <ng-container matColumnDef="private">
          <th *matHeaderCellDef mat-header-cell mat-sort-header arrowPosition="before" matTooltip="Private"></th>
          <td *matCellDef="let invoice" mat-cell>
            <span *ngIf="invoice.private" class="mr-1" matTooltip="Private" matTooltipPosition="right"><fa-icon [icon]="faEyeSlash" /></span>
            <span *ngIf="!invoice.private" class="mr-1" matTooltip="Public" matTooltipPosition="right"><fa-icon [icon]="faEye" /></span>
          </td>
        </ng-container>
        <ng-container matColumnDef="is_keysend">
          <th *matHeaderCellDef mat-header-cell mat-sort-header arrowPosition="before" matTooltip="Keysend"></th>
          <td *matCellDef="let invoice" mat-cell>
            <span *ngIf="invoice.is_keysend" class="mr-1" matTooltip="Keysend Invoices" matTooltipPosition="right"><fa-icon [icon]="faArrowsTurnToDots" /></span>
            <span *ngIf="!invoice.is_keysend" class="mr-1" matTooltip="Non Keysend Invoices" matTooltipPosition="right"><fa-icon [icon]="faArrowsTurnRight" /></span>
          </td>
        </ng-container>
        <ng-container matColumnDef="is_amp">
          <th *matHeaderCellDef mat-header-cell mat-sort-header arrowPosition="before" matTooltip="AMP"></th>
          <td *matCellDef="let invoice" mat-cell>
            <span *ngIf="!invoice.is_amp" class="mr-1" matTooltip="Non Atomic Payment" matTooltipPosition="right"><fa-icon [icon]="faMoneyBill1" /></span>
            <span *ngIf="invoice.is_amp" class="mr-1" matTooltip="Atomic Multi Path Payment" matTooltipPosition="right"><fa-icon [icon]="faBurst" /></span>
          </td>
        </ng-container>
        <ng-container matColumnDef="creation_date">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Date Created</th>
          <td *matCellDef="let invoice" mat-cell>
            {{(invoice?.creation_date * 1000) | date:'dd/MMM/y HH:mm'}}</td>
        </ng-container>
        <ng-container matColumnDef="settle_date">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Date Settled</th>
          <td *matCellDef="let invoice" mat-cell>{{(+invoice?.settle_date !== 0 ? ((+invoice?.settle_date * 1000) | date:'dd/MMM/y HH:mm') : '-')}}</td>
        </ng-container>
        <ng-container matColumnDef="memo">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Memo</th>
          <td *matCellDef="let invoice" mat-cell>
            <div class="ellipsis-parent" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
              <span class="ellipsis-child">{{invoice?.memo}}</span>
            </div>
          </td>
        </ng-container>
        <ng-container matColumnDef="r_preimage">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Preimage</th>
          <td *matCellDef="let invoice" mat-cell>
            <div class="ellipsis-parent" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
              <span class="ellipsis-child">{{invoice?.r_preimage}}</span>
            </div>
          </td>
        </ng-container>
        <ng-container matColumnDef="r_hash">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Preimage Hash</th>
          <td *matCellDef="let invoice" mat-cell>
            <div class="ellipsis-parent" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
              <span class="ellipsis-child">{{invoice?.r_hash}}</span>
            </div>
          </td>
        </ng-container>
        <ng-container matColumnDef="payment_addr">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Payment Address</th>
          <td *matCellDef="let invoice" mat-cell>
            <div class="ellipsis-parent" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
              <span class="ellipsis-child">{{invoice?.payment_addr}}</span>
            </div>
          </td>
        </ng-container>
        <ng-container matColumnDef="payment_request">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Payment Request</th>
          <td *matCellDef="let invoice" mat-cell>
            <div class="ellipsis-parent" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
              <span class="ellipsis-child">{{invoice?.payment_request}}</span>
            </div>
          </td>
        </ng-container>
        <ng-container matColumnDef="description_hash">
          <th *matHeaderCellDef mat-header-cell mat-sort-header>Description Hash</th>
          <td *matCellDef="let invoice" mat-cell>
            <div class="ellipsis-parent" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
              <span class="ellipsis-child">{{invoice?.description_hash}}</span>
            </div>
          </td>
        </ng-container>
        <ng-container matColumnDef="expiry">
          <th *matHeaderCellDef mat-header-cell mat-sort-header arrowPosition="before">Expiry</th>
          <td *matCellDef="let invoice" mat-cell><span fxLayoutAlign="end center">{{invoice?.expiry | number}} </span></td>
        </ng-container>
        <ng-container matColumnDef="cltv_expiry">
          <th *matHeaderCellDef mat-header-cell mat-sort-header arrowPosition="before">CLTV Expiry</th>
          <td *matCellDef="let invoice" mat-cell><span fxLayoutAlign="end center">{{invoice?.cltv_expiry | number}} </span></td>
        </ng-container>
        <ng-container matColumnDef="add_index">
          <th *matHeaderCellDef mat-header-cell mat-sort-header arrowPosition="before">Add Index</th>
          <td *matCellDef="let invoice" mat-cell><span fxLayoutAlign="end center">{{invoice?.add_index | number}} </span></td>
        </ng-container>
        <ng-container matColumnDef="settle_index">
          <th *matHeaderCellDef mat-header-cell mat-sort-header arrowPosition="before">Settle Index</th>
          <td *matCellDef="let invoice" mat-cell><span fxLayoutAlign="end center">{{invoice?.settle_index | number}} </span></td>
        </ng-container>
        <ng-container matColumnDef="value">
          <th *matHeaderCellDef mat-header-cell mat-sort-header arrowPosition="before">Amount (Sats)</th>
          <td *matCellDef="let invoice" mat-cell><span fxLayoutAlign="end center">{{invoice?.value | number}} </span></td>
        </ng-container>
        <ng-container matColumnDef="amt_paid_sat">
          <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?.amt_paid_sat | number}} </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-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-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="6">
            <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>
      <mat-paginator class="mb-1" [length]="totalInvoices" [pageSize]="pageSize" [pageSizeOptions]="pageSizeOptions" [showFirstLastButtons]="screenSize === screenSizeEnum.XS ? false : true" (page)="onPageChange($event)" />
    </div>
  </div>
</div>