<div fxLayout="row">
  <div 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">{{sweepAll ? 'Sweep All Funds' : 'Send Funds'}}</span>
      </div>
      <button tabindex="8" fxFlex="5" fxLayoutAlign="center center" class="btn-close-x p-0" default mat-button [mat-dialog-close]="false">X</button>
    </mat-card-header>
    <mat-card-content class="padding-gap-x-large">
      <form *ngIf="!sweepAll;  else sweepAllBlock;" #form="ngForm" fxLayout="row wrap"fxLayoutAlign="space-between start" fxFlex="100" class="overflow-x-hidden" (submit)="onSendFunds()" (reset)="resetData()">
        <mat-form-field fxLayout="column" fxFlex="55">
          <mat-label>Bitcoin Address</mat-label>
          <input #address="ngModel" matInput autoFocus tabindex="1" name="address" required [(ngModel)]="transaction.destination">
          <mat-error *ngIf="!transaction.destination">Bitcoin address is required.</mat-error>
        </mat-form-field>
        <mat-form-field fxLayout="column" fxFlex="30">
          <mat-label>Amount</mat-label>
          <input #amount="ngModel" matInput name="amount" tabindex="2" required [type]="flgUseAllBalance ? 'text' : 'number'" [step]="100" [min]="0" [disabled]="flgUseAllBalance" [(ngModel)]="transaction.satoshi">
          <mat-hint *ngIf="flgUseAllBalance">Amount replaced by UTXO balance</mat-hint>
          <span matSuffix>{{selAmountUnit}} </span>
          <mat-error *ngIf="!transaction.satoshi">{{amountError}}</mat-error>
        </mat-form-field>
        <mat-form-field fxLayout="column" fxFlex="10" fxLayoutAlign="start end">
          <mat-select tabindex="3" required name="amountUnit" [value]="selAmountUnit" [disabled]="flgUseAllBalance" (selectionChange)="onAmountUnitChange($event)">
            <mat-option *ngFor="let amountUnit of amountUnits" [value]="amountUnit">{{amountUnit}}</mat-option>
          </mat-select>
        </mat-form-field>
        <div fxLayout="column" fxFlex="100" fxLayoutAlign="start stretch">
          <div fxLayout="column" fxLayoutAlign="space-between stretch" fxLayoutAlign.gt-sm="space-between center" fxLayout.gt-sm="row wrap">
            <div fxFlex="48" fxLayoutAlign="space-between start">
              <mat-form-field fxLayout="column" fxLayoutAlign="start center" [fxFlex]="selFeeRate === 'customperkb' && !flgMinConf ? '48' : '100'">
                <mat-label>Fee Rate</mat-label>
                <mat-select tabindex="4" [disabled]="flgMinConf" [(value)]="selFeeRate" (selectionChange)="customFeeRate=null">
                  <mat-option *ngFor="let feeRateType of feeRateTypes" [value]="feeRateType.feeRateId">
                    {{feeRateType.feeRateType}}
                  </mat-option>
                </mat-select>
              </mat-form-field>
              <mat-form-field *ngIf="selFeeRate === 'customperkb' && !flgMinConf" fxFlex="48" fxLayoutAlign="end center">
                <mat-label>Fee Rate (Sats/vByte)</mat-label>
                <input #custFeeRate="ngModel" matInput type="number" name="custFeeRate" tabindex="4" [step]="0.1" [min]="0" [required]="selFeeRate === 'customperkb' && !flgMinConf" [(ngModel)]="customFeeRate">
                <mat-error *ngIf="selFeeRate === 'customperkb' && !flgMinConf && !customFeeRate">Fee Rate is required.</mat-error>
              </mat-form-field>
            </div>
            <div fxFlex="48" fxLayout="row" fxLayoutAlign="start center">
              <mat-checkbox fxFlex="7" tabindex="5" color="primary" name="flgMinConf" fxLayoutAlign="stretch start" [ngClass]="{'mr-6': screenSize === screenSizeEnum.XS || screenSize === screenSizeEnum.SM, 'mr-2': screenSize === screenSizeEnum.MD || screenSize === screenSizeEnum.LG || screenSize === screenSizeEnum.XL}" [(ngModel)]="flgMinConf" (change)="flgMinConf ? selFeeRate=null : minConfValue=null" />
              <mat-form-field fxLayout="column" fxFlex="93">
                <mat-label>Min Confirmation Blocks</mat-label>
                <input #blocks="ngModel" matInput type="number" name="blocks" tabindex="8" [step]="1" [min]="0" [required]="flgMinConf" [disabled]="!flgMinConf" [(ngModel)]="minConfValue">
                <mat-error *ngIf="flgMinConf && !minConfValue">Min Confirmation Blocks is required.</mat-error>
              </mat-form-field>
            </div>
          </div>
          <mat-expansion-panel fxLayout="column" fxFlex="100" 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="35" fxLayoutAlign="start end">
                  <mat-label>Coin Selection</mat-label>
                  <mat-select tabindex="8" multiple [(value)]="selUTXOs" (selectionChange)="onUTXOSelectionChange($event)">
                    <mat-select-trigger>{{totalSelectedUTXOAmount | number}} Sats ({{selUTXOs.length > 1 ? selUTXOs.length + ' UTXOs' : '1 UTXO'}})</mat-select-trigger>
                    <mat-option *ngFor="let utxo of utxos" [value]="utxo">{{utxo.amount_msat/1000 | number:'1.0-0'}} Sats</mat-option>
                  </mat-select>
                </mat-form-field>
                <div fxFlex="60" fxLayout="row" fxLayoutAlign="start center">
                  <mat-slide-toggle tabindex="9" color="primary" name="flgUseAllBalance" [disabled]="selUTXOs.length < 1" [(ngModel)]="flgUseAllBalance" (change)="onUTXOAllBalanceChange()">
                    Use selected UTXOs balance
                  </mat-slide-toggle>
                  <mat-icon matTooltip="Use selected UTXOs balance as the amount to be sent. Final amount sent will be less the mining fee." matTooltipPosition="above" class="info-icon">info_outline</mat-icon>
                </div>
              </div>
            </div>
          </mat-expansion-panel>    
        <div fxLayout="column" fxFlex="100" fxLayoutAlign="start stretch"></div>
        <div *ngIf="sendFundError !== ''" fxFlex="100" class="alert alert-danger mt-1">
          <fa-icon class="mr-1 alert-icon" [icon]="faExclamationTriangle" />
          <span *ngIf="sendFundError !== ''">{{sendFundError}}</span>
        </div>
        <div class="mt-2" fxLayout="row" fxFlex="100" fxLayoutAlign="end center">
          <button class="mr-1" mat-button color="primary" tabindex="7" type="reset">Clear Fields</button>
          <button mat-button color="primary" type="submit" tabindex="8">Send Funds</button>
        </div>
      </div>
      </form>
    </mat-card-content>
  </div>
</div>

<ng-template #sweepAllBlock>
  <div fxLayout="column">
    <mat-vertical-stepper #stepper [linear]="true" (selectionChange)="stepSelectionChanged($event)">
      <mat-step *ngIf="!appConfig.sso.rtlSSO" [stepControl]="passwordFormGroup" [editable]="flgEditable">
        <form fxLayout="column" fxLayoutAlign="space-between" class="my-1 pr-1" [formGroup]="passwordFormGroup">
          <ng-template matStepLabel>{{passwordFormLabel}}</ng-template>
          <div fxLayout="row">
            <mat-form-field fxLayout="column" fxFlex="100">
              <mat-label>Password</mat-label>
              <input autoFocus matInput type="password" tabindex="1" formControlName="password" required>
              <mat-error *ngIf="passwordFormGroup.controls.password.errors?.required">Password is required.</mat-error>
            </mat-form-field>
          </div>
          <div class="mt-2" fxLayout="row">
            <button mat-button color="primary" tabindex="3" type="default" (click)="onAuthenticate()">Confirm</button>
          </div>
        </form>
      </mat-step>
      <mat-step [stepControl]="sendFundFormGroup" [editable]="flgEditable">
        <form fxLayout="column" class="my-1 pr-1" [formGroup]="sendFundFormGroup">
          <ng-template matStepLabel disabled="true">{{sendFundFormLabel}}</ng-template>
          <div fxLayout="column" fxFlex="100" fxLayoutAlign="start stretch">
            <mat-form-field fxLayout="column" fxFlex="100">
              <mat-label>Bitcoin Address</mat-label>
              <input matInput formControlName="transactionAddress" tabindex="4" name="address" required>
              <mat-error *ngIf="sendFundFormGroup.controls.transactionAddress.errors?.required">Bitcoin address is required.</mat-error>
            </mat-form-field>
            <div fxLayout="column" fxLayoutAlign="space-between stretch" fxLayout.gt-sm="row wrap" fxFlex="100" fxLayoutAlign.gt-sm="space-between center">
              <div fxFlex="48" fxLayoutAlign="space-between start">
                <mat-form-field fxLayout="column" fxLayoutAlign="start center" [fxFlex]="sendFundFormGroup.controls.selFeeRate.value === 'customperkb' && !sendFundFormGroup.controls.flgMinConf.value ? '48' : '100'">
                  <mat-label>Fee Rate</mat-label>
                  <mat-select tabindex="4" formControlName="selFeeRate">
                    <mat-option *ngFor="let feeRateType of feeRateTypes" [value]="feeRateType.feeRateId">
                      {{feeRateType.feeRateType}}
                    </mat-option>
                  </mat-select>
                </mat-form-field>
                <mat-form-field *ngIf="sendFundFormGroup.controls.selFeeRate.value === 'customperkb' && !sendFundFormGroup.controls.flgMinConf.value" fxFlex="48" fxLayoutAlign="end center">
                  <mat-label>Fee Rate (Sats/vByte)</mat-label>
                  <input matInput formControlName="customFeeRate" type="number" name="custFeeRate" tabindex="4" [step]="0.1" [min]="0">
                  <mat-error *ngIf="sendFundFormGroup.controls.selFeeRate.value === 'customperkb' && !sendFundFormGroup.controls.flgMinConf.value && !sendFundFormGroup.controls.customFeeRate.value">Fee Rate is required.</mat-error>
                </mat-form-field>
              </div>
              <div fxFlex="48" fxLayout="row" fxLayoutAlign="start center">
                <mat-checkbox fxFlex="7" tabindex="5" color="primary" formControlName="flgMinConf" fxLayoutAlign="stretch start" [ngClass]="{'mr-6': screenSize === screenSizeEnum.XS || screenSize === screenSizeEnum.SM, 'mr-2': screenSize === screenSizeEnum.MD || screenSize === screenSizeEnum.LG || screenSize === screenSizeEnum.XL}" />
                <mat-form-field fxLayout="column" fxFlex="93">
                  <mat-label>Min Confirmation Blocks</mat-label>
                  <input matInput formControlName="minConfValue" type="number" name="blocks" tabindex="8" [step]="1" [min]="0" [required]="sendFundFormGroup.controls.flgMinConf.value">
                  <mat-error *ngIf="sendFundFormGroup.controls.flgMinConf.value && !sendFundFormGroup.controls.minConfValue.value">Min Confirmation Blocks is required.</mat-error>
                </mat-form-field>
              </div>
            </div>
          </div>
          <div class="mt-2" fxLayout="row" fxLayoutAlign="start center" fxFlex="100">
            <button mat-button color="primary" tabindex="8" type="default" matStepperNext>Next</button>
          </div>
        </form>
      </mat-step>
      <mat-step [stepControl]="confirmFormGroup">
        <form fxLayout="column" fxLayoutAlign="start" class="my-1 pr-1" [formGroup]="confirmFormGroup">
          <ng-template matStepLabel>{{confirmFormLabel}}</ng-template>
          <div fxLayout="column">
            <div fxFlex="100" class="w-100 alert alert-warn">
              <fa-icon class="mt-1 mr-1 alert-icon" [icon]="faExclamationTriangle" />
              <span>You are about to sweep all funds from RTL. Are you sure?</span>
            </div>
            <div *ngIf="sendFundError !== ''" fxFlex="100" class="alert alert-danger mt-1">
              <fa-icon class="mr-1 alert-icon" [icon]="faExclamationTriangle" />
              <span *ngIf="sendFundError !== ''">{{sendFundError}}</span>
            </div>
            <div class="mt-2" fxLayout="row" fxLayoutAlign="start center" fxFlex="100">
              <button mat-button color="primary" tabindex="9" type="button" (click)="onSendFunds()">Sweep All Funds</button>
            </div>
          </div>
        </form>
      </mat-step>
    </mat-vertical-stepper>
    <div fxLayout="row" fxFlex="100" fxLayoutAlign="end center">
      <button mat-button color="primary" tabindex="12" type="button" default [mat-dialog-close]="false">{{flgValidated ? 'Close' : 'Cancel'}}</button>
    </div>
  </div>
</ng-template>
