<!--

    Copyright © 2016-2023 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.

-->
<mat-card appearance="outlined" class="settings-card">
  <mat-card-header>
    <mat-card-title>
      <div fxLayout="row">
        <span class="mat-headline-5" translate>admin.general-settings</span>
      </div>
    </mat-card-title>
  </mat-card-header>
  <mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async">
  </mat-progress-bar>
  <div style="height: 4px;" *ngIf="!(isLoading$ | async)"></div>
  <mat-card-content>
    <form [formGroup]="generalSettings" (ngSubmit)="save()" class="tb-form-panel no-border no-padding">
      <fieldset [disabled]="isLoading$ | async">
        <mat-form-field class="mat-block">
          <mat-label translate>admin.base-url</mat-label>
          <input matInput formControlName="baseUrl" required/>
          <mat-error *ngIf="generalSettings.get('baseUrl').hasError('required')">
            {{ 'admin.base-url-required' | translate }}
          </mat-error>
        </mat-form-field>
        <div class="tb-form-row" tb-hint-tooltip-icon="{{ 'admin.prohibit-different-url-hint' | translate }}">
          <mat-slide-toggle class="mat-slide margin" formControlName="prohibitDifferentUrl">
            {{ 'admin.prohibit-different-url' | translate }}
          </mat-slide-toggle>
        </div>
      </fieldset>
      <div fxLayout="row" fxLayoutAlign="end center" fxLayoutGap="8px" class="layout-wrap">
        <button mat-button color="primary"
                [disabled]="generalSettings.pristine"
                (click)="discardGeneralSettings()"
                type="button">{{'action.undo' | translate}}
        </button>
        <button mat-button mat-raised-button color="primary" [disabled]="(isLoading$ | async) || generalSettings.invalid || !generalSettings.dirty"
                type="submit">{{'action.save' | translate}}
        </button>
      </div>
    </form>
  </mat-card-content>
</mat-card>
<mat-card appearance="outlined" class="settings-card">
  <mat-card-header>
    <mat-card-title>
      <div class="mat-headline-5" translate>admin.device-connectivity.device-connectivity</div>
    </mat-card-title>
  </mat-card-header>
  <mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async">
  </mat-progress-bar>
  <div style="height: 4px;" *ngIf="!(isLoading$ | async)"></div>
  <mat-card-content>
    <section class="tb-form-panel no-border no-padding">
      <div class="tb-form-panel no-border no-padding toggle-group">
        <tb-toggle-select appearance="fill" [(ngModel)]="protocol">
          <tb-toggle-option value="http">{{ "admin.device-connectivity.http-s" | translate }}</tb-toggle-option>
          <tb-toggle-option value="mqtt">{{ 'admin.device-connectivity.mqtt-s' | translate }}</tb-toggle-option>
          <tb-toggle-option value="coap">{{ 'admin.device-connectivity.coap-s' | translate }}</tb-toggle-option>
        </tb-toggle-select>
        <div class="tb-form-hint tb-primary-fill">{{ 'admin.device-connectivity.hint' | translate }}</div>
      </div>
      <form [formGroup]="deviceConnectivitySettingsForm" (ngSubmit)="saveDeviceConnectivitySettings()"
            class="tb-form-panel no-border no-padding">
        <ng-container *ngIf="protocol === 'http'">
          <ng-container *ngTemplateOutlet="connectivitySettings; context:{protocol: protocol}"></ng-container>
          <ng-container *ngTemplateOutlet="connectivitySettings; context:{protocol: protocol + 's'}"></ng-container>
        </ng-container>
        <ng-container *ngIf="protocol === 'mqtt'">
          <ng-container *ngTemplateOutlet="connectivitySettings; context:{protocol: protocol}"></ng-container>
          <ng-container *ngTemplateOutlet="connectivitySettings; context:{protocol: protocol + 's'}"></ng-container>
        </ng-container>
        <ng-container *ngIf="protocol === 'coap'">
          <ng-container *ngTemplateOutlet="connectivitySettings; context:{protocol: protocol}"></ng-container>
          <ng-container *ngTemplateOutlet="connectivitySettings; context:{protocol: protocol + 's'}"></ng-container>
        </ng-container>
        <ng-template #connectivitySettings let-protocol="protocol">
          <div class="tb-form-panel stroked no-padding-bottom" [formGroupName]="protocol">
            <mat-slide-toggle class="mat-slide" formControlName="enabled">
              {{ 'admin.device-connectivity.' + protocol | translate }}
            </mat-slide-toggle>
            <div class="tb-form-row column-xs no-border no-padding tb-standard-fields">
              <mat-form-field fxFlex>
                <mat-label translate>admin.device-connectivity.host</mat-label>
                <input matInput formControlName="host"/>
              </mat-form-field>
              <mat-form-field fxFlex>
                <mat-label translate>admin.device-connectivity.port</mat-label>
                <input matInput type="number" min="0" max="65535" formControlName="port"/>
                <mat-error *ngIf="deviceConnectivitySettingsForm.get(protocol + '.port').hasError('pattern')">
                  {{ 'admin.device-connectivity.port-pattern' | translate }}
                </mat-error>
                <mat-error *ngIf="deviceConnectivitySettingsForm.get(protocol + '.port').hasError('min') ||
                       deviceConnectivitySettingsForm.get(protocol + '.port').hasError('max')">
                  {{ 'admin.device-connectivity.port-range' | translate }}
                </mat-error>
              </mat-form-field>
            </div>
          </div>
        </ng-template>
        <div fxLayout="row" fxLayoutAlign="end center" fxLayoutGap="8px" class="layout-wrap">
          <button mat-button color="primary"
                  [disabled]="deviceConnectivitySettingsForm.pristine"
                  (click)="discardDeviceConnectivitySettings()"
                  type="button">{{'action.undo' | translate}}
          </button>
          <button mat-raised-button color="primary"
                  [disabled]="(isLoading$ | async) || deviceConnectivitySettingsForm.invalid || !deviceConnectivitySettingsForm.dirty"
                  type="submit">{{'action.save' | translate}}
          </button>
        </div>
      </form>
    </section>
  </mat-card-content>
</mat-card>
