<div *ngIf="loading">
  <i
    class="bwi bwi-spinner bwi-spin tw-text-muted"
    title="{{ 'loading' | i18n }}"
    aria-hidden="true"
  ></i>
  <span class="tw-sr-only">{{ "loading" | i18n }}</span>
</div>
<div class="tw-mt-4" *ngIf="!dataSource.data.length">
  <bit-no-items [icon]="noItemsIcon" class="tw-text-main">
    <ng-container slot="title">
      <h2 class="tw-font-semibold mt-4">
        {{ "noAppsInOrgTitle" | i18n: organization.name }}
      </h2>
    </ng-container>
    <ng-container slot="description">
      <div class="tw-flex tw-flex-col tw-mb-2">
        <span class="tw-text-muted">
          {{ "noAppsInOrgDescription" | i18n }}
        </span>
        <a class="text-primary" routerLink="/login">{{ "learnMore" | i18n }}</a>
      </div>
    </ng-container>
    <ng-container slot="button">
      <button (click)="goToCreateNewLoginItem()" bitButton buttonType="primary" type="button">
        {{ "createNewLoginItem" | i18n }}
      </button>
    </ng-container>
  </bit-no-items>
</div>
<div class="tw-mt-4 tw-flex tw-flex-col" *ngIf="!loading && dataSource.data.length">
  <h2 class="tw-mb-6" bitTypography="h2">{{ "allApplications" | i18n }}</h2>
  <div class="tw-flex tw-gap-6">
    <tools-card
      class="tw-flex-1"
      [title]="'atRiskMembers' | i18n"
      [value]="mockAtRiskMembersCount"
      [maxValue]="mockTotalMembersCount"
    >
    </tools-card>
    <tools-card
      class="tw-flex-1"
      [title]="'atRiskApplications' | i18n"
      [value]="mockAtRiskAppsCount"
      [maxValue]="mockTotalAppsCount"
    >
    </tools-card>
  </div>
  <div class="tw-flex tw-mt-8 tw-mb-4 tw-gap-4">
    <bit-search
      [placeholder]="'searchApps' | i18n"
      class="tw-grow"
      [formControl]="searchControl"
    ></bit-search>
    <button
      class="tw-rounded-lg"
      type="button"
      buttonType="secondary"
      bitButton
      *ngIf="isCritialAppsFeatureEnabled"
      [disabled]="!selectedIds.size"
      [loading]="markingAsCritical"
      (click)="markAppsAsCritical()"
    >
      <i class="bwi bwi-star-f tw-mr-2"></i>
      {{ "markAppAsCritical" | i18n }}
    </button>
  </div>
  <bit-table [dataSource]="dataSource">
    <ng-container header>
      <tr>
        <th *ngIf="isCritialAppsFeatureEnabled"></th>
        <th bitSortable="name" bitCell>{{ "application" | i18n }}</th>
        <th bitSortable="atRiskPasswords" bitCell>{{ "atRiskPasswords" | i18n }}</th>
        <th bitSortable="totalPasswords" bitCell>{{ "totalPasswords" | i18n }}</th>
        <th bitSortable="atRiskMembers" bitCell>{{ "atRiskMembers" | i18n }}</th>
        <th bitSortable="totalMembers" bitCell>{{ "totalMembers" | i18n }}</th>
      </tr>
    </ng-container>
    <ng-template body let-rows$>
      <tr bitRow *ngFor="let r of rows$ | async; trackBy: trackByFunction">
        <td *ngIf="isCritialAppsFeatureEnabled">
          <input
            bitCheckbox
            type="checkbox"
            [checked]="selectedIds.has(r.id)"
            (change)="onCheckboxChange(r.id, $event)"
          />
        </td>
        <td bitCell>
          <span>{{ r.name }}</span>
        </td>
        <td bitCell>
          <span>
            {{ r.atRiskPasswords }}
          </span>
        </td>
        <td bitCell>
          <span>
            {{ r.totalPasswords }}
          </span>
        </td>
        <td bitCell>
          <span>
            {{ r.atRiskMembers }}
          </span>
        </td>
        <td bitCell data-testid="total-membership">
          {{ r.totalMembers }}
        </td>
      </tr>
    </ng-template>
  </bit-table>
</div>
