<div fxLayout="row" fxLayoutAlign="start center">
  <div fxLayout="column" fxFlex="100">
    <mat-card-header fxLayout="row" fxLayoutAlign="space-between center" class="modal-info-header">
      <div fxFlex="95" fxLayoutAlign="start start">
        <span class="page-title">{{alertTitle}}</span>
      </div>
      <button tabindex="8" fxFlex="5" fxLayoutAlign="center center" class="btn-close-x p-0" mat-button (click)="onClose()">X</button>
    </mat-card-header>
    <mat-card-content class="padding-gap-x-large">
      <form #form="ngForm" fxLayout="column" (submit)="onOpenChannel()" (reset)="resetData()">
        <div fxLayout="column">
          <mat-form-field *ngIf="!peer && peers && peers.length > 0" fxFlex="100">
            <mat-label>Peer Alias</mat-label>
            <input type="text" aria-label="Peers" matInput tabindex="1" required [formControl]="selectedPeer" [matAutocomplete]="auto">
            <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn" (optionSelected)="onSelectedPeerChanged()">
              <mat-option *ngFor="let peer of filteredPeers | async" [value]="peer">{{peer.alias ? peer.alias : peer.pub_key ? peer.pub_key : ''}}</mat-option>
            </mat-autocomplete>
            <mat-error *ngIf="selectedPeer.errors?.required">Peer alias is required.</mat-error>
            <mat-error *ngIf="selectedPeer.errors?.notfound">Peer not found in the list.</mat-error>
          </mat-form-field>
        </div>
        <ng-container *ngTemplateOutlet="peerDetailsExpansionBlock"></ng-container>
        <div fxLayout="column">
          <div fxLayout="row" fxFlex="100" fxLayoutAlign="space-between center">
            <mat-form-field fxLayout="column" fxFlex="60" fxLayoutAlign="start end">
              <mat-label>Amount</mat-label>
              <input #amt="ngModel" matInput type="number" tabindex="1" required name="amnt" [step]="1000" [min]="1" [max]="totalBalance" [(ngModel)]="fundingAmount">
              <mat-hint>(Remaining: {{totalBalance - ((fundingAmount) ? fundingAmount : 0) | number}})</mat-hint>
              <span matSuffix> Sats </span>
              <mat-error *ngIf="amount.errors?.required">Amount is required.</mat-error>
              <mat-error *ngIf="amount.errors?.max">Amount must be less than or equal to {{totalBalance}}.</mat-error>
            </mat-form-field>
            <div fxFlex="35" fxLayoutAlign="start center">
              <mat-slide-toggle tabindex="2" color="primary" name="isPrivate" [(ngModel)]="isPrivate">Private Channel</mat-slide-toggle>
            </div>
          </div>
          <mat-expansion-panel fxLayout="column" fxFlex="100" fxLayoutAlign="start stretch" class="flat-expansion-panel mt-2" expanded="false" (closed)="onAdvancedPanelToggle(true)" (opened)="onAdvancedPanelToggle(false)">
            <mat-expansion-panel-header>
              <mat-panel-title>
                <span>{{advancedTitle}}</span>
              </mat-panel-title>
            </mat-expansion-panel-header>
            <div fxLayout="column" fxFlex="100" fxLayoutAlign="start stretch">
              <div fxLayout="row" fxFlex="100" fxLayoutAlign="space-between center">
                <mat-form-field fxLayout="column" fxFlex="49">
                  <mat-select tabindex="3" [(value)]="selTransType">
                    <mat-option *ngFor="let transType of transTypes" [value]="transType.id">
                      {{transType.name}}
                    </mat-option>
                  </mat-select>
                </mat-form-field>
                <mat-form-field fxLayout="column" fxFlex="49">
                  <mat-label>{{selTransType==='0' ? 'Default' : selTransType==='1' ? 'Target Confirmation Blocks' : 'Fee (Sats/vByte)'}}</mat-label>
                  <input #transTypeVal="ngModel" matInput type="number" tabindex="4" name="transTpValue" [required]="selTransType!=='0'" [disabled]="selTransType==='0'" [step]="1" [min]="0" [(ngModel)]="transTypeValue">
                  <mat-error *ngIf="selTransType!=='0' && !transTypeValue">{{selTransType === '1' ? 'Target Confirmation Blocks' : 'Fee'}} is required.</mat-error>
                </mat-form-field>
              </div>
              <div fxLayout="row" fxFlex="100" fxLayoutAlign="space-between center">
                <div fxFlex="50" fxLayoutAlign="start center">
                  <mat-slide-toggle tabindex="6" color="primary" name="spendUnconfirmed" [(ngModel)]="spendUnconfirmed">Spend Unconfirmed Output</mat-slide-toggle>
                </div>
              </div>
            </div>
          </mat-expansion-panel>    
        </div>
        <div *ngIf="channelConnectionError !== ''" fxFlex="100" class="alert alert-danger mt-1">
          <fa-icon class="mr-1 alert-icon" [icon]="faExclamationTriangle"></fa-icon>
          <span *ngIf="channelConnectionError !== ''">{{channelConnectionError}}</span>
        </div>
        <div class="mt-2" fxLayout="row" fxLayoutAlign="end center">
          <button mat-button color="primary" class="mr-1" tabindex="7" type="reset">Clear Fields</button>
          <button autoFocus mat-button color="primary" type="submit" tabindex="9">Open Channel</button>
        </div>
      </form>
    </mat-card-content>
  </div>
</div>

<ng-template #peerDetailsExpansionBlock>
  <mat-expansion-panel *ngIf="peer" class="flat-expansion-panel my-1" expanded="false">
    <mat-expansion-panel-header>
      <mat-panel-title>
        <span>Peer: &nbsp;</span><strong class="font-weight-900">{{peer?.alias || peer?.address}}</strong>
      </mat-panel-title>
    </mat-expansion-panel-header>
    <div fxLayout="column">
      <div fxLayout="row">
        <div fxFlex="100">
          <h4 fxLayoutAlign="start" class="font-bold-500">Pubkey</h4>
          <span class="foreground-secondary-text">{{peer.pub_key}}</span>
        </div>
      </div>
      <mat-divider class="w-100 my-1"></mat-divider>
      <div fxLayout="row">
        <div fxFlex="50">
          <h4 fxLayoutAlign="start" class="font-bold-500">Address</h4>
          <span class="overflow-wrap foreground-secondary-text">{{peer?.address}}</span>
        </div>
        <div fxFlex="50">
          <h4 fxLayoutAlign="start" class="font-bold-500">Inbound</h4>
          <span class="overflow-wrap foreground-secondary-text">{{peer?.inbound ? 'True' : 'False'}}</span>
        </div>
      </div>
    </div>
  </mat-expansion-panel>
</ng-template>
