<!--

    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-card-content flex flex-col gap-2">
  @if (hasDashboardsAccess) {
    <div class="tb-card-header">
      <a class="tb-title-link" routerLink="/dashboards">{{ 'widgets.recent-dashboards.title' | translate }}</a>
      <div class="flex flex-row items-center justify-start gap-2 lt-md:items-center lt-md:justify-between">
        <tb-toggle-header #dashboardsToggle [value]="toggleValue" name="usageToggle" (valueChange)="toggleValueChange($event)">
          <tb-toggle-option value="last">{{ 'widgets.recent-dashboards.last' | translate }}</tb-toggle-option>
          <tb-toggle-option value="starred">{{ 'widgets.recent-dashboards.starred' | translate }}</tb-toggle-option>
        </tb-toggle-header>
        @if (authUser.authority === authority.TENANT_ADMIN) {
          @if (hasDevice) {
            <a class="md:!hidden"
            mat-flat-button color="primary" routerLink="/dashboards/all" [queryParams]="{action: 'add'}">{{ 'dashboard.add' | translate }}</a>
          } @else {
            <a class="md:!hidden"
            mat-stroked-button color="primary" routerLink="/dashboards/all" [queryParams]="{action: 'add'}">{{ 'dashboard.add' | translate }}</a>
          }
        }
      </div>
    </div>
    @if (userDashboardsInfo) {
      @switch (dashboardsToggle.value) {
        @case ('last') {
          @if (hasLastVisitedDashboards()) {
            <div style="overflow-y: auto;">
              <table mat-table
                     [dataSource]="lastVisitedDashboardsDataSource" matSort
                     [matSortActive]="lastVisitedDashboardsPageLink.sortOrder?.property"
                     [matSortDirection]="lastVisitedDashboardsPageLink.sortDirection()" matSortDisableClear>
                <ng-container matColumnDef="starred">
                  <mat-header-cell class="star-cell" *matHeaderCellDef>
                  </mat-header-cell>
                  <mat-cell class="star-cell" *matCellDef="let lastVisitedDashboard">
                    <mat-icon (click)="toggleDashboardStar(lastVisitedDashboard)"
                    class="star" [class.starred]="lastVisitedDashboard.starred">{{ lastVisitedDashboard.starred ? 'star' : 'star_border' }}</mat-icon>
                  </mat-cell>
                </ng-container>
                <ng-container matColumnDef="title">
                  <mat-header-cell class="title" *matHeaderCellDef mat-sort-header>
                    {{ 'widgets.recent-dashboards.name' | translate }}
                  </mat-header-cell>
                  <mat-cell class="title" *matCellDef="let lastVisitedDashboard">
                    <a [routerLink]="createDashboardUrl(lastVisitedDashboard.id)">{{ lastVisitedDashboard.title | customTranslate }}</a>
                  </mat-cell>
                </ng-container>
                <ng-container matColumnDef="lastVisited">
                  <mat-header-cell class="last-visited" *matHeaderCellDef mat-sort-header>
                    {{ 'widgets.recent-dashboards.last-viewed' | translate }}
                  </mat-header-cell>
                  <mat-cell class="last-visited" *matCellDef="let lastVisitedDashboard">
                    {{ lastVisitedDashboard.lastVisited | dateAgo:{applyAgo: true} }}
                  </mat-cell>
                </ng-container>
                <mat-header-row *matHeaderRowDef="lastVisitedDashboardsColumns; sticky: true"></mat-header-row>
                <mat-row *matRowDef="let lastVisitedDashboard; columns: lastVisitedDashboardsColumns;"></mat-row>
              </table>
            </div>
          } @else {
            <div class="tb-no-dashboards">
              <div class="tb-no-last-visited-bg"></div>
              <div class="tb-no-last-visited-dashboards-text">{{ 'widgets.recent-dashboards.no-last-viewed-dashboards' | translate }}</div>
            </div>
          }
        }
        @case ('starred') {
          <div style="overflow-y: auto;">
            @for (dashboard of userDashboardsInfo?.starred; track dashboard) {
              <div class="tb-starred-dashboard-row">
                <div class="tb-cell star-cell">
                  <mat-icon (click)="toggleDashboardStar(dashboard)"
                  class="star" [class.starred]="dashboard.starred">{{ dashboard.starred ? 'star' : 'star_border' }}</mat-icon>
                </div>
                <div class="tb-cell title">
                  <a [routerLink]="createDashboardUrl(dashboard.id)">{{ dashboard.title | customTranslate }}</a>
                </div>
              </div>
            }
          </div>
          <tb-dashboard-autocomplete class="tb-star-dashboard-autocomplete"
                                     #starDashboardAutocomplete
                                     subscriptSizing="dynamic"
                                     appearance="outline"
                                     [useIdValue]="false"
                                     [customerId]="customerId"
                                     label=""
                                     placeholder="{{ 'dashboard.select-dashboard' | translate }}"
          [(ngModel)]="starredDashboardValue" (ngModelChange)="onStarDashboard($event)"></tb-dashboard-autocomplete>
        }
      }
    } @else {
      <div class="tb-no-dashboards">
        <mat-spinner [diameter]="50" mode="indeterminate"></mat-spinner>
      </div>
    }
  } @else {
    <div class="tb-card-header">
      <div class="tb-title">{{ 'widgets.recent-dashboards.title' | translate }}</div>
    </div>
    <div class="tb-no-data-available">
      <div class="tb-no-data-bg"></div>
      <div class="tb-no-data-text" translate>widgets.home.no-data-available</div>
    </div>
  }
</div>
