<div fxLayout="column" fxFlex="100" fxLayoutAlign="start stretch" class="padding-gap">
  <form *ngIf="calledFrom === 'home'" fxLayout="row wrap" fxLayoutAlign="stretch start" fxFlex="100" #addInvoiceForm="ngForm">
    <mat-form-field fxFlex="100" fxLayoutAlign="end start">
      <input matInput [(ngModel)]="memo" placeholder="Memo" tabindex="1" name="memo">
    </mat-form-field>
    <mat-form-field fxFlex="100" fxLayoutAlign="start end">
      <input matInput [(ngModel)]="invoiceValue" (keyup)="onInvoiceValueChange()" placeholder="Amount" type="number" [step]="100" [min]="1" tabindex="2" name="invValue">
      <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" (click)="onAddInvoice(addInvoiceForm)" tabindex="4">Create Invoice</button>
    </div>
  </form>
  <div *ngIf="calledFrom === 'transactions'" fxLayout="row">
    <button mat-flat-button color="primary" (click)="openCreateInvoiceModal()" tabindex="5">Create Invoice</button>
  </div>
  <div fxLayout="column" fxLayoutAlign="start stretch" *ngIf="calledFrom === 'transactions'">
    <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">Invoices History</span>
      </div>
      <mat-form-field fxFlex="30" fxLayoutAlign="start end">
        <input matInput (keyup)="applyFilter()" [(ngModel)]="selFilter" tabindex="6" placeholder="Filter">
      </mat-form-field>
    </div>
    <div [perfectScrollbar] fxLayout="column" fxLayoutAlign="start end" 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]="invoices" matSort [ngClass]="{'overflow-auto error-border': errorMessage !== '','overflow-auto': true}">
        <ng-container matColumnDef="creation_date">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> Date Created </th>
          <td mat-cell *matCellDef="let invoice">
            <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="Cancelled" matTooltipPosition="right" [ngClass]="{'mr-0': screenSize === screenSizeEnum.XS}"></span>
            {{(invoice?.creation_date * 1000) | date:'dd/MMM/y HH:mm'}}</td>
        </ng-container>
        <ng-container matColumnDef="settle_date">
          <th mat-header-cell *matHeaderCellDef mat-sort-header class="pl-4"> Date Settled </th>
          <td mat-cell *matCellDef="let invoice" class="pl-4">{{(+invoice?.settle_date !== 0 ? ((+invoice?.settle_date * 1000) | date:'dd/MMM/y HH:mm') : '-')}}</td>
        </ng-container>
        <ng-container matColumnDef="memo">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> Memo </th>
          <td mat-cell *matCellDef="let invoice">
            <div class="ellipsis-parent" [ngStyle]="{'max-width': (screenSize === screenSizeEnum.XS) ? '10rem' : '22rem'}">
              <span class="ellipsis-child">{{invoice?.memo}}</span>
            </div>
          </td>
        </ng-container>
        <ng-container matColumnDef="value">
          <th mat-header-cell *matHeaderCellDef mat-sort-header arrowPosition="before"> Amount (Sats) </th>
          <td mat-cell *matCellDef="let invoice"><span fxLayoutAlign="end center"> {{invoice?.value | number}} </span></td>
        </ng-container>
        <ng-container matColumnDef="amt_paid_sat">
          <th mat-header-cell *matHeaderCellDef mat-sort-header arrowPosition="before"> Amount Settled (Sats) </th>
          <td mat-cell *matCellDef="let invoice"><span fxLayoutAlign="end center"> {{invoice?.amt_paid_sat | number}} </span></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 invoice" [ngClass]="{'pl-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)="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 mat-footer-cell *matFooterCellDef 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 mat-footer-row *matFooterRowDef="['no_invoice']" [ngClass]="{'display-none': invoices?.data && invoices?.data?.length>0}"></tr>
        <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: flgSticky;"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
      </table>
      <mat-paginator [length]="totalInvoices" (page)="onPageChange($event)" [pageSize]="pageSize" [pageSizeOptions]="pageSizeOptions" [showFirstLastButtons]="screenSize === screenSizeEnum.XS ? false : true" class="mb-1"></mat-paginator>
    </div>
  </div>
</div>