<!--

    Copyright © 2016-2022 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form [formGroup]="persistentFormGroup" (ngSubmit)="save()" style="min-width: 480px; max-width: 600px;">
  <mat-toolbar color="primary">
    <h2>{{ 'widgets.persistent-table.add-title' | translate }}</h2>
    <span fxFlex></span>
    <button mat-icon-button
            (click)="close()"
            type="button">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  <mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async">
  </mat-progress-bar>
  <div style="height: 4px;" *ngIf="!(isLoading$ | async)"></div>
  <div mat-dialog-content class="add-dialog">
    <fieldset [disabled]="isLoading$ | async">
      <div fxLayout="row" fxLayoutGap="6px">
        <mat-slide-toggle fxFlex class="mat-block" formControlName="oneWayElseTwoWay">
          {{ rpcMessageTypeText }}
        </mat-slide-toggle>
      </div>
      <div fxLayout="row wrap" fxLayout.xs="column" fxLayoutGap="6px">
        <mat-form-field fxFlex class="mat-block">
          <mat-label translate>widgets.persistent-table.method</mat-label>
          <input matInput formControlName="method" required>
          <mat-error *ngIf="this.persistentFormGroup.get('method').hasError('required')">
            {{'widgets.persistent-table.method-error' | translate}}
          </mat-error>
          <mat-error *ngIf="this.persistentFormGroup.get('method').hasError('pattern')">
            {{'widgets.persistent-table.white-space-error' | translate}}
          </mat-error>
        </mat-form-field>
        <mat-form-field fxFlex class="mat-block">
          <mat-label translate>widgets.persistent-table.retries</mat-label>
          <input matInput type="number" formControlName="retries">
        </mat-form-field>
      </div>
      <div class="params-json-editor">
        <tb-json-object-edit formControlName="params"
                             [editorStyle]="{minHeight: '130px'}"
                             label="{{ 'widgets.persistent-table.params' | translate }}">
        </tb-json-object-edit>
      </div>
      <mat-expansion-panel class="additional-json-editor">
        <mat-expansion-panel-header>
          <mat-panel-title translate>
            widgets.persistent-table.additional-info
          </mat-panel-title>
        </mat-expansion-panel-header>
        <ng-template matExpansionPanelContent>
          <tb-json-object-edit formControlName="additionalInfo"
                               [editorStyle]="{minHeight: '130px'}">
          </tb-json-object-edit>
        </ng-template>
      </mat-expansion-panel>
    </fieldset>
  </div>
  <div style="height: 4px;" *ngIf="!(isLoading$ | async)"></div>
  <div mat-dialog-actions fxLayout="row">
    <button mat-button color="primary"
            type="button"
            [disabled]="(isLoading$ | async)"
            (click)="close()">
      {{ 'action.close' | translate }}
    </button>
    <span fxFlex></span>
    <div fxLayout="row" fxLayoutGap="8px">
      <button mat-raised-button
              color="primary"
              type="submit"
              [disabled]="(isLoading$ | async) || persistentFormGroup.invalid || !persistentFormGroup.dirty">
        {{ 'widgets.persistent-table.send-request' | translate }}
      </button>
    </div>
  </div>
</form>
