<div fxLayout="column" fxFlex="100" fxLayoutAlign="space-between stretch" class="padding-gap">
  <form [fxLayout]="showDetails ? 'column' : 'row wrap'" fxLayoutAlign.gt-sm="space-between center" fxLayoutAlign="space-between stretch" fxLayout.gt-sm="row wrap" fxFlex="100" #addInvoiceForm="ngForm">
    <mat-form-field [fxFlex]="showDetails ? '49' : '100'" fxLayoutAlign="space-between stretch">
      <input matInput [(ngModel)]="label" placeholder="Label" tabindex="1" name="label" required>
      <mat-error *ngIf="!label">Label is required.</mat-error>
    </mat-form-field>
    <mat-form-field *ngIf="showDetails" fxFlex="49" fxLayoutAlign="space-between stretch">
      <input matInput [(ngModel)]="description" placeholder="Description" tabindex="2" name="description">
    </mat-form-field>
    <mat-form-field [fxFlex]="showDetails ? '40' : '100'" fxLayoutAlign="start end">
      <input matInput [(ngModel)]="invoiceValue" (keyup)="onInvoiceValueChange()" placeholder="Amount" type="number" step="100" min="1" tabindex="3" name="invoiceValue" required>
      <span matSuffix> {{information?.smaller_currency_unit}} </span>
      <mat-hint>{{invoiceValueHint}}</mat-hint>
      <mat-error *ngIf="!invoiceValue">Amount is required.</mat-error>
    </mat-form-field>
    <mat-form-field fxFlex="15" fxLayoutAlign="start end" *ngIf="showDetails" [ngClass]="{'mr-2': screenSize === screenSizeEnum.LG}">
      <input matInput [(ngModel)]="expiry" placeholder="Expiry" type="number" step="{{selTimeUnit === timeUnitEnum.SECS ? 300 : selTimeUnit === timeUnitEnum.MINS ? 10 : selTimeUnit === timeUnitEnum.HOURS ? 2 : 1}}" min="1" tabindex="4" name="expiry">
      <span matSuffix> {{selTimeUnit | titlecase}} </span>
    </mat-form-field>
    <mat-form-field fxFlex="15" fxLayoutAlign="start end" *ngIf="showDetails" [ngClass]="{'mr-2': screenSize === screenSizeEnum.LG}">
      <mat-select [value]="selTimeUnit" tabindex="5" name="timeUnit" (selectionChange)="onTimeUnitChange($event)">
        <mat-option *ngFor="let timeUnit of timeUnits" [value]="timeUnit">{{timeUnit | titlecase}}</mat-option>
      </mat-select>
    </mat-form-field>
    <div fxFlex="23" tabindex="6" fxLayoutAlign="start center" *ngIf="showDetails">
      <mat-slide-toggle color="primary" [(ngModel)]="private" matTooltip="Include routing hints for private channels" [matTooltipPosition]="'above'" name="private">Private Routing Hints</mat-slide-toggle>
    </div>
    <div fxLayout="row" fxFlex="100" fxFlex.gt-sm="40" fxLayoutAlign="space-between start" *ngIf="showDetails" class="mt-2">
      <button fxFlex="31" fxLayoutAlign="center center" mat-stroked-button color="warn" tabindex="11" type="button" (click)="onDeleteExpiredInvoices()">Delete Expired</button>
      <button fxFlex="31" fxLayoutAlign="center center" mat-stroked-button color="primary" tabindex="7" type="reset" (click)="resetData()">Clear Field</button>
      <button fxFlex="31" fxLayoutAlign="center center" mat-raised-button color="primary" (click)="onAddInvoice(addInvoiceForm)" tabindex="8">Create Invoice</button>
    </div>
    <div  fxLayout="row" fxFlex="100" fxLayoutAlign="space-between stretch" *ngIf="!showDetails" class="mt-1">
      <button fxFlex="48" fxLayoutAlign="center center" mat-stroked-button color="primary" tabindex="9" type="reset" (click)="resetData()">Clear Field</button>
      <button fxFlex="48" fxLayoutAlign="center center" mat-raised-button color="primary" (click)="onAddInvoice(addInvoiceForm)" tabindex="10">Create Invoice</button>
    </div>
  </form>
  <div fxLayout="column" fxLayoutAlign="start stretch" *ngIf="showDetails">
    <div fxLayout="column" fxLayout.gt-xs="row" fxLayoutAlign.gt-xs="start center" fxLayoutAlign="start stretch" class="padding-gap-x page-sub-title-container mt-2">
      <div fxFlex="70">
        <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">
        <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
      </mat-form-field>
    </div>
    <div perfectScrollbar class="table-container">
      <mat-progress-bar *ngIf="flgLoading[0]===true" mode="indeterminate"></mat-progress-bar>
      <table mat-table #table [dataSource]="invoices" matSort [ngClass]="{'overflow-auto error-border': flgLoading[0]==='error','overflow-auto': true}">
        <ng-container matColumnDef="expires_at_str">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> Expiry Date </th>
          <td mat-cell *matCellDef="let invoice">
            <span *ngIf="invoice.status === 'paid'" class="dot green" matTooltip="Settled" matTooltipPosition="right" [ngClass]="{'mr-0': screenSize === screenSizeEnum.XS}"></span>
            <span *ngIf="invoice.status !== 'paid'" class="dot yellow" matTooltip="Unsettled" matTooltipPosition="right" [ngClass]="{'mr-0': screenSize === screenSizeEnum.XS}"></span>
            {{invoice.expires_at_str}}
          </td>
        </ng-container>
        <ng-container matColumnDef="paid_at_str">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> Paid Date </th>
          <td mat-cell *matCellDef="let invoice">{{invoice.paid_at_str}}</td>
        </ng-container>
        <ng-container matColumnDef="label">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> Label </th>
          <td mat-cell *matCellDef="let invoice">{{invoice.label}}</td>
        </ng-container>
        <ng-container matColumnDef="pay_index">
          <th mat-header-cell *matHeaderCellDef mat-sort-header arrowPosition="before"> Pay Index </th>
          <td mat-cell *matCellDef="let invoice"><span fxLayoutAlign="end center"> {{invoice?.pay_index | number}}
            </span></td>
        </ng-container>
        <ng-container matColumnDef="msatoshi">
          <th mat-header-cell *matHeaderCellDef mat-sort-header arrowPosition="before"> Amount (Sats) </th>
          <td mat-cell *matCellDef="let invoice"><span fxLayoutAlign="end center"> {{invoice.msatoshi/1000 | number:'1.0-0'}}
            </span></td>
        </ng-container>
        <ng-container matColumnDef="msatoshi_received">
          <th mat-header-cell *matHeaderCellDef mat-sort-header arrowPosition="before"> Received mSatoshi </th>
          <td mat-cell *matCellDef="let invoice"><span fxLayoutAlign="end center">
              {{invoice.msatoshi_received | number}} </span></td>
        </ng-container>
        <ng-container matColumnDef="description">
          <th mat-header-cell class="pl-4" *matHeaderCellDef mat-sort-header> Description </th>
          <td mat-cell class="pl-4" *matCellDef="let invoice">{{invoice.description}}</td>
        </ng-container>
        <ng-container matColumnDef="actions">
          <th mat-header-cell *matHeaderCellDef class="pl-3 pr-3"><span fxLayoutAlign="end center">Actions</span></th>        
          <td mat-cell *matCellDef="let invoice" [ngClass]="{'pl-3': screenSize !== screenSizeEnum.XS}">
            <button mat-stroked-button color="primary" type="button" tabindex="4" (click)="onInvoiceClick(invoice, $event)">View Info</button>          
          </td>
        </ng-container>
        <ng-container matColumnDef="no_invoice">
          <td mat-footer-cell *matFooterCellDef colspan="4">
            <p *ngIf="!invoices.data || invoices.data.length<1">No invoices available.</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;" [@newlyAddedRowAnimation]="(row.label == newlyAddedInvoiceMemo && row.value == newlyAddedInvoiceValue && flgAnimate) ? 'added' : 'notAdded'" class="row-invoices"></tr>
      </table>
      <mat-paginator [length]="totalInvoices" [pageSize]="pageSize" [pageSizeOptions]="pageSizeOptions" (page)="onPageChange($event)" [showFirstLastButtons]="screenSize === screenSizeEnum.XS ? false : true" class="mb-4"></mat-paginator>
    </div>
  </div>
</div>