<div *ngIf="!serviceAccounts" class="tw-items-center tw-justify-center tw-pt-64 tw-text-center">
  <i class="bwi bwi-spinner bwi-spin bwi-3x"></i>
</div>

<bit-no-items *ngIf="serviceAccounts?.length == 0">
  <ng-container slot="title">{{ "machineAccountsNoItemsTitle" | i18n }}</ng-container>
  <ng-container slot="description">{{ "machineAccountsNoItemsMessage" | i18n }}</ng-container>
  <button
    slot="button"
    type="button"
    bitButton
    buttonType="secondary"
    (click)="newServiceAccountEvent.emit()"
    [startIcon]="btnTextAddCreateFeatureFlag() ? null : 'bwi-plus'"
  >
    {{ (btnTextAddCreateFeatureFlag() ? "addMachineAccount" : "newMachineAccount") | i18n }}
  </button>
</bit-no-items>

<bit-table *ngIf="serviceAccounts?.length >= 1" [dataSource]="dataSource">
  <ng-container header>
    <tr>
      <th bitCell class="tw-w-0">
        <input
          type="checkbox"
          (change)="$event ? toggleAll() : null"
          [checked]="selection.hasValue() && isAllSelected()"
          [indeterminate]="selection.hasValue() && !isAllSelected()"
          [attr.aria-label]="'selectAll' | i18n"
        />
      </th>
      <th bitCell colspan="2" bitSortable="name" default>{{ "name" | i18n }}</th>
      <th bitCell>{{ "secrets" | i18n }}</th>
      <th bitCell bitSortable="revisionDate">{{ "lastEdited" | i18n }}</th>
      <th bitCell class="tw-w-0">
        <button
          type="button"
          bitIconButton="bwi-ellipsis-v"
          buttonType="primaryGhost"
          [label]="'options' | i18n"
          [bitMenuTriggerFor]="tableMenu"
        ></button>
      </th>
    </tr>
  </ng-container>
  <ng-template body let-rows$>
    <tr bitRow *ngFor="let serviceAccount of rows$ | async">
      <td bitCell>
        <input
          type="checkbox"
          (change)="$event ? selection.toggle(serviceAccount.id) : null"
          [checked]="selection.isSelected(serviceAccount.id)"
        />
      </td>
      <td bitCell class="tw-w-0 tw-pr-0">
        <i class="bwi bwi-wrench tw-text-muted" aria-hidden="true"></i>
      </td>
      <td bitCell class="tw-break-all">
        @if (serviceAccount.decryptionError) {
          <span
            class="tw-cursor-pointer tw-text-warning tw-underline"
            role="button"
            tabindex="0"
            [title]="'clickToRenameServiceAccount' | i18n"
            [attr.aria-label]="'clickToRenameServiceAccount' | i18n"
            (click)="editServiceAccountEvent.emit(serviceAccount.id); $event.stopPropagation()"
            (keydown.enter)="
              editServiceAccountEvent.emit(serviceAccount.id); $event.stopPropagation()
            "
            (keydown.space)="
              editServiceAccountEvent.emit(serviceAccount.id);
              $event.stopPropagation();
              $event.preventDefault()
            "
          >
            {{ serviceAccount.name }}
            <i class="bwi bwi-pencil-square tw-ml-1" aria-hidden="true"></i>
          </span>
        } @else {
          <a bitLink [routerLink]="serviceAccount.id">
            {{ serviceAccount.name }}
          </a>
        }
      </td>
      <td bitCell>
        <span> {{ serviceAccount.accessToSecrets }} </span>
      </td>
      <td bitCell>{{ serviceAccount.revisionDate | date: "medium" }}</td>
      <td bitCell>
        <button
          type="button"
          bitIconButton="bwi-ellipsis-v"
          buttonType="primaryGhost"
          [label]="'options' | i18n"
          [bitMenuTriggerFor]="serviceAccountMenu"
        ></button>
      </td>
      <bit-menu #serviceAccountMenu>
        <a type="button" bitMenuItem [routerLink]="serviceAccount.id">
          <i class="bwi bwi-fw bwi-eye" aria-hidden="true"></i>
          {{ "viewMachineAccount" | i18n }}
        </a>
        <button type="button" bitMenuItem (click)="editServiceAccountEvent.emit(serviceAccount.id)">
          <i class="bwi bwi-fw bwi-pencil" aria-hidden="true"></i>
          {{ "editMachineAccount" | i18n }}
        </button>
        <button
          type="button"
          bitMenuItem
          *ngIf="viewEventsAllowed$ | async as allowed"
          (click)="openEventsDialog(serviceAccount)"
        >
          <i class="bwi bwi-fw bwi-billing" aria-hidden="true"></i>
          <span> {{ "viewEvents" | i18n }} </span>
        </button>
        <button type="button" bitMenuItem variant="danger" (click)="delete(serviceAccount)">
          <bit-icon fixedWidth name="bwi-trash" slot="start" />
          {{ "deleteMachineAccount" | i18n }}
        </button>
      </bit-menu>
    </tr>
  </ng-template>
</bit-table>

<bit-menu #tableMenu>
  <button type="button" bitMenuItem variant="danger" (click)="bulkDeleteServiceAccounts()">
    <bit-icon fixedWidth name="bwi-trash" slot="start" />
    {{ "deleteMachineAccounts" | i18n }}
  </button>
</bit-menu>
