<!--

    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.

-->
<div class="tb-dynamic-form-property-settings-panel" [formGroup]="propertyFormGroup">
  <div class="tb-dynamic-form-property-settings-title">{{ panelTitle | translate }}</div>
  <div class="tb-dynamic-form-property-settings-panel-content">
    <div class="tb-form-row">
      <div class="fixed-title-width" translate>dynamic-form.property.id</div>
      <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic">
        <input required matInput formControlName="id" placeholder="{{ 'widget-config.set' | translate }}">
      </mat-form-field>
    </div>
    <div class="tb-form-row">
      <div class="fixed-title-width" translate>dynamic-form.property.name</div>
      <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic">
        <input required matInput formControlName="name" placeholder="{{ 'widget-config.set' | translate }}">
      </mat-form-field>
    </div>
    <div class="tb-form-row">
      <div class="fixed-title-width" translate>scada.behavior.hint</div>
      <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic">
        <input matInput formControlName="hint" placeholder="{{ 'widget-config.set' | translate }}">
      </mat-form-field>
    </div>
    <div class="tb-form-row">
      <div class="fixed-title-width" translate>dynamic-form.property.group-title</div>
      <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic">
        <input matInput formControlName="group" placeholder="{{ 'widget-config.set' | translate }}">
      </mat-form-field>
    </div>
    <div class="tb-form-row">
      <div class="fixed-title-width" translate>dynamic-form.property.type</div>
      <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic">
        <mat-select formControlName="type">
          @for (type of formPropertyTypes; track type) {
            <mat-option [value]="type">
              {{ formPropertyTypeTranslations.get(type) | translate }}
            </mat-option>
          }
        </mat-select>
      </mat-form-field>
    </div>
    @if (isArray) {
      <div class="tb-form-panel stroked">
        <div class="tb-form-panel-title" translate>dynamic-form.property.array-item</div>
        <div class="tb-form-row">
          <div class="fixed-title-width" translate>dynamic-form.property.item-type</div>
          <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic">
            <mat-select formControlName="arrayItemType">
              @for (type of arrayItemFormPropertyTypes; track type) {
                <mat-option [value]="type">
                  {{ formPropertyTypeTranslations.get(type) | translate }}
                </mat-option>
              }
            </mat-select>
          </mat-form-field>
        </div>
        <div class="tb-form-row">
          <div class="fixed-title-width" translate>dynamic-form.property.item-name</div>
          <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic">
            <input matInput formControlName="arrayItemName" placeholder="{{ 'widget-config.set' | translate }}">
          </mat-form-field>
        </div>
        <ng-container *ngTemplateOutlet="itemSettings">
        </ng-container>
      </div>
    }
    @if (!isArray) {
      <ng-container *ngTemplateOutlet="itemSettings">
      </ng-container>
    }
    @if (![FormPropertyType.fieldset, FormPropertyType.htmlSection, FormPropertyType.array].includes(propertyFormGroup.get('type').value)) {
      <div class="tb-form-row space-between">
        <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="required">
          {{ 'dynamic-form.property.value-required' | translate }}
        </mat-slide-toggle>
      </div>
    }
    <div class="tb-form-panel stroked">
      <mat-expansion-panel class="tb-settings">
        <mat-expansion-panel-header class="flex flex-row flex-wrap">
          <mat-panel-title>
            {{ 'dynamic-form.property.advanced-ui-settings' | translate }}
          </mat-panel-title>
        </mat-expansion-panel-header>
        <ng-template matExpansionPanelContent>
          @if (isPropertyTypeAllowedForRow(propertyFormGroup.get('type').value)) {
            <div class="tb-form-row space-between">
              <div class="fixed-title-width" translate>dynamic-form.property.sub-label</div>
              <mat-form-field  class="flex"
                               appearance="outline" subscriptSizing="dynamic">
                <input matInput formControlName="subLabel" placeholder="{{ 'widget-config.set' | translate }}">
              </mat-form-field>
            </div>
            <div class="tb-form-row space-between">
              <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="divider">
                {{ 'dynamic-form.property.vertical-divider-after' | translate }}
              </mat-slide-toggle>
            </div>
          }
          @if ([FormPropertyType.number, FormPropertyType.text, FormPropertyType.password].includes(propertyItemType)) {
            <div
              class="tb-form-row space-between">
              <div class="fixed-title-width" translate>dynamic-form.property.input-field-suffix</div>
              <mat-form-field  class="flex"
                               appearance="outline" subscriptSizing="dynamic">
                <input matInput formControlName="fieldSuffix" placeholder="{{ 'widget-config.set' | translate }}">
              </mat-form-field>
            </div>
          }
          @if (propertyFormGroup.get('type').value !== FormPropertyType.htmlSection) {
            <div class="tb-form-row space-between">
              <div class="fixed-title-width" translate>dynamic-form.property.disable-on-property</div>
              <mat-form-field  class="flex" appearance="outline" subscriptSizing="dynamic">
                <mat-select formControlName="disableOnProperty" placeholder="{{ 'widget-config.set' | translate }}">
                  <mat-option [value]="null">{{ 'dynamic-form.property.disable-on-property-none' | translate }}</mat-option>
                  @for (prop of booleanPropertyIds; track prop) {
                    <mat-option [value]="prop">
                      {{ prop }}
                    </mat-option>
                  }
                </mat-select>
              </mat-form-field>
            </div>
          }
          <div class="tb-form-row">
            <tb-js-func
              class="flex-1"
              formControlName="condition"
              [functionArgs]="['property', 'model']"
              functionTitle="{{ 'dynamic-form.property.display-condition-function' | translate }}">
            </tb-js-func>
          </div>
          @if (isPropertyTypeAllowedForRow(propertyFormGroup.get('type').value)) {
            <tb-string-items-list
              class="flex-1"
              fieldClass="flex"
              floatLabel="always"
              label="{{ 'dynamic-form.property.property-row-classes' | translate }}"
              placeholder="{{ 'widget-config.set' | translate }}"
              [predefinedValues]="formPropertyRowClasses"
              formControlName="rowClass"
              appearance="outline"
              subscriptSizing="dynamic">
            </tb-string-items-list>
          }
          @if (isPropertyTypeAllowedForRow(propertyFormGroup.get('type').value) && FormPropertyType.switch !== propertyFormGroup.get('type').value) {
            <tb-string-items-list
              class="flex-1"
              fieldClass="flex"
              floatLabel="always"
              label="{{ 'dynamic-form.property.property-field-classes' | translate }}"
              placeholder="{{ 'widget-config.set' | translate }}"
              [predefinedValues]="formPropertyFieldClasses"
              formControlName="fieldClass"
              appearance="outline"
              subscriptSizing="dynamic">
            </tb-string-items-list>
          }
        </ng-template>
      </mat-expansion-panel>
    </div>
  </div>
  <div class="tb-dynamic-form-property-settings-panel-buttons">
    <button mat-button
            color="primary"
            type="button"
            (click)="cancel()">
      {{ (disabled ? 'action.close' : 'action.cancel') | translate }}
    </button>
    @if (!disabled) {
      <button
        mat-raised-button
        color="primary"
        type="button"
        (click)="applyPropertySettings()"
        [disabled]="propertyFormGroup.invalid || !propertyFormGroup.dirty">
        {{ (isAdd ? 'action.add' : 'action.apply') | translate }}
      </button>
    }
  </div>
