<!--

    Copyright © 2016-2026 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 class="flex-1"></span>
    <button mat-icon-button
            (click)="close()"
            type="button">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  @if (isLoading$ | async) {
    <mat-progress-bar color="warn" mode="indeterminate">
    </mat-progress-bar>
  } @else {
    <div style="height: 4px;"></div>
  }
  <div mat-dialog-content class="add-dialog">
    <fieldset [disabled]="isLoading$ | async">
      <div class="flex flex-row gap-1.5">
        <mat-slide-toggle class="mat-block flex-1" formControlName="oneWayElseTwoWay">
          {{ rpcMessageTypeText }}
        </mat-slide-toggle>
      </div>
      <div class="flex flex-row flex-wrap gap-1.5 xs:flex-col mt-1">
        <mat-form-field class="mat-block flex-1" appearance="outline">
          <mat-label translate>widgets.persistent-table.method</mat-label>
          <input matInput formControlName="method" required>
          @if (persistentFormGroup.get('method').hasError('required')) {
            <mat-error>
              {{'widgets.persistent-table.method-error' | translate}}
            </mat-error>
          }
          @if (persistentFormGroup.get('method').hasError('pattern')) {
            <mat-error>
              {{'widgets.persistent-table.white-space-error' | translate}}
            </mat-error>
          }
        </mat-form-field>
        <mat-form-field class="mat-block flex-1" appearance="outline">
          <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>
  @if (!(isLoading$ | async)) {
    <div style="height: 4px;"></div>
  }
  <div mat-dialog-actions class="flex flex-row">
    <button mat-button color="primary"
            type="button"
            [disabled]="(isLoading$ | async)"
            (click)="close()">
      {{ 'action.close' | translate }}
    </button>
    <span class="flex-1"></span>
    <div class="flex flex-row gap-2">
      <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>
