<!--

    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="mat-padding tb-entity-table tb-absolute-fill">
  <div class="tb-entity-table-content tb-outlined-border flex flex-1 flex-col">
    <mat-toolbar class="mat-mdc-table-toolbar" [class.!hidden]="textSearchMode || !dataSource.selection.isEmpty()">
      <div class="mat-toolbar-tools">
        <div class="title-container flex flex-row items-center justify-start xs:flex-col xs:items-start xs:justify-center pt-1">
          <span class="tb-entity-table-title">
            {{(direction == directions.FROM ? 'relation.from-relations' : 'relation.to-relations') | translate}}
          </span>
          <mat-form-field class="mat-block tb-relation-direction" appearance="outline" subscriptSizing="dynamic">
            <mat-label translate>relation.direction</mat-label>
            <mat-select [ngModel]="direction"
                        (ngModelChange)="directionChanged($event)">
              @for (type of directionTypes; track type) {
                <mat-option [value]="type">
                  {{ directionTypeTranslations.get(directions[type]) | translate }}
                </mat-option>
              }
            </mat-select>
          </mat-form-field>
        </div>
        <span class="flex-1"></span>
        <button mat-icon-button [disabled]="isLoading$ | async"
                (click)="addRelation($event)"
                matTooltip="{{ 'action.add' | translate }}"
                matTooltipPosition="above">
          <mat-icon>add</mat-icon>
        </button>
        <button mat-icon-button [disabled]="isLoading$ | async" (click)="reloadRelations()"
                matTooltip="{{ 'action.refresh' | translate }}"
                matTooltipPosition="above">
          <mat-icon>refresh</mat-icon>
        </button>
        <button mat-icon-button [disabled]="isLoading$ | async" (click)="enterFilterMode()"
                matTooltip="{{ 'action.search' | translate }}"
                matTooltipPosition="above">
          <mat-icon>search</mat-icon>
        </button>
      </div>
    </mat-toolbar>
    <mat-toolbar class="mat-mdc-table-toolbar" [class.!hidden]="!textSearchMode || !dataSource.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="{{ 'common.enter-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]="dataSource.selection.isEmpty()">
      <div class="mat-toolbar-tools flex items-center justify-between">
        <span class="tb-entity-table-info">
          {{ 'relation.selected-relations' | translate:{count: dataSource.selection.selected.length} }}
        </span>
        <button mat-icon-button [disabled]="isLoading$ | async"
                matTooltip="{{ 'action.delete' | translate }}"
                matTooltipPosition="above"
                (click)="deleteRelations($event)">
          <mat-icon>delete</mat-icon>
        </button>
      </div>
    </mat-toolbar>
    <div class="table-container flex-1">
      <table mat-table [dataSource]="dataSource"
             matSort [matSortActive]="pageLink.sortOrder.property" [matSortDirection]="pageLink.sortDirection()" matSortDisableClear>
        <ng-container matColumnDef="select" sticky>
          <mat-header-cell *matHeaderCellDef style="width: 30px;">
            <mat-checkbox (change)="$event ? dataSource.masterToggle() : null"
                          [checked]="dataSource.selection.hasValue() && (dataSource.isAllSelected() | async)"
                          [indeterminate]="dataSource.selection.hasValue() && !(dataSource.isAllSelected() | async)">
            </mat-checkbox>
          </mat-header-cell>
          <mat-cell *matCellDef="let relation">
            <mat-checkbox (click)="$event.stopPropagation()"
                          (change)="$event ? dataSource.selection.toggle(relation) : null"
                          [checked]="dataSource.selection.isSelected(relation)">
            </mat-checkbox>
          </mat-cell>
        </ng-container>
        <ng-container matColumnDef="type">
          <mat-header-cell *matHeaderCellDef mat-sort-header style="width: 33%"> {{ 'relation.type' | translate }} </mat-header-cell>
          <mat-cell *matCellDef="let relation" class="type">
            <div class="flex flex-row" style="align-items: center">
              <span class="ellipsis">{{ relation.type }}</span>
              <tb-copy-button
                class="type-copy"
                [disabled]="isLoading$ | async"
                [copyText]="relation.type"
                tooltipText="{{ 'relation.copy-type' | translate }}"
                tooltipPosition="above"
                icon="content_copy"
                [style]="{padding: '4px', 'font-size': '16px', color: 'rgba(0,0,0,.87)'}"
                >
              </tb-copy-button>
            </div>
          </mat-cell>
        </ng-container>
        @if (direction === directions.FROM) {
          <ng-container matColumnDef="toEntityTypeName">
            <mat-header-cell *matHeaderCellDef mat-sort-header style="width: 33%"> {{ 'relation.to-entity-type' | translate }} </mat-header-cell>
            <mat-cell *matCellDef="let relation">
              {{ relation.toEntityTypeName }}
            </mat-cell>
          </ng-container>
        }
        @if (direction === directions.FROM) {
          <ng-container matColumnDef="toName">
            <mat-header-cell *matHeaderCellDef mat-sort-header style="width: 33%"> {{ 'relation.to-entity-name' | translate }} </mat-header-cell>
            <mat-cell *matCellDef="let relation">
              @if(relation.entityURL){
                <a class="link" [routerLink]="relation.entityURL">
                  {{ relation.toName | customTranslate }}
                </a>
              } @else {
                {{ relation.toName | customTranslate }}
              }
            </mat-cell>
          </ng-container>
        }
        @if (direction === directions.TO) {
          <ng-container matColumnDef="fromEntityTypeName">
            <mat-header-cell *matHeaderCellDef mat-sort-header style="width: 33%"> {{ 'relation.from-entity-type' | translate }} </mat-header-cell>
            <mat-cell *matCellDef="let relation">
              {{ relation.fromEntityTypeName }}
            </mat-cell>
          </ng-container>
        }
        @if (direction === directions.TO) {
          <ng-container matColumnDef="fromName">
            <mat-header-cell *matHeaderCellDef mat-sort-header style="width: 33%"> {{ 'relation.from-entity-name' | translate }} </mat-header-cell>
            <mat-cell *matCellDef="let relation">
              @if(relation.entityURL){
                <a class="link" [routerLink]="relation.entityURL">
                  {{ relation.fromName | customTranslate }}
                </a>
              } @else {
                {{ relation.fromName | customTranslate }}
              }
            </mat-cell>
          </ng-container>
        }
        <ng-container matColumnDef="actions" stickyEnd>
          <mat-header-cell *matHeaderCellDef style="min-width: 80px; max-width: 80px; width: 80px">
          </mat-header-cell>
          <mat-cell *matCellDef="let relation">
            <div class="flex flex-1 flex-row items-stretch justify-end">
              <button mat-icon-button [disabled]="isLoading$ | async"
                      matTooltip="{{ 'relation.edit' | translate }}"
                      matTooltipPosition="above"
                      (click)="editRelation($event, relation)">
                <mat-icon>edit</mat-icon>
              </button>
              <button mat-icon-button [disabled]="isLoading$ | async"
                      matTooltip="{{ 'relation.delete' | translate }}"
                      matTooltipPosition="above"
                      (click)="deleteRelation($event, relation)">
                <mat-icon>delete</mat-icon>
              </button>
            </div>
          </mat-cell>
        </ng-container>
        <mat-header-row class="mat-row-select" *matHeaderRowDef="displayedColumns; sticky: true"></mat-header-row>
        <mat-row class="mat-row-select"
                 [class.mat-selected]="dataSource.selection.isSelected(relation)"
        *matRowDef="let relation; columns: displayedColumns;" (click)="dataSource.selection.toggle(relation)"></mat-row>
      </table>
      <span [class.!hidden]="(dataSource.isEmpty() | async) === false"
      class="no-data-found flex items-center justify-center" translate>{{ 'relation.no-relations-text' }}</span>
    </div>
    <mat-divider></mat-divider>
    <mat-paginator [length]="dataSource.total() | async"
                   [pageIndex]="pageLink.page"
                   [pageSize]="pageLink.pageSize"
                   [pageSizeOptions]="[10, 20, 30]"
    [hidePageSize]="hidePageSize"></mat-paginator>
  </div>
</div>
