<!--

    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.

-->
<ng-container [formGroup]="timeseriesTableWidgetSettingsForm">
  <div class="tb-form-panel">
    <div class="tb-form-panel-title" translate>widgets.table.table-header</div>
    <mat-slide-toggle class="mat-slide"  formControlName="enableStickyHeader">
      {{ 'widgets.table.enable-sticky-header' | translate }}
    </mat-slide-toggle>
    <mat-slide-toggle class="mat-slide"  formControlName="enableSearch">
      {{ 'widgets.table.enable-search' | translate }}
    </mat-slide-toggle>
    <mat-slide-toggle class="mat-slide"  formControlName="enableSelectColumnDisplay">
      {{ 'widgets.table.enable-select-column-display' | translate }}
    </mat-slide-toggle>
  </div>
  <div class="tb-form-panel">
    <div class="tb-form-panel-title" translate>widgets.table.columns</div>

    <div  class="tb-form-row column-xs">
      <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="showTimestamp">
        {{ 'widgets.table.display-timestamp' | translate }}
      </mat-slide-toggle>
      <tb-date-format-select class="flex-1" excludeLastUpdateAgo formControlName="dateFormat"></tb-date-format-select>
    </div>

    <mat-slide-toggle class="mat-slide"  formControlName="enableStickyAction">
      {{ 'widgets.table.enable-sticky-action' | translate }}
    </mat-slide-toggle>
    <mat-slide-toggle class="mat-slide"  formControlName="showCellActionsMenu">
      {{ 'widgets.table.show-cell-actions-menu-mobile' | translate }}
    </mat-slide-toggle>
    <mat-form-field class="flex-1" subscriptSizing="dynamic">
      <mat-label translate>widgets.table.hidden-cell-button-display-mode</mat-label>
      <mat-select formControlName="reserveSpaceForHiddenAction">
        <mat-option [value]="'true'">
          {{ 'widgets.table.show-empty-space-hidden-action' | translate }}
        </mat-option>
        <mat-option [value]="'false'">
          {{ 'widgets.table.dont-reserve-space-hidden-action' | translate }}
        </mat-option>
      </mat-select>
    </mat-form-field>
  </div>
  <div class="tb-form-panel">
    <div class="tb-form-panel-title" translate>widgets.table.pagination</div>
    <mat-slide-toggle class="mat-slide"  formControlName="displayPagination">
      {{ 'widgets.table.display-pagination' | translate }}
    </mat-slide-toggle>
    <div class="tb-form-row space-between column-xs">
      <div class="fixed-title-width !min-w-30">{{ 'widgets.table.page-step-settings' | translate }}</div>
      <div class="flex flex-row items-center justify-start gap-2">
        <div class="tb-small-label" translate>widgets.table.page-step-increment</div>
        <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
          <input matInput formControlName="pageStepIncrement" type="number" min="1" step="1" required
                 placeholder="{{ 'widget-config.set' | translate }}">
          @if (timeseriesTableWidgetSettingsForm.get('pageStepIncrement').invalid
            && timeseriesTableWidgetSettingsForm.get('pageStepIncrement').touched) {
            <mat-icon matSuffix
                      matTooltipPosition="above"
                      matTooltipClass="tb-error-tooltip"
                    [matTooltip]="(timeseriesTableWidgetSettingsForm.get('pageStepIncrement').hasError('required')
                                     ? 'widgets.value-source.value-required'
                                     : 'widgets.table.page-step-increment-format-message') | translate"
              class="tb-error">
              warning
            </mat-icon>
          }
        </mat-form-field>
        <mat-divider vertical class="xs:hidden"></mat-divider>
        <div class="tb-small-label" translate>widgets.table.page-step-count</div>
        <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
          <input matInput formControlName="pageStepCount" type="number" min="1" max="100" step="1" required
                 placeholder="{{ 'widget-config.set' | translate }}">
          @if (timeseriesTableWidgetSettingsForm.get('pageStepCount').invalid
            && timeseriesTableWidgetSettingsForm.get('pageStepCount').touched) {
            <mat-icon matSuffix
                      matTooltipPosition="above"
                      matTooltipClass="tb-error-tooltip"
                    [matTooltip]="(timeseriesTableWidgetSettingsForm.get('pageStepCount').hasError('required')
                                     ? 'widgets.value-source.value-required'
                                     : 'widgets.table.page-step-count-format-message') | translate"
              class="tb-error">
              warning
            </mat-icon>
          }
        </mat-form-field>
      </div>
    </div>
    <div class="tb-form-row space-between column-xs">
      <div translate>widgets.table.default-page-size</div>
      <mat-form-field appearance="outline" subscriptSizing="dynamic">
        <mat-select formControlName="defaultPageSize" placeholder="{{ 'widget-config.set' | translate }}">
          @for (size of pageStepSizeValues; track size) {
            <mat-option [value]="size">
              {{ size }}
            </mat-option>
          }
        </mat-select>
      </mat-form-field>
    </div>
  </div>
  <div class="tb-form-panel">
    <div class="tb-form-panel-title" translate>widgets.table.table-tabs</div>
    <mat-slide-toggle class="mat-slide" formControlName="useEntityLabel">
      {{ 'widgets.table.use-entity-label-tab-name' | translate }}
    </mat-slide-toggle>
    <div class="tb-form-row justify-between column-xs" formGroupName="sortOrder">
      <div class="fixed-title-width" translate>widgets.table.sort-by</div>
      <div class="flex flex-1 flex-row justify-end gap-2 ">
        <mat-form-field class="flex-lt-lg medium-width" appearance="outline" subscriptSizing="dynamic">
          <mat-select formControlName="property" placeholder="{{ 'widget-config.set' | translate }}">
            <mat-option [value]="entityFields.createdTime.value">{{ entityFields.createdTime.name | translate }}</mat-option>
            <mat-option  [value]="entityFields.name.value">{{ entityFields.name.name | translate }}</mat-option>
          </mat-select>
        </mat-form-field>
        <mat-form-field class="flex-lt-lg medium-width" appearance="outline" subscriptSizing="dynamic">
          <mat-select formControlName="direction" placeholder="{{ 'widget-config.set' | translate }}">
            <mat-option  [value]="Direction.ASC">{{ 'common.sort-asc' | translate }}</mat-option>
            <mat-option  [value]="Direction.DESC">{{ 'common.sort-desc' | translate }}</mat-option>
          </mat-select>
        </mat-form-field>
      </div>
    </div>
  </div>
  <div class="tb-form-panel tb-slide-toggle">
    <div class="tb-form-panel-title" style="padding-bottom: 16px;" translate>widgets.table.rows</div>
    <mat-slide-toggle class="mat-slide" style="padding-left: 16px;" formControlName="hideEmptyLines">
      {{ 'widgets.table.hide-empty-lines' | translate }}
    </mat-slide-toggle>
    <mat-expansion-panel class="tb-settings" [expanded]="timeseriesTableWidgetSettingsForm.get('useRowStyleFunction').value">
      <mat-expansion-panel-header class="flex flex-row flex-wrap">
        <mat-panel-title>
          <mat-slide-toggle class="mat-slide flex items-stretch justify-center" formControlName="useRowStyleFunction" (click)="$event.stopPropagation()">
            {{ 'widgets.table.use-row-style-function' | translate }}
          </mat-slide-toggle>
        </mat-panel-title>
        <mat-panel-description class="flex max-w-40% flex-full items-center justify-end xs:!hidden" translate>
          widget-config.advanced-settings
        </mat-panel-description>
      </mat-expansion-panel-header>
      <ng-template matExpansionPanelContent>
        <tb-js-func required
                    formControlName="rowStyleFunction"
                    withModules
                    [globalVariables]="functionScopeVariables"
                    [functionArgs]="['rowData', 'ctx']"
                    functionTitle="{{ 'widgets.table.row-style-function' | translate }}"
                    helpId="widget/lib/timeseries/row_style_fn">
        </tb-js-func>
      </ng-template>
    </mat-expansion-panel>
  </div>
</ng-container>
