<!--

    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 || !alarmsDatasource.selection.isEmpty()">
      <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="{{ 'alarm.search' | 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-toolbar class="mat-mdc-table-toolbar" color="primary" [class.!hidden]="alarmsDatasource.selection.isEmpty()">
      <div class="mat-toolbar-tools">
        <span>
          {{ translate.get('alarm.selected-alarms',
          {count: alarmsDatasource.selection.selected.length}) | async }}
        </span>
        <span class="flex-1"></span>
        @if (allowAcknowledgment) {
          <button
            mat-icon-button
            matTooltip="{{ 'alarm.acknowledge' | translate }}"
            matTooltipPosition="above"
            (click)="ackAlarms($event)">
            <mat-icon>done</mat-icon>
          </button>
        }
        @if (ctx.settings.allowClear) {
          <button
            mat-icon-button
            matTooltip="{{ 'alarm.clear' | translate }}"
            matTooltipPosition="above"
            (click)="clearAlarms($event)">
            <mat-icon>clear</mat-icon>
          </button>
        }
      </div>
    </mat-toolbar>
    <div class="table-container flex-1">
      <table mat-table [dataSource]="alarmsDatasource" [trackBy]="trackByAlarmId"
             matSort [matSortActive]="sortOrderProperty" [matSortDirection]="pageLinkSortDirection()" matSortDisableClear>
        <ng-container matColumnDef="select" sticky>
          <mat-header-cell *matHeaderCellDef style="width: 30px;">
            <mat-checkbox (change)="$event ? alarmsDatasource.masterToggle() : null"
                          [checked]="alarmsDatasource.selection.hasValue() && (alarmsDatasource.isAllSelected() | async)"
                          [indeterminate]="alarmsDatasource.selection.hasValue() && !(alarmsDatasource.isAllSelected() | async)">
            </mat-checkbox>
          </mat-header-cell>
          <mat-cell *matCellDef="let alarm; let row = index" [style]="rowStyle(alarm, row) | async">
            <mat-checkbox (click)="$event.stopPropagation();"
                          (change)="$event ? alarmsDatasource.toggleSelection(alarm) : null"
                          [checked]="alarmsDatasource.isSelected(alarm)">
            </mat-checkbox>
          </mat-cell>
        </ng-container>
        @for (column of columns; track trackByColumnDef($index, column)) {
          <ng-container [matColumnDef]="column.def">
            <mat-header-cell [style]="headerStyle(column)" *matHeaderCellDef mat-sort-header [disabled]="!column.sortable">
              {{ column.title }}
            </mat-header-cell>
            <mat-cell *matCellDef="let alarm; let row = index"
                      [style]="cellStyle(alarm, column, row) | async"
                      (click)="onCellClick($event, alarm, column, $index)"
                      [class.tb-pointer]="columnHasCellClick($index)">
              <span [innerHTML]="cellContent(alarm, column, row) | async"></span>
              @if (column.entityKey.key === 'assignee') {
                <span class="assignee-cell flex flex-row items-center justify-start">
                  @if (alarm.assigneeId && checkAssigneeHasName(alarm.assignee)) {
                    <span
                      class="assigned-container">
                      <span class="user-avatar" [style.backgroundColor]="getAvatarBgColor(alarm.assignee)">
                        {{ getUserInitials(alarm.assignee) }}
                      </span>
                      <span [matTooltip]="getUserDisplayName(alarm.assignee)" matTooltipPosition="above">
                        {{ getUserDisplayName(alarm.assignee) }}
                      </span>
                    </span>
                  } @else {
                    <span class="unassigned-container">
                      <mat-icon class="material-icons unassigned-icon">{{ alarm.assigneeId ? 'no_accounts' : 'account_circle' }}</mat-icon>
                      <span matTooltip="{{ (alarm.assigneeId ? 'alarm.user-deleted' : 'alarm.unassigned') | translate }}"
                            matTooltipPosition="above" style="vertical-align: middle">
                        {{ (alarm.assigneeId ? 'alarm.user-deleted' : 'alarm.unassigned') | translate }}
                      </span>
                    </span>
                  }
                  @if (allowAssign) {
                    <button
                      mat-icon-button
                      matTooltip="{{ 'alarm.assign' | translate }}"
                      matTooltipPosition="above"
                      (click)="openAlarmAssigneePanel($event, alarm)">
                      <mat-icon>
                        keyboard_arrow_down
                      </mat-icon>
                    </button>
                  }
                </span>
              }
            </mat-cell>
          </ng-container>
        }
        <ng-container matColumnDef="actions" [stickyEnd]="enableStickyAction">
          <mat-header-cell *matHeaderCellDef>
            @if (alarmsDatasource.countCellButtonAction) {
              <div class="gt-md:!hidden" style="min-width: 48px;">
              </div>
              <div class="lt-lg:!hidden"
                   [style.min-width]="(alarmsDatasource.countCellButtonAction * 48) + 'px'">
              </div>
            }
          </mat-header-cell>
          <mat-cell *matCellDef="let alarm; let row = index" [style]="rowStyle(alarm, row) | async">
            @if (alarmsDatasource.countCellButtonAction) {
              <div [class.lt-lg:!hidden]="showCellActionsMenu && alarmsDatasource.countCellButtonAction !== 1" class="flex flex-row items-stretch justify-end"
                   [style.min-width]="(alarmsDatasource.countCellButtonAction * 48) + 'px'">
                @for (actionDescriptor of alarm.actionCellButtons; track trackByActionCellDescriptionId($index, actionDescriptor)) {
                  @if (!actionDescriptor.icon) {
                    <span style="width: 48px;"></span>
                  }
                  @if (actionDescriptor.icon) {
                    <button mat-icon-button [disabled]="(isLoading$ | async) || !actionEnabled(alarm, actionDescriptor)"
                            matTooltip="{{ actionDescriptor.displayName }}"
                            matTooltipPosition="above"
                            (click)="onActionButtonClick($event, alarm, actionDescriptor)">
                      <mat-icon>{{actionDescriptor.icon}}</mat-icon>
                    </button>
                  }
                }
              </div>
              @if (alarm.hasActions) {
                <div [class.!hidden]="!showCellActionsMenu || alarmsDatasource.countCellButtonAction === 1" class="gt-md:!hidden">
                  <button mat-icon-button
                          (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 alarm.actionCellButtons; track trackByActionCellDescriptionId($index, actionDescriptor)) {
                      @if (actionDescriptor.icon) {
                        <button mat-menu-item
                                [disabled]="(isLoading$ | async) || !actionEnabled(alarm, actionDescriptor)"
                                (click)="onActionButtonClick($event, alarm, actionDescriptor)">
                          <tb-icon matMenuItemIcon>{{actionDescriptor.icon}}</tb-icon>
                          <span>{{ actionDescriptor.displayName }}</span>
                        </button>
                      }
                    }
                  </mat-menu>
                </div>
              }
            }
          </mat-cell>
        </ng-container>
        <mat-header-row [class.mat-row-select]="enableSelection" *matHeaderRowDef="displayedColumns; sticky: enableStickyHeader"></mat-header-row>
        <mat-row [class.mat-row-select]="enableSelection"
                 [class.mat-selected]="alarmsDatasource.isSelected(alarm)"
                 [class.tb-current-entity]="alarmsDatasource.isCurrentAlarm(alarm)"
                 [class.invisible]="alarmsDatasource.dataLoading"
                 [class.tb-pointer]="hasRowAction"
                 *matRowDef="let alarm; columns: displayedColumns; let row = index"
                 [style]="rowStyle(alarm, row) | async"
        (click)="onRowClick($event, alarm)"></mat-row>
      </table>
      <span [class.!hidden]="(alarmsDatasource.isEmpty() | async) === false || alarmsDatasource.dataLoading"
      class="no-data-found flex items-center justify-center">{{ noDataDisplayMessageText }}</span>
      <span [class.!hidden]="!alarmsDatasource.dataLoading"
      class="no-data-found flex items-center justify-center" >{{ 'common.loading' | translate }}</span>
    </div>
    @if (displayPagination) {
      <mat-divider></mat-divider>
    }
    @if (displayPagination) {
      <mat-paginator
        [length]="alarmsDatasource.total() | async"
        [pageIndex]="pageLink.page"
        [pageSize]="pageLink.pageSize"
        [pageSizeOptions]="pageSizeOptions"
        [hidePageSize]="hidePageSize"
      showFirstLastButtons></mat-paginator>
    }
  </div>
</div>
