<!--

    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.

-->
<section class="tb-widget-settings" [formGroup]="updateMultipleAttributesKeySettingsForm">
  <mat-slide-toggle formControlName="dataKeyHidden" class="slide-block">
    {{ 'widgets.input-widgets.hide-input-field' | translate }}
  </mat-slide-toggle>
  <fieldset [class.!hidden]="updateMultipleAttributesKeySettingsForm.get('dataKeyHidden').value" class="fields-group">
    <legend class="group-title" translate>widgets.input-widgets.general-settings</legend>
    <div class="flex flex-col">
      <section class="flex flex-col gt-xs:flex-row gt-xs:items-center gt-xs:justify-start gt-xs:gap-2">
        <mat-form-field class="mat-block flex-full xs:max-h-50% gt-xs:max-w-50%" appearance="outline">
          <mat-label translate>widgets.input-widgets.datakey-type</mat-label>
          <mat-select formControlName="dataKeyType">
            <mat-option [value]="'server'">
              {{ 'widgets.input-widgets.datakey-type-server' | translate }}
            </mat-option>
            <mat-option [value]="'shared'">
              {{ 'widgets.input-widgets.datakey-type-shared' | translate }}
            </mat-option>
            <mat-option [value]="'timeseries'">
              {{ 'widgets.input-widgets.datakey-type-timeseries' | translate }}
            </mat-option>
          </mat-select>
        </mat-form-field>
        <mat-form-field class="mat-block flex-full xs:max-h-50% gt-xs:max-w-50%" appearance="outline">
          <mat-label translate>widgets.input-widgets.datakey-value-type</mat-label>
          <mat-select formControlName="dataKeyValueType">
            <mat-option [value]="'string'">
              {{ 'widgets.input-widgets.datakey-value-type-string' | translate }}
            </mat-option>
            <mat-option [value]="'double'">
              {{ 'widgets.input-widgets.datakey-value-type-double' | translate }}
            </mat-option>
            <mat-option [value]="'integer'">
              {{ 'widgets.input-widgets.datakey-value-type-integer' | translate }}
            </mat-option>
            <mat-option [value]="'booleanCheckbox'">
              {{ 'widgets.input-widgets.datakey-value-type-boolean-checkbox' | translate }}
            </mat-option>
            <mat-option [value]="'booleanSwitch'">
              {{ 'widgets.input-widgets.datakey-value-type-boolean-switch' | translate }}
            </mat-option>
            <mat-option [value]="'dateTime'">
              {{ 'widgets.input-widgets.datakey-value-type-date-time' | translate }}
            </mat-option>
            <mat-option [value]="'date'">
              {{ 'widgets.input-widgets.datakey-value-type-date' | translate }}
            </mat-option>
            <mat-option [value]="'time'">
              {{ 'widgets.input-widgets.datakey-value-type-time' | translate }}
            </mat-option>
            <mat-option [value]="'select'">
              {{ 'widgets.input-widgets.datakey-value-type-select' | translate }}
            </mat-option>
            <mat-option [value]="'radio'">
              {{ 'widgets.input-widgets.datakey-value-type-radio' | translate }}
            </mat-option>
            <mat-option [value]="'JSON'">
              {{ 'widgets.input-widgets.datakey-value-type-json' | translate }}
            </mat-option>
            <mat-option [value]="'color'">
              {{ 'widgets.input-widgets.datakey-value-type-color' | translate }}
            </mat-option>
          </mat-select>
        </mat-form-field>
      </section>
      <mat-checkbox formControlName="required" style="margin-bottom: 8px;">
        {{ 'widgets.input-widgets.value-is-required' | translate }}
      </mat-checkbox>
      <section class="flex flex-col gt-xs:flex-row gt-xs:items-center gt-xs:justify-start gt-xs:gap-2">
        <mat-form-field class="mat-block flex-full xs:max-h-50% gt-xs:max-w-50%" appearance="outline">
          <mat-label translate>widgets.input-widgets.ability-to-edit-attribute</mat-label>
          <mat-select formControlName="isEditable">
            <mat-option [value]="'editable'">
              {{ 'widgets.input-widgets.ability-to-edit-attribute-editable' | translate }}
            </mat-option>
            <mat-option [value]="'disabled'">
              {{ 'widgets.input-widgets.ability-to-edit-attribute-disabled' | translate }}
            </mat-option>
            <mat-option [value]="'readonly'">
              {{ 'widgets.input-widgets.ability-to-edit-attribute-readonly' | translate }}
            </mat-option>
          </mat-select>
        </mat-form-field>
        <mat-form-field [class.!hidden]="updateMultipleAttributesKeySettingsForm.get('isEditable').value !== 'editable'" class="mat-block flex-full xs:max-h-50% gt-xs:max-w-50%" appearance="outline">
          <mat-label translate>widgets.input-widgets.disable-on-datakey-name</mat-label>
          <input matInput formControlName="disabledOnDataKey">
        </mat-form-field>
      </section>
      @if (!['booleanSwitch', 'booleanCheckbox', 'radio'].includes(updateMultipleAttributesKeySettingsForm.get('dataKeyValueType').value)) {
        <section
          class="flex flex-col gt-xs:flex-row gt-xs:items-center gt-xs:justify-start gt-xs:gap-2">
          <mat-form-field class="mat-block flex-full xs:max-h-50% gt-xs:max-w-50%" appearance="outline">
            <mat-label translate>widgets.input-widgets.field-appearance</mat-label>
            <mat-select formControlName="appearance">
              <mat-option [value]="'fill'">
                {{ 'widgets.input-widgets.appearance-fill' | translate }}
              </mat-option>
              <mat-option [value]="'outline'">
                {{ 'widgets.input-widgets.appearance-outline' | translate }}
              </mat-option>
            </mat-select>
          </mat-form-field>
          <mat-form-field class="mat-block flex-full xs:max-h-50% gt-xs:max-w-50%" appearance="outline">
            <mat-label translate>widgets.input-widgets.subscript-sizing</mat-label>
            <mat-select formControlName="subscriptSizing">
              <mat-option [value]="'fixed'">
                {{ 'widgets.input-widgets.subscript-sizing-fixed' | translate }}
              </mat-option>
              <mat-option [value]="'dynamic'">
                {{ 'widgets.input-widgets.subscript-sizing-dynamic' | translate }}
              </mat-option>
            </mat-select>
          </mat-form-field>
        </section>
      }
    </div>
  </fieldset>
  <fieldset [class.!hidden]="updateMultipleAttributesKeySettingsForm.get('dataKeyHidden').value ||
                             updateMultipleAttributesKeySettingsForm.get('dataKeyValueType').value !== 'booleanSwitch'" class="fields-group">
    <legend class="group-title" translate>widgets.input-widgets.slide-toggle-settings</legend>
    <mat-form-field class="mat-block flex-1" appearance="outline">
      <mat-label translate>widgets.input-widgets.slide-toggle-label-position</mat-label>
      <mat-select formControlName="slideToggleLabelPosition">
        <mat-option [value]="'after'">
          {{ 'widgets.input-widgets.slide-toggle-label-position-after' | translate }}
        </mat-option>
        <mat-option [value]="'before'">
          {{ 'widgets.input-widgets.slide-toggle-label-position-before' | translate }}
        </mat-option>
      </mat-select>
    </mat-form-field>
  </fieldset>
  <fieldset [class.!hidden]="updateMultipleAttributesKeySettingsForm.get('dataKeyHidden').value ||
                             (updateMultipleAttributesKeySettingsForm.get('dataKeyValueType').value !== 'select' && updateMultipleAttributesKeySettingsForm.get('dataKeyValueType').value !== 'radio')" class="fields-group">
    <legend class="group-title">
      {{ (updateMultipleAttributesKeySettingsForm.get('dataKeyValueType').value === 'select' ? 'widgets.input-widgets.select-options' : 'widgets.input-widgets.radio-options') | translate }}
    </legend>
    <div class="flex flex-col">
      <div class="tb-control-list tb-drop-list" cdkDropList cdkDropListOrientation="vertical"
           (cdkDropListDropped)="selectOptionDrop($event)">
        @for (selectOptionControl of typedSelectOptions; track trackBySelectOption($index, selectOptionControl); let isLast = $last) {
          <div cdkDrag class="tb-draggable flex flex-col" [class.pb-2]="!isLast">
            <tb-datakey-select-option [formControl]="selectOptionControl"
                                      [expanded]="selectOptionControl.new"
                                      (removeSelectOption)="removeSelectOption($index)">
            </tb-datakey-select-option>
          </div>
        }
      </div>
      @if (!selectOptionsFormArray().controls.length) {
        <div>
          <span class="tb-prompt flex items-center justify-center">
            {{ (updateMultipleAttributesKeySettingsForm.get('dataKeyValueType').value === 'select' ? 'widgets.input-widgets.no-select-options' : 'widgets.input-widgets.no-radio-options') | translate }}
          </span>
        </div>
      }
      <div style="padding-top: 16px;">
        <button mat-raised-button color="primary"
                type="button"
                (click)="addSelectOption()">
          <span>
            {{ (updateMultipleAttributesKeySettingsForm.get('dataKeyValueType').value === 'select' ? 'widgets.input-widgets.add-select-option' : 'widgets.input-widgets.add-radio-option') | translate }}
          </span>
        </button>
      </div>
    </div>
  </fieldset>
  <div class="tb-form-panel mb-3.5"
       [class.!hidden]="updateMultipleAttributesKeySettingsForm.get('dataKeyHidden').value ||
       updateMultipleAttributesKeySettingsForm.get('dataKeyValueType').value !== 'radio'">
    <div class="tb-form-panel-title" translate>widgets.input-widgets.radio-button-settings</div>
    <div class="tb-form-row space-between">
      <div>{{ 'widgets.input-widgets.color' | translate }}</div>
      <tb-color-input asBoxInput
                      appearance="outline"
                      formControlName="radioColor">
      </tb-color-input>
    </div>
    <div class="tb-form-row space-between">
      <div>{{ 'widgets.input-widgets.columns' | translate }}</div>
      <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
        <input matInput formControlName="radioColumns" type="number" min="0" step="1" placeholder="{{ 'widget-config.set' | translate }}">
      </mat-form-field>
    </div>
    <div class="tb-form-row space-between">
      <div>{{ 'widgets.input-widgets.radio-label-position' | translate }}</div>
      <mat-form-field class="medium-width" appearance="outline" subscriptSizing="dynamic">
        <mat-select formControlName="radioLabelPosition">
          <mat-option [value]="'after'">
            {{ 'widgets.input-widgets.radio-label-position-after' | translate }}
          </mat-option>
          <mat-option [value]="'before'">
            {{ 'widgets.input-widgets.radio-label-position-before' | translate }}
          </mat-option>
        </mat-select>
      </mat-form-field>
    </div>
  </div>
  <fieldset [class.!hidden]="updateMultipleAttributesKeySettingsForm.get('dataKeyHidden').value ||
                             (updateMultipleAttributesKeySettingsForm.get('dataKeyValueType').value !== 'integer' &&
                             updateMultipleAttributesKeySettingsForm.get('dataKeyValueType').value !== 'double')" class="fields-group">
    <legend class="group-title" translate>widgets.input-widgets.numeric-field-settings</legend>
    <section class="flex flex-col gt-xs:flex-row gt-xs:gap-2">
      <mat-form-field class="mat-block flex-1" appearance="outline">
        <mat-label translate>widgets.input-widgets.step-interval</mat-label>
        <input matInput type="number" min="0" formControlName="step">
      </mat-form-field>
      <mat-form-field class="mat-block flex-1" appearance="outline">
        <mat-label translate>widgets.input-widgets.min-value</mat-label>
        <input matInput type="number" formControlName="minValue">
      </mat-form-field>
      <mat-form-field class="mat-block flex-1" appearance="outline">
        <mat-label translate>widgets.input-widgets.max-value</mat-label>
        <input matInput type="number" formControlName="maxValue">
      </mat-form-field>
    </section>
  </fieldset>
  <fieldset [class.!hidden]="!displayErrorMessagesSection()" class="fields-group">
    <legend class="group-title" translate>widgets.input-widgets.error-messages</legend>
    <mat-form-field [class.!hidden]="!updateMultipleAttributesKeySettingsForm.get('required').value" class="mat-block flex-1" appearance="outline">
      <mat-label translate>widgets.input-widgets.required-error-message</mat-label>
      <input matInput formControlName="requiredErrorMessage">
    </mat-form-field>
    <section [class.!hidden]="updateMultipleAttributesKeySettingsForm.get('dataKeyValueType').value !== 'integer' &&
                              updateMultipleAttributesKeySettingsForm.get('dataKeyValueType').value !== 'double'"
      class="flex flex-row gap-2">
      <mat-form-field class="mat-block flex-1" appearance="outline">
        <mat-label translate>widgets.input-widgets.min-value-error-message</mat-label>
        <input matInput formControlName="minValueErrorMessage">
      </mat-form-field>
      <mat-form-field class="mat-block flex-1" appearance="outline">
        <mat-label translate>widgets.input-widgets.max-value-error-message</mat-label>
        <input matInput formControlName="maxValueErrorMessage">
      </mat-form-field>
    </section>
    <mat-form-field [class.!hidden]="updateMultipleAttributesKeySettingsForm.get('dataKeyValueType').value !== 'dateTime' &&
                                     updateMultipleAttributesKeySettingsForm.get('dataKeyValueType').value !== 'date' &&
                                     updateMultipleAttributesKeySettingsForm.get('dataKeyValueType').value !== 'time'" class="mat-block flex-1"
                    appearance="outline">
      <mat-label translate>widgets.input-widgets.invalid-date-error-message</mat-label>
      <input matInput formControlName="invalidDateErrorMessage">
    </mat-form-field>
    <mat-form-field [class.!hidden]="updateMultipleAttributesKeySettingsForm.get('dataKeyValueType').value !== 'JSON'" class="mat-block flex-1" appearance="outline">
      <mat-label translate>widgets.input-widgets.invalid-JSON-error-message</mat-label>
      <input matInput formControlName="invalidJsonErrorMessage">
    </mat-form-field>
  </fieldset>
  <fieldset [class.!hidden]="updateMultipleAttributesKeySettingsForm.get('dataKeyHidden').value ||
                             updateMultipleAttributesKeySettingsForm.get('dataKeyValueType').value !== 'JSON'"class="fields-group">
    <legend class="group-title" translate>widgets.input-widgets.dialog-editor-settings</legend>
    <mat-form-field class="mat-block flex-1" appearance="outline">
      <mat-label translate>widgets.input-widgets.title</mat-label>
      <input matInput formControlName="dialogTitle">
    </mat-form-field>
    <section class="flex flex-row xs:flex-col gt-xs:items-center gt-xs:justify-start gt-xs:gap-2">
      <mat-form-field class="mat-block flex-1" appearance="outline">
        <mat-label translate>widgets.input-widgets.save-button-label</mat-label>
        <input matInput formControlName="saveButtonLabel">
      </mat-form-field>
      <mat-form-field class="mat-block flex-1" appearance="outline">
        <mat-label translate>widgets.input-widgets.cancel-button-label</mat-label>
        <input matInput formControlName="cancelButtonLabel">
      </mat-form-field>
    </section>
  </fieldset>
  <fieldset [class.!hidden]="updateMultipleAttributesKeySettingsForm.get('dataKeyHidden').value ||
                             ['date', 'dateTime', 'time'].includes(updateMultipleAttributesKeySettingsForm.get('dataKeyValueType').value)"
    class="fields-group">
    <legend class="group-title" translate>widgets.input-widgets.icon-settings</legend>
    <mat-slide-toggle formControlName="useCustomIcon" class="slide-block">
      {{ 'widgets.input-widgets.use-custom-icon' | translate }}
    </mat-slide-toggle>
    <tb-material-icon-select [class.!hidden]="updateMultipleAttributesKeySettingsForm.get('useCustomIcon').value"
                             label="{{ 'widgets.input-widgets.input-cell-icon' | translate }}"
                             iconClearButton
                             formControlName="icon"
                             appearance="outline">
    </tb-material-icon-select>
    <tb-gallery-image-input [class.!hidden]="!updateMultipleAttributesKeySettingsForm.get('useCustomIcon').value"
                            label="{{ 'widgets.input-widgets.input-cell-icon' | translate }}"
                            formControlName="customIcon">
    </tb-gallery-image-input>
  </fieldset>
  <fieldset [class.!hidden]="updateMultipleAttributesKeySettingsForm.get('dataKeyHidden').value" class="fields-group">
    <legend class="group-title" translate>widgets.input-widgets.value-conversion-settings</legend>
    <fieldset class="fields-group fields-group-slider">
      <legend class="group-title" translate>widgets.input-widgets.get-value-settings</legend>
      <mat-expansion-panel class="tb-settings" [expanded]="updateMultipleAttributesKeySettingsForm.get('useGetValueFunction').value">
        <mat-expansion-panel-header class="flex flex-row flex-wrap">
          <mat-panel-title>
            <mat-slide-toggle formControlName="useGetValueFunction" (click)="$event.stopPropagation()"
                              class="flex items-stretch justify-center">
              {{ 'widgets.input-widgets.use-get-value-function' | translate }}
            </mat-slide-toggle>
          </mat-panel-title>
          <mat-panel-description class="flex items-center justify-end xs:!hidden" translate>
            widget-config.advanced-settings
          </mat-panel-description>
        </mat-expansion-panel-header>
        <ng-template matExpansionPanelContent>
          <tb-js-func formControlName="getValueFunctionBody"
                      [globalVariables]="functionScopeVariables"
                      [functionArgs]="['value', 'ctx']"
                      functionTitle="{{ 'widgets.input-widgets.get-value-function' | translate }}">
          </tb-js-func>
        </ng-template>
      </mat-expansion-panel>
    </fieldset>
    <fieldset class="fields-group fields-group-slider">
      <legend class="group-title" translate>widgets.input-widgets.set-value-settings</legend>
      <mat-expansion-panel class="tb-settings" [expanded]="updateMultipleAttributesKeySettingsForm.get('useSetValueFunction').value">
        <mat-expansion-panel-header class="flex flex-row flex-wrap">
          <mat-panel-title>
            <mat-slide-toggle formControlName="useSetValueFunction" (click)="$event.stopPropagation()"
                              class="flex items-stretch justify-center">
              {{ 'widgets.input-widgets.use-set-value-function' | translate }}
            </mat-slide-toggle>
          </mat-panel-title>
          <mat-panel-description class="flex items-center justify-end xs:!hidden" translate>
            widget-config.advanced-settings
          </mat-panel-description>
        </mat-expansion-panel-header>
        <ng-template matExpansionPanelContent>
          <tb-js-func formControlName="setValueFunctionBody"
                      [globalVariables]="functionScopeVariables"
                      [functionArgs]="['value', 'originValue', 'ctx']"
                      functionTitle="{{ 'widgets.input-widgets.set-value-function' | translate }}">
          </tb-js-func>
        </ng-template>
      </mat-expansion-panel>
    </fieldset>
  </fieldset>
</section>