</div>

<ng-template #itemSettings>
  <ng-container [formGroup]="propertyFormGroup">
    @if (propertyItemType === FormPropertyType.textarea) {
      <div class="tb-form-row space-between">
        <div translate>dynamic-form.property.textarea-rows</div>
        <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
          <input matInput formControlName="rows" min="1" step="1"
                 type="number" placeholder="{{ 'widget-config.set' | translate }}">
        </mat-form-field>
      </div>
    }
    @if (propertyItemType === FormPropertyType.number) {
      <div class="tb-form-row space-between">
        <div translate>dynamic-form.property.number-settings</div>
        <div class="flex flex-1 flex-row items-center justify-end gap-2">
          <div class="tb-small-label" translate>dynamic-form.property.min</div>
          <mat-form-field appearance="outline" class="number flex" subscriptSizing="dynamic">
            <input matInput formControlName="min"
                   type="number" placeholder="{{ 'widget-config.set' | translate }}">
          </mat-form-field>
          <mat-divider vertical></mat-divider>
          <div class="tb-small-label" translate>dynamic-form.property.max</div>
          <mat-form-field appearance="outline" class="number flex" subscriptSizing="dynamic">
            <input matInput formControlName="max"
                   type="number" placeholder="{{ 'widget-config.set' | translate }}">
          </mat-form-field>
          <mat-divider vertical></mat-divider>
          <div class="tb-small-label" translate>dynamic-form.property.step</div>
          <mat-form-field appearance="outline" class="number flex" subscriptSizing="dynamic">
            <input matInput formControlName="step" min="0"
                   type="number" placeholder="{{ 'widget-config.set' | translate }}">
          </mat-form-field>
        </div>
      </div>
    }
    @if (propertyItemType === FormPropertyType.fieldset) {
      <div class="tb-form-panel stroked">
        <mat-expansion-panel class="tb-settings" expanded>
          <mat-expansion-panel-header class="flex flex-row flex-wrap">
            <mat-panel-title>
              {{ 'dynamic-form.property.properties' | translate }}
            </mat-panel-title>
          </mat-expansion-panel-header>
          <ng-template matExpansionPanelContent>
            <tb-dynamic-form-properties formControlName="properties" noBorder noMargin class="!overflow-visible">
            </tb-dynamic-form-properties>
          </ng-template>
        </mat-expansion-panel>
      </div>
    }
    @if (propertyItemType === FormPropertyType.htmlSection) {
      <div class="tb-form-panel stroked">
        <mat-expansion-panel class="tb-settings" expanded>
          <mat-expansion-panel-header class="flex flex-row flex-wrap">
            <mat-panel-title>
              {{ 'dynamic-form.property.html-section-settings' | translate }}
            </mat-panel-title>
          </mat-expansion-panel-header>
          <ng-template matExpansionPanelContent>
            <div class="tb-form-row">
              <tb-html
                class="flex-1"
                formControlName="htmlContent"
                label="{{ 'dynamic-form.property.html-section-content' | translate }}">
              </tb-html>
            </div>
            <tb-string-items-list class="flex-1"
                                  fieldClass="flex"
                                  floatLabel="always"
                                  label="{{ 'dynamic-form.property.html-section-classes' | translate }}"
                                  formControlName="htmlClassList"
                                  appearance="outline"
                                  subscriptSizing="dynamic">
            </tb-string-items-list>
          </ng-template>
        </mat-expansion-panel>
      </div>
    }
    @if ([FormPropertyType.select, FormPropertyType.radios].includes(propertyItemType)) {
      <div class="tb-form-panel stroked">
        <mat-expansion-panel class="tb-settings" expanded>
          <mat-expansion-panel-header class="flex flex-row flex-wrap">
            <mat-panel-title>
              {{ (propertyFormGroup.get('type').value === FormPropertyType.select ? 'dynamic-form.property.select-options' : 'dynamic-form.property.radio-button-options') | translate }}
            </mat-panel-title>
          </mat-expansion-panel-header>
          <ng-template matExpansionPanelContent>
            @if (propertyItemType === FormPropertyType.radios) {
              <div class="tb-form-row space-between">
                <div translate>dynamic-form.property.buttons-direction</div>
                <mat-chip-listbox class="center-stretch" formControlName="direction">
                  <mat-chip-option [selectable]="propertyFormGroup.get('direction').value !== 'column'"
                  value="column">{{ 'dynamic-form.property.direction-column' | translate }}</mat-chip-option>
                  <mat-chip-option [selectable]="propertyFormGroup.get('direction').value !== 'row'"
                  value="row">{{ 'dynamic-form.property.direction-row' | translate }}</mat-chip-option>
                </mat-chip-listbox>
              </div>
            }
            @if (propertyItemType === FormPropertyType.select) {
              <div class="tb-form-row space-between">
                <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="multiple">
                  {{ 'dynamic-form.property.enable-multiple-select' | translate }}
                </mat-slide-toggle>
              </div>
            }
            @if (propertyItemType === FormPropertyType.select && !propertyFormGroup.get('multiple').value) {
              <div class="tb-form-row space-between">
                <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="allowEmptyOption">
                  {{ 'dynamic-form.property.allow-empty-select-option' | translate }}
                </mat-slide-toggle>
              </div>
            }
            @if (propertyItemType === FormPropertyType.select && propertyFormGroup.get('multiple').value) {
              <div class="tb-form-row space-between">
                <div translate>dynamic-form.property.selected-options-limit</div>
                <div class="flex flex-1 flex-row items-center justify-end gap-2">
                  <div class="tb-small-label" translate>dynamic-form.property.min</div>
                  <mat-form-field appearance="outline" class="number flex" subscriptSizing="dynamic">
                    <input matInput formControlName="minItems"
                           type="number" placeholder="{{ 'widget-config.set' | translate }}">
                  </mat-form-field>
                  <mat-divider vertical></mat-divider>
                  <div class="tb-small-label" translate>dynamic-form.property.max</div>
                  <mat-form-field appearance="outline" class="number flex" subscriptSizing="dynamic">
                    <input matInput formControlName="maxItems"
                           type="number" placeholder="{{ 'widget-config.set' | translate }}">
                  </mat-form-field>
                </div>
              </div>
            }
            <tb-dynamic-form-select-items
              formControlName="items">
            </tb-dynamic-form-select-items>
          </ng-template>
        </mat-expansion-panel>
      </div>
    }
    @if ([FormPropertyType.javascript, FormPropertyType.markdown].includes(propertyItemType)) {
      <div class="tb-form-row">
        <div class="fixed-title-width" translate>dynamic-form.property.help-id</div>
        <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic">
          <input matInput formControlName="helpId" placeholder="{{ 'widget-config.set' | translate }}">
        </mat-form-field>
      </div>
    }
    @if (propertyItemType === FormPropertyType.datetime) {
      <div class="tb-form-row space-between">
        <div translate>dynamic-form.property.datetime-type</div>
        <mat-chip-listbox class="center-stretch" formControlName="dateTimeType">
          <mat-chip-option [selectable]="propertyFormGroup.get('dateTimeType').value !== 'date'"
          value="date">{{ 'dynamic-form.property.datetime-type-date' | translate }}</mat-chip-option>
          <mat-chip-option [selectable]="propertyFormGroup.get('dateTimeType').value !== 'time'"
          value="time">{{ 'dynamic-form.property.datetime-type-time' | translate }}</mat-chip-option>
          <mat-chip-option [selectable]="propertyFormGroup.get('dateTimeType').value !== 'datetime'"
          value="datetime">{{ 'dynamic-form.property.datetime-type-datetime' | translate }}</mat-chip-option>
        </mat-chip-listbox>
      </div>
      <div class="tb-form-row space-between">
        <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="allowClear">
          {{ 'dynamic-form.property.enable-clear-button' | translate }}
        </mat-slide-toggle>
      </div>
    }
    @if (propertyItemType === FormPropertyType.textarea) {
      <div class="tb-form-row no-border no-padding">
        <mat-form-field  class="flex"
                         floatLabel="always"
                         appearance="outline" subscriptSizing="dynamic">
          <mat-label translate>dynamic-form.property.default-value</mat-label>
          <textarea matInput
                    [rows]="propertyFormGroup.get('rows').value || 2"
          formControlName="default" placeholder="{{ 'widget-config.set' | translate }}"></textarea>
        </mat-form-field>
      </div>
    }
    @if (propertyItemType === FormPropertyType.javascript) {
      <div class="tb-form-row" >
        <tb-js-func
          class="flex-1"
          disableUndefinedCheck
          formControlName="default"
          [helpId]="propertyFormGroup.get('helpId').value"
          label="{{ 'dynamic-form.property.default-value' | translate }}">
        </tb-js-func>
      </div>
    }
    @if (propertyItemType === FormPropertyType.json) {
      <div class="tb-form-row" >
        <tb-json-content
          class="flex-1"
          formControlName="default"
          [contentType]="ContentType.JSON"
          label="{{ 'dynamic-form.property.default-value' | translate }}">
        </tb-json-content>
      </div>
    }
    @if (propertyItemType === FormPropertyType.html) {
      <div class="tb-form-row" >
        <tb-html
          class="flex-1"
          formControlName="default"
          label="{{ 'dynamic-form.property.default-value' | translate }}">
        </tb-html>
      </div>
    }
    @if (propertyItemType === FormPropertyType.css) {
      <div class="tb-form-row" >
        <tb-css
          class="flex-1"
          formControlName="default"
          label="{{ 'dynamic-form.property.default-value' | translate }}">
        </tb-css>
      </div>
    }
    @if (propertyItemType === FormPropertyType.markdown) {
      <div class="tb-form-row" >
        <tb-markdown-editor
          class="flex-1"
          formControlName="default"
          [helpId]="propertyFormGroup.get('helpId').value"
          label="{{ 'dynamic-form.property.default-value' | translate }}">
        </tb-markdown-editor>
      </div>
    }
    @if (propertyItemType === FormPropertyType.image && !isArray) {
      <div class="tb-form-row" >
        <tb-gallery-image-input
          class="flex-1"
          formControlName="default"
          label="{{ 'dynamic-form.property.default-value' | translate }}">
        </tb-gallery-image-input>
      </div>
    }
    @if (propertyItemType === FormPropertyType.radios) {
      <div
        class="tb-form-row tb-radios-property" [class.direction-column]="propertyFormGroup.get('direction').value === 'column'">
        <div tbTruncateWithTooltip translate class="fixed-title-height">dynamic-form.property.default-value</div>
        <mat-radio-group class="flex flex-1 flex-wrap" [class.flex-col]="propertyFormGroup.get('direction').value === 'column'"
                         formControlName="default">
          @for (item of propertyFormGroup.get('items').value; track item) {
            <mat-radio-button [value]="item.value">
              <div tbTruncateWithTooltip>{{ item.label | customTranslate }}</div>
            </mat-radio-button>
          }
        </mat-radio-group>
      </div>
    }
    @if (propertyItemType === FormPropertyType.units) {
      <div class="tb-form-row space-between">
        <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="supportsUnitConversion">
          {{ 'dynamic-form.property.support-unit-conversion' | translate }}
        </mat-slide-toggle>
      </div>
    }
    @if (isPropertyTypeAllowedForRow(propertyItemType)) {
      <div class="tb-form-row space-between">
        <div class="fixed-title-width" translate>dynamic-form.property.default-value</div>
        @if ([FormPropertyType.text, FormPropertyType.password].includes(propertyItemType)) {
          <mat-form-field
            class="flex"
            appearance="outline" subscriptSizing="dynamic">
            <input matInput [type]="propertyFormGroup.get('type').value" formControlName="default" placeholder="{{ 'widget-config.set' | translate }}">
          </mat-form-field>
        }
        @if (propertyItemType === FormPropertyType.color) {
          <tb-color-input
            asBoxInput
            colorClearButton
            formControlName="default">
          </tb-color-input>
        }
        @if (propertyItemType === FormPropertyType.color_settings) {
          <tb-color-settings
            formControlName="default"
            settingsKey="{{ propertyFormGroup.get('id').value }}">
          </tb-color-settings>
        }
        @if (propertyItemType === FormPropertyType.number) {
          <mat-form-field
            appearance="outline"
            class="number"
            subscriptSizing="dynamic">
            <input matInput formControlName="default"
                   [min]="propertyFormGroup.get('min').value"
                   [max]="propertyFormGroup.get('max').value"
                   [step]="propertyFormGroup.get('step').value"
                   type="number" placeholder="{{ 'widget-config.set' | translate }}">
          </mat-form-field>
        }
        @if (propertyItemType === FormPropertyType.font) {
          <tb-font-settings
            formControlName="default"
            clearButton
            disabledLineHeight>
          </tb-font-settings>
        }
        @if (propertyItemType === FormPropertyType.units) {
          <tb-unit-input
            [supportsUnitConversion]="propertyFormGroup.get('supportsUnitConversion').value"
          formControlName="default"></tb-unit-input>
        }
        @if (propertyItemType === FormPropertyType.switch) {
          <tb-value-input
            [valueType]="ValueType.BOOLEAN"
          formControlName="default"></tb-value-input>
        }
        @if (propertyItemType === FormPropertyType.icon) {
          <tb-material-icon-select
            asBoxInput
            formControlName="default">
          </tb-material-icon-select>
        }
        @if (propertyItemType === FormPropertyType.select) {
          <mat-form-field class="medium-width"
                          appearance="outline" subscriptSizing="dynamic">
            @if (!propertyFormGroup.get('multiple').value) {
              <mat-select formControlName="default">
                @if (propertyFormGroup.get('allowEmptyOption').value) {
                  <mat-option [value]="null"></mat-option>
                }
                @for (item of propertyFormGroup.get('items').value; track item) {
                  <mat-option [value]="item.value">
                    {{ item.label | customTranslate }}
                  </mat-option>
                }
              </mat-select>
            }
            @if (propertyFormGroup.get('multiple').value) {
              <mat-select formControlName="default" multiple>
                @for (item of propertyFormGroup.get('items').value; track item) {
                  <mat-option [value]="item.value">
                    {{ item.label | customTranslate }}
                  </mat-option>
                }
              </mat-select>
            }
          </mat-form-field>
        }
        @if (propertyItemType === FormPropertyType.datetime) {
          @if (propertyFormGroup.get('dateTimeType').value === 'date') {
            <tb-datetime
              class="flex-1"
              showLabel="false"
              fieldClass="flex"
              [allowClear]="propertyFormGroup.get('allowClear').value"
              appearance="outline"
              subscriptSizing="dynamic"
              type="date"
              formControlName="default"
            ></tb-datetime>
          }
          @if (propertyFormGroup.get('dateTimeType').value === 'time') {
            <tb-datetime
              class="flex-1"
              showLabel="false"
              fieldClass="flex"
              [allowClear]="propertyFormGroup.get('allowClear').value"
              appearance="outline"
              subscriptSizing="dynamic"
              type="time"
              formControlName="default"
            ></tb-datetime>
          }
          @if (propertyFormGroup.get('dateTimeType').value === 'datetime') {
            <tb-datetime
              class="flex-1"
              showLabel="false"
              fieldClass="flex"
              [allowClear]="propertyFormGroup.get('allowClear').value"
              appearance="outline"
              subscriptSizing="dynamic"
              type="datetime"
              formControlName="default"
            ></tb-datetime>
          }
        }
      </div>
    }
  </ng-container>
</ng-template>
