<!--

    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-table-widget tb-absolute-fill">
  <div class="tb-absolute-fill flex flex-1 flex-col">
    <mat-toolbar class="mat-mdc-table-toolbar" [class.!hidden]="!textSearchMode">
      <div class="mat-toolbar-tools">
        <button mat-icon-button
                matTooltip="{{ 'action.search' | translate }}"
                matTooltipPosition="above">
          <mat-icon>search</mat-icon>
        </button>
        <mat-form-field class="flex-1">
          <mat-label>&nbsp;</mat-label>
          <input #searchInput matInput
                 [formControl]="textSearch"
                 placeholder="{{ 'widget.search-data' | translate }}"/>
        </mat-form-field>
        <button mat-icon-button (click)="exitFilterMode()"
                matTooltip="{{ 'action.close' | translate }}"
                matTooltipPosition="above">
          <mat-icon>close</mat-icon>
        </button>
      </div>
    </mat-toolbar>
    <mat-tab-group [class.tb-headless]="sources.length === 1"
                   class="flex-1"
                   mat-stretch-tabs="false"
                   [(selectedIndex)]="sourceIndex" (selectedIndexChange)="onSourceIndexChanged()">
      @for (source of sources; track trackBySourcesIndex(index, source); let index = $index) {
        <mat-tab [label]="source.displayName">
          @if (isActiveTab(index)) {
            <div class="table-container flex-1">
              <table mat-table [dataSource]="source.timeseriesDatasource" [trackBy]="trackByRowTimestamp"
                     matSort [matSortActive]="source.pageLink.sortOrder.property" [matSortDirection]="source.pageLink.sortDirection()" matSortDisableClear>
                @if (showTimestamp) {
                  <ng-container [matColumnDef]="'0'">
                    <mat-header-cell *matHeaderCellDef mat-sort-header>{{ 'widgets.table.timestamp-column-name' | translate }}</mat-header-cell>
                    <mat-cell *matCellDef="let row; let rowIndex = index"
                              [innerHTML]="cellContent(source, null, 0, row, row[0], rowIndex) | async"
                              [style]="cellStyle(source, null, 0, row, row[0], rowIndex) | async">
                    </mat-cell>
                  </ng-container>
                }
                @for (h of source.header; track trackByColumnIndex($index, h)) {
                  <ng-container [matColumnDef]="h.index + ''">
                    <mat-header-cell *matHeaderCellDef mat-sort-header [disabled]="!h.sortable"> {{ h.dataKey.label }} </mat-header-cell>
                    <mat-cell *matCellDef="let row; let rowIndex = index"
                              [innerHTML]="cellContent(source, h, h.index, row, row[h.index], rowIndex) | async"
                              [style]="cellStyle(source, h, h.index, row, row[h.index], rowIndex) | async">
                    </mat-cell>
                  </ng-container>
                }
                <ng-container matColumnDef="actions" [stickyEnd]="enableStickyAction">
                  <mat-header-cell *matHeaderCellDef>
                    @if (source.timeseriesDatasource.countCellButtonAction) {
                      <div class="gt-md:!hidden" style="min-width: 40px;">
                      </div>
                      <div class="lt-lg:!hidden"
                           [style.min-width]="(source.timeseriesDatasource.countCellButtonAction * 40) + 'px'">
                      </div>
                    }
                  </mat-header-cell>
                  <mat-cell *matCellDef="let row; let rowIndex = index" [style]="rowStyle(source, row, rowIndex) | async">
                    @if (source.timeseriesDatasource.countCellButtonAction) {
                      <div [class.lt-lg:!hidden]="showCellActionsMenu && source.timeseriesDatasource.countCellButtonAction !== 1" class="flex flex-row items-stretch justify-end"
                           [style.min-width]="(source.timeseriesDatasource.countCellButtonAction * 40) + 'px'">
                        @for (actionDescriptor of row.actionCellButtons; track trackByActionCellDescriptionId($index, actionDescriptor)) {
                          @if (!actionDescriptor.icon) {
                            <span style="width: 40px;"></span>
                          }
                          @if (actionDescriptor.icon) {
                            <button
                              class="tb-mat-40"
                              mat-icon-button
                              matTooltip="{{ actionDescriptor.displayName }}"
                              matTooltipPosition="above"
                              (click)="onActionButtonClick($event, row, actionDescriptor)">
                              <tb-icon>{{actionDescriptor.icon}}</tb-icon>
                            </button>
                          }
                        }
                      </div>
                      @if (row.hasActions) {
                        <div [class.!hidden]="!showCellActionsMenu || source.timeseriesDatasource.countCellButtonAction === 1" class="gt-md:!hidden">
                          <button mat-icon-button
                                  class="tb-mat-40"
                                  (click)="$event.stopPropagation(); ctx.detectChanges();"
                                  [matMenuTriggerFor]="cellActionsMenu">
                            <mat-icon class="material-icons">more_vert</mat-icon>
                          </button>
                          <mat-menu #cellActionsMenu="matMenu" xPosition="before">
                            @for (actionDescriptor of row.actionCellButtons; track trackByActionCellDescriptionId($index, actionDescriptor)) {
                              @if (actionDescriptor.icon) {
                                <button mat-menu-item
                                        (click)="onActionButtonClick($event, row, actionDescriptor)">
                                  <tb-icon matMenuItemIcon>{{actionDescriptor.icon}}</tb-icon>
                                  <span>{{ actionDescriptor.displayName }}</span>
                                </button>
                              }
                            }
                          </mat-menu>
                        </div>
                      }
                    }
                  </mat-cell>
                </ng-container>
                <mat-header-row *matHeaderRowDef="source.displayedColumns; sticky: enableStickyHeader"></mat-header-row>
                <mat-row [class.tb-pointer]="hasRowAction"
                         *matRowDef="let row; columns: source.displayedColumns; let rowIndex = index"
                         [style]="rowStyle(source, row, rowIndex) | async"
                (click)="onRowClick($event, row)"></mat-row>
              </table>
              <span [class.!hidden]="(source.timeseriesDatasource.isEmpty() | async) === false"
              class="no-data-found flex items-center justify-center">{{ noDataDisplayMessageText }}</span>
            </div>
            @if (displayPagination) {
              <mat-divider></mat-divider>
            }
            @if (displayPagination) {
              <mat-paginator
                [length]="source.timeseriesDatasource.total() | async"
                [pageIndex]="source.pageLink.page"
                [pageSize]="source.pageLink.pageSize"
                [pageSizeOptions]="pageSizeOptions"
                [hidePageSize]="hidePageSize"
              showFirstLastButtons></mat-paginator>
            }
          }
        </mat-tab>
      }
    </mat-tab-group>
  </div>
</div>
