<div fxLayout="column" fxFlex="100" fxLayoutAlign="space-between stretch" class="padding-gap">
  <form *ngIf="calledFrom === 'home'" #sendPaymentForm="ngForm" fxLayout="column" fxLayoutAlign="space-between stretch" fxLayout.gt-sm="row wrap">
    <mat-form-field fxLayout="column" fxFlex="100">
      <mat-label>Payment Request</mat-label>
      <textarea #paymentReq="ngModel" matInput name="paymentRequest" tabindex="1" required [perfectScrollbar] [ngModel]="paymentRequest" (ngModelChange)="onPaymentRequestEntry($event)" (matTextareaAutosize)="true"></textarea>
      <mat-hint *ngIf="paymentRequest && paymentDecodedHint !== ''">{{paymentDecodedHint}}</mat-hint>
      <mat-error *ngIf="!paymentRequest">Payment request is required.</mat-error>
    </mat-form-field>
    <div fxLayout="row" class="mt-3">
      <button class="mr-1" mat-stroked-button color="primary" tabindex="2" type="reset" (click)="resetData()">Clear Field</button>
      <button mat-flat-button color="primary" tabindex="3" (click)="onSendPayment()">Send Payment</button>
    </div>
  </form>
  <div *ngIf="calledFrom === 'transactions'" fxLayout="row">
    <button mat-flat-button color="primary" tabindex="3" (click)="openSendPaymentModal()">Send Payment</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">Payments 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="row" fxLayoutAlign="start start">
      <div fxLayout="column" 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]="payments" [ngClass]="{'error-border': errorMessage !== ''}">
          <ng-container matColumnDef="status">
            <th *matHeaderCellDef mat-header-cell mat-sort-header arrowPosition="before" matTooltip="Status"></th>
            <td *matCellDef="let payment" mat-cell>
              <span *ngIf="payment.status === 'complete'" class="dot green" matTooltip="Completed" matTooltipPosition="right"></span>
              <span *ngIf="payment.status !== 'complete'" class="dot yellow" matTooltip="Incomplete/Failed" matTooltipPosition="right"></span>
            </td>
          </ng-container>
          <ng-container matColumnDef="created_at">
            <th *matHeaderCellDef mat-header-cell mat-sort-header>Created At</th>
            <td *matCellDef="let payment" mat-cell>
              {{(payment?.created_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 payment" mat-cell>{{ payment?.bolt12 ? 'Bolt12' : payment?.bolt11 ? 'Bolt11' : 'Keysend' }}</td>
          </ng-container>
          <ng-container matColumnDef="payment_hash">
            <th *matHeaderCellDef mat-header-cell mat-sort-header>Payment Hash</th>
            <td *matCellDef="let payment" mat-cell>
              <span fxLayout.gt-xs="row" class="ellipsis-parent" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
                <span class="ellipsis-child">{{payment?.payment_hash}}</span>
              </span>
            </td>
          </ng-container>
          <ng-container matColumnDef="bolt11">
            <th *matHeaderCellDef mat-header-cell mat-sort-header>Invoice</th>
            <td *matCellDef="let payment" mat-cell>
              <span fxLayout.gt-xs="row" class="ellipsis-parent" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
                <span class="ellipsis-child">{{payment?.bolt11}}</span>
              </span>
            </td>
          </ng-container>
          <ng-container matColumnDef="label">
            <th *matHeaderCellDef mat-header-cell mat-sort-header>Label</th>
            <td *matCellDef="let payment" mat-cell>
              <span fxLayout.gt-xs="row" class="ellipsis-parent" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
                <span class="ellipsis-child">{{payment?.label}}</span>
              </span>
            </td>
          </ng-container>
          <ng-container matColumnDef="destination">
            <th *matHeaderCellDef mat-header-cell mat-sort-header>Destination</th>
            <td *matCellDef="let payment" mat-cell>
              <span fxLayout.gt-xs="row" class="ellipsis-parent" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
                <span class="ellipsis-child">{{payment?.destination}}</span>
              </span>
            </td>
          </ng-container>
          <ng-container matColumnDef="memo">
            <th *matHeaderCellDef mat-header-cell mat-sort-header>Memo</th>
            <td *matCellDef="let payment" mat-cell>
              <span fxLayout.gt-xs="row" class="ellipsis-parent" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
                <span class="ellipsis-child">{{payment?.memo}}</span>
              </span>
            </td>
          </ng-container>
          <ng-container matColumnDef="msatoshi_sent">
            <th *matHeaderCellDef mat-header-cell mat-sort-header arrowPosition="before">Sats Sent</th>
            <td *matCellDef="let payment" mat-cell><span fxLayoutAlign="end center">{{payment?.amount_sent_msat/1000 | number:'1.0-4'}}</span></td>
          </ng-container>
          <ng-container matColumnDef="msatoshi">
            <th *matHeaderCellDef mat-header-cell mat-sort-header arrowPosition="before">Sats Received</th>
            <td *matCellDef="let payment" mat-cell><span fxLayoutAlign="end center">{{payment?.amount_msat/1000 | number:'1.0-4'}}</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 payment" mat-cell fxLayoutAlign="end center">
              <button mat-stroked-button color="primary" type="button" tabindex="4" class="table-actions-button" (click)="onPaymentClick(payment)">View Info</button>
            </td>
          </ng-container>
          <ng-container matColumnDef="no_payment">
            <td *matFooterCellDef  mat-footer-cell colspan="4">
              <p *ngIf="(!payments?.data || !payments?.data?.length) && apiCallStatus?.status === apiCallStatusEnum.COMPLETED">No payment available.</p>
              <p *ngIf="(!payments?.data || !payments?.data?.length) && apiCallStatus?.status === apiCallStatusEnum.INITIATED">Getting payments...</p>
              <p *ngIf="(!payments?.data || !payments?.data?.length) && apiCallStatus?.status === apiCallStatusEnum.ERROR">{{errorMessage}}</p>
            </td>
          </ng-container>

          <!-- Payment Group Row Start -->
          <ng-container matColumnDef="group_status">
            <td *matCellDef="let payment" mat-cell>
              <span fxLayoutAlign="start center" class="mpp-row-span">
                <span *ngIf="payment.status === 'complete'" class="dot green" matTooltip="Completed" matTooltipPosition="right"></span>
                <span *ngIf="payment.status !== 'complete'" class="dot yellow" matTooltip="Incomplete/Failed" matTooltipPosition="right"></span>
              </span>
              <ng-container *ngIf="payment.is_expanded">
                <span *ngFor="let mpp of payment?.mpps" fxLayoutAlign="start center" class="mpp-row-span">
                  <span *ngIf="mpp.status === 'complete'" class="dot green" matTooltip="Completed" matTooltipPosition="right"></span>
                  <span *ngIf="mpp.status !== 'complete'" class="dot yellow" matTooltip="Incomplete/Failed" matTooltipPosition="right"></span>
                </span>
              </ng-container>
            </td>
          </ng-container>
          <ng-container matColumnDef="group_created_at">
            <td *matCellDef="let payment" mat-cell>
              <span fxLayoutAlign="start center" class="mpp-row-span">
                Total Attempts: {{payment?.total_parts}}
              </span>
              <ng-container *ngIf="payment.is_expanded">
                <span *ngFor="let mpp of payment?.mpps" fxLayoutAlign="start center" class="mpp-row-span">
                  {{(mpp.created_at * 1000) | date:'dd/MMM/y HH:mm'}}
                </span>
              </ng-container>
            </td>
          </ng-container>
          <ng-container matColumnDef="group_type">
            <td *matCellDef="let payment" mat-cell>
              <span fxLayoutAlign="start center" class="mpp-row-span">{{ payment?.bolt12 ? 'Bolt12' : payment?.bolt11 ? 'Bolt11' : 'Keysend' }}</span>
              <span *ngIf="payment.is_expanded"><span *ngFor="let mpp of payment?.mpps" fxLayoutAlign="start center" class="mpp-row-span"></span></span>
            </td>
          </ng-container>
          <ng-container matColumnDef="group_payment_hash">
            <td *matCellDef="let payment" mat-cell>
              <span fxLayout="row" class="ellipsis-parent mpp-row-span" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
                <span class="ellipsis-child">{{payment?.payment_hash}}</span>
              </span>
              <span *ngIf="payment.is_expanded">
                <span *ngFor="let mpp of payment?.mpps" fxLayoutAlign="start center" class="mpp-row-span">
                  Part ID {{mpp.partid ? mpp.partid : 0}}
                </span>
              </span>
            </td>
          </ng-container>
          <ng-container matColumnDef="group_bolt11">
            <td *matCellDef="let payment" mat-cell>
              <span fxLayout="row" class="ellipsis-parent mpp-row-span" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
                <span class="ellipsis-child">{{payment?.bolt11}}</span>
              </span>
              <span *ngIf="payment.is_expanded">
                <span *ngFor="let mpp of payment?.mpps" fxLayoutAlign="start center" class="ellipsis-parent mpp-row-span" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
                  <span class="ellipsis-child"></span>
                </span>
              </span>
            </td>
          </ng-container>
          <ng-container matColumnDef="group_label">
            <td *matCellDef="let payment" mat-cell>
              <span fxLayout="row" class="ellipsis-parent mpp-row-span" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
                <span class="ellipsis-child">{{payment?.label}}</span>
              </span>
              <span *ngIf="payment.is_expanded">
                <span *ngFor="let mpp of payment?.mpps" fxLayoutAlign="start center" class="ellipsis-parent mpp-row-span" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
                  <span class="ellipsis-child"></span>
                </span>
              </span>
            </td>
          </ng-container>
          <ng-container matColumnDef="group_destination">
            <td *matCellDef="let payment" mat-cell>
              <span fxLayout="row" class="ellipsis-parent mpp-row-span" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
                <span class="ellipsis-child">{{payment?.destination}}</span>
              </span>
              <span *ngIf="payment.is_expanded">
                <span *ngFor="let mpp of payment?.mpps" fxLayoutAlign="start center" class="ellipsis-parent mpp-row-span" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
                  <span class="ellipsis-child"></span>
                </span>
              </span>
            </td>
          </ng-container>
          <ng-container matColumnDef="group_memo">
            <td *matCellDef="let payment" mat-cell>
              <span fxLayout="row" class="ellipsis-parent mpp-row-span" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
                <span class="ellipsis-child">{{payment?.memo}}</span>
              </span>
              <span *ngIf="payment.is_expanded">
                <span *ngFor="let mpp of payment?.mpps" fxLayoutAlign="start center" class="ellipsis-parent mpp-row-span" [ngStyle]="{'width': (screenSize === screenSizeEnum.XS) ? '6rem' : colWidth}">
                  <span class="ellipsis-child"></span>
                </span>
              </span>
            </td>
          </ng-container>
          <ng-container matColumnDef="group_msatoshi_sent">
            <td *matCellDef="let payment" mat-cell>
              <span fxLayoutAlign="end center" class="mpp-row-span">{{payment?.amount_sent_msat/1000 | number:payment?.amount_sent_msat < 1000 ? '1.0-4' : '1.0-0'}}</span>
              <span *ngIf="payment.is_expanded">
                <span *ngFor="let mpp of payment?.mpps" fxLayoutAlign="end center" class="mpp-row-span">
                  {{mpp.amount_sent_msat/1000 | number:mpp.amount_sent_msat < 1000 ? '1.0-4' : '1.0-0'}}
                </span>
              </span>
            </td>
          </ng-container>
          <ng-container matColumnDef="group_msatoshi">
            <td *matCellDef="let payment" mat-cell>
              <span fxLayoutAlign="end center" class="mpp-row-span">{{payment?.amount_msat/1000 | number:payment?.amount_msat < 1000 ? '1.0-4' : '1.0-0'}}</span>
              <span *ngIf="payment.is_expanded">
                <span *ngFor="let mpp of payment?.mpps" fxLayoutAlign="end center" class="mpp-row-span">
                  {{mpp.amount_msat/1000 | number:mpp.amount_msat < 1000 ? '1.0-4' : '1.0-0'}}
                </span>
              </span>
            </td>
          </ng-container>
          <ng-container matColumnDef="group_actions">
            <td *matCellDef="let payment" mat-cell>
              <span fxLayoutAlign="end center" class="mpp-group-head">
                <button mat-flat-button class="btn-mpp-expand" color="primary" type="button" tabindex="5" (click)="payment.is_expanded = !payment.is_expanded">{{payment.is_expanded ? 'Hide' : 'Show'}}</button>
              </span>
              <div *ngIf="payment.is_expanded">
                <div *ngFor="let mpp of payment?.mpps; index as i" class="mpp-group-details" fxLayoutAlign="end center">
                  <button mat-stroked-button class="btn-mpp-info" color="primary" type="button" tabindex="6" (click)="onPaymentClick(mpp)">View {{mpp.partid ? mpp.partid : 0}}</button>
                </div>
              </div>
            </td>
          </ng-container>
          <tr *matRowDef="let row; columns: mppColumns; when: is_group;" mat-row></tr>
          <!-- Payment Group Row End -->
          <tr *matFooterRowDef="['no_payment']" mat-footer-row [ngClass]="{'display-none': payments?.data && payments?.data?.length && payments?.data?.length>0}"></tr>
          <tr *matHeaderRowDef="displayedColumns" mat-header-row></tr>
          <tr *matRowDef="let row; columns: displayedColumns; when: !is_group;" mat-row></tr>
        </table>
      </div>
    </div>
    <mat-paginator class="mb-1" [pageSize]="pageSize" [pageSizeOptions]="pageSizeOptions" [showFirstLastButtons]="screenSize === screenSizeEnum.XS ? false : true" />
  </div>
</div>
