<!--

    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 (ngSubmit)="save()" style="width: 750px;">
  <mat-toolbar color="primary">
    <h2 translate>{{settings ? 'dashboard.settings' : 'layout.settings'}}</h2>
    <span fxFlex></span>
    <button mat-button mat-icon-button
            (click)="cancel()"
            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 mat-dialog-content>
    <fieldset [disabled]="isLoading$ | async">
      <div *ngIf="settings" [formGroup]="settingsFormGroup" fxLayout="column">
        <mat-form-field class="mat-block">
          <mat-label translate>dashboard.state-controller</mat-label>
          <mat-select required formControlName="stateControllerId">
            <mat-option *ngFor="let stateControllerId of stateControllerIds" [value]="stateControllerId">
              {{stateControllerId}}
            </mat-option>
          </mat-select>
        </mat-form-field>
        <fieldset class="fields-group" fxLayout="column" fxLayout.gt-sm="row" fxLayoutAlign.gt-sm="start center" fxLayoutGap="8px">
          <legend class="group-title" translate>dashboard.title-settings</legend>
          <mat-slide-toggle fxFlex formControlName="showTitle">
            {{ 'dashboard.display-title' | translate }}
          </mat-slide-toggle>
          <tb-color-input fxFlex
                          label="{{'dashboard.title-color' | translate}}"
                          icon="format_color_fill"
                          openOnInput
                          formControlName="titleColor">
          </tb-color-input>
        </fieldset>
        <fieldset class="fields-group" fxLayout="column" fxLayoutGap="8px">
          <legend class="group-title" translate>dashboard.dashboard-logo-settings</legend>
          <mat-slide-toggle formControlName="showDashboardLogo">
            {{ 'dashboard.display-dashboard-logo' | translate }}
          </mat-slide-toggle>
          <tb-image-input fxFlex
                          label="{{'dashboard.dashboard-logo-image' | translate}}"
                          formControlName="dashboardLogoUrl">
          </tb-image-input>
        </fieldset>
        <fieldset class="fields-group" fxLayout="column" fxLayoutGap="8px">
          <legend class="group-title" translate>dashboard.toolbar-settings</legend>
          <mat-slide-toggle formControlName="hideToolbar">
            {{ 'dashboard.hide-toolbar' | translate }}
          </mat-slide-toggle>
          <mat-slide-toggle formControlName="toolbarAlwaysOpen">
            {{ 'dashboard.toolbar-always-open' | translate }}
          </mat-slide-toggle>
          <mat-slide-toggle formControlName="showDashboardsSelect">
            {{ 'dashboard.display-dashboards-selection' | translate }}
          </mat-slide-toggle>
          <mat-slide-toggle formControlName="showEntitiesSelect">
            {{ 'dashboard.display-entities-selection' | translate }}
          </mat-slide-toggle>
          <mat-slide-toggle formControlName="showFilters">
            {{ 'dashboard.display-filters' | translate }}
          </mat-slide-toggle>
          <mat-slide-toggle formControlName="showDashboardTimewindow">
            {{ 'dashboard.display-dashboard-timewindow' | translate }}
          </mat-slide-toggle>
          <mat-slide-toggle formControlName="showDashboardExport">
            {{ 'dashboard.display-dashboard-export' | translate }}
          </mat-slide-toggle>
          <mat-slide-toggle formControlName="showUpdateDashboardImage">
            {{ 'dashboard.display-update-dashboard-image' | translate }}
          </mat-slide-toggle>
        </fieldset>
        <mat-expansion-panel class="tb-settings">
          <mat-expansion-panel-header>
            <mat-panel-description fxLayoutAlign="end" translate>
              dashboard.advanced-settings
            </mat-panel-description>
          </mat-expansion-panel-header>
          <ng-template matExpansionPanelContent>
            <tb-css
              label="{{ 'dashboard.dashboard-css' | translate }}"
              formControlName="dashboardCss"
            ></tb-css>
          </ng-template>
        </mat-expansion-panel>
      </div>
      <div *ngIf="gridSettings" [formGroup]="gridSettingsFormGroup" fxLayout="column">
        <fieldset class="fields-group" fxLayout="column" fxLayoutGap="8px">
          <legend class="group-title" translate>dashboard.layout-settings</legend>
          <mat-form-field class="mat-block">
            <mat-label translate>dashboard.columns-count</mat-label>
            <input matInput formControlName="columns" type="number" step="any" min="10"
                   max="1000" required>
            <mat-error *ngIf="gridSettingsFormGroup.get('columns').hasError('required')">
              {{ 'dashboard.columns-count-required' | translate }}
            </mat-error>
            <mat-error *ngIf="gridSettingsFormGroup.get('columns').hasError('min')">
              {{ 'dashboard.min-columns-count-message' | translate }}
            </mat-error>
            <mat-error *ngIf="gridSettingsFormGroup.get('columns').hasError('max')">
              {{ 'dashboard.max-columns-count-message' | translate }}
            </mat-error>
          </mat-form-field>
          <mat-form-field fxFlex class="mat-block">
            <mat-label translate>dashboard.widgets-margins</mat-label>
            <input matInput formControlName="margin" type="number" step="any" min="0"
                   max="50" required>
            <mat-error *ngIf="gridSettingsFormGroup.get('margin').hasError('required')">
              {{ 'dashboard.margin-required' | translate }}
            </mat-error>
            <mat-error *ngIf="gridSettingsFormGroup.get('margin').hasError('min')">
              {{ 'dashboard.min-margin-message' | translate }}
            </mat-error>
            <mat-error *ngIf="gridSettingsFormGroup.get('margin').hasError('max')">
              {{ 'dashboard.max-margin-message' | translate }}
            </mat-error>
          </mat-form-field>
          <mat-slide-toggle fxFlex formControlName="autoFillHeight" style="display: block; padding-bottom: 12px;">
            {{ 'dashboard.autofill-height' | translate }}
          </mat-slide-toggle>
          <tb-color-input fxFlex
                          label="{{'dashboard.background-color' | translate}}"
                          icon="format_color_fill"
                          openOnInput
                          formControlName="backgroundColor">
          </tb-color-input>
          <tb-image-input fxFlex
                          label="{{'dashboard.background-image' | translate}}"
                          formControlName="backgroundImageUrl">
          </tb-image-input>
          <mat-form-field class="mat-block">
            <mat-label translate>dashboard.background-size-mode</mat-label>
            <mat-select formControlName="backgroundSizeMode">
              <mat-option value="100%">Fit width</mat-option>
              <mat-option value="auto 100%">Fit height</mat-option>
              <mat-option value="cover">Cover</mat-option>
              <mat-option value="contain">Contain</mat-option>
              <mat-option value="auto">Original size</mat-option>
            </mat-select>
          </mat-form-field>
        </fieldset>
        <fieldset class="fields-group" fxLayout="column" fxLayout.gt-sm="row" fxLayoutAlign.gt-sm="start center" fxLayoutGap="8px">
          <legend class="group-title" translate>dashboard.mobile-layout</legend>
          <mat-slide-toggle fxFlex formControlName="mobileAutoFillHeight">
            {{ 'dashboard.autofill-height' | translate }}
          </mat-slide-toggle>
          <mat-form-field fxFlex class="mat-block">
            <mat-label translate>dashboard.mobile-row-height</mat-label>
            <input matInput formControlName="mobileRowHeight" type="number" step="any" min="5"
                   max="200" required>
            <mat-error *ngIf="gridSettingsFormGroup.get('mobileRowHeight').hasError('required')">
              {{ 'dashboard.mobile-row-height-required' | translate }}
            </mat-error>
            <mat-error *ngIf="gridSettingsFormGroup.get('mobileRowHeight').hasError('min')">
              {{ 'dashboard.min-mobile-row-height-message' | translate }}
            </mat-error>
            <mat-error *ngIf="gridSettingsFormGroup.get('mobileRowHeight').hasError('max')">
              {{ 'dashboard.max-mobile-row-height-message' | translate }}
            </mat-error>
          </mat-form-field>
        </fieldset>
      </div>
    </fieldset>
  </div>
  <div mat-dialog-actions fxLayoutAlign="end center">
    <button mat-button color="primary"
            type="button"
            [disabled]="(isLoading$ | async)"
            (click)="cancel()" cdkFocusInitial>
      {{ 'action.cancel' | translate }}
    </button>
    <button mat-raised-button color="primary"
            type="submit"
            [disabled]="(isLoading$ | async) || settingsFormGroup.invalid || gridSettingsFormGroup.invalid
                         || (!settingsFormGroup.dirty && !gridSettingsFormGroup.dirty)">
      {{ 'action.save' | translate }}
    </button>
  </div>
</form>
