<app-header></app-header>

<div class="tw-mb-4" [formGroup]="eventsForm">
  <div class="tw-mt-4 tw-flex tw-items-center">
    <bit-form-field>
      <bit-label>{{ "startDate" | i18n }}</bit-label>
      <input
        bitInput
        type="datetime-local"
        placeholder="{{ 'startDate' | i18n }}"
        formControlName="start"
        (change)="dirtyDates = true"
      />
    </bit-form-field>
    <span class="tw-mx-2">-</span>
    <bit-form-field>
      <bit-label>{{ "endDate" | i18n }}</bit-label>
      <input
        bitInput
        type="datetime-local"
        placeholder="{{ 'endDate' | i18n }}"
        formControlName="end"
        (change)="dirtyDates = true"
      />
    </bit-form-field>
    <form>
      <button
        class="tw-mx-3 tw-mt-1"
        type="button"
        bitButton
        bitFormButton
        buttonType="primary"
        [bitAction]="refreshEvents"
      >
        {{ "refresh" | i18n }}
      </button>
    </form>
    <form>
      <button
        type="button"
        class="tw-mt-1"
        bitButton
        bitFormButton
        [bitAction]="exportEvents"
        [disabled]="dirtyDates"
        endIcon="bwi-sign-in"
      >
        {{ "exportVerb" | i18n }}
      </button>
    </form>
  </div>
</div>
@if (!loaded()) {
  <ng-container>
    <bit-icon
      class="bwi-lg bwi-spin tw-text-muted"
      name="bwi-spinner"
      aria-hidden="true"
    ></bit-icon>
    <span class="tw-sr-only">{{ "loading" | i18n }}</span>
  </ng-container>
}
@if (loaded()) {
  <ng-container>
    @if (!events() || !events().length) {
      <p>{{ "noEventsInList" | i18n }}</p>
    }

    @if (events() && events().length) {
      <bit-table data-testid="events-table">
        <ng-container header>
          <tr>
            <th bitCell>{{ "timestamp" | i18n }}</th>
            <th bitCell>{{ "device" | i18n }}</th>
            <th bitCell>{{ "user" | i18n }}</th>
            <th bitCell>{{ "event" | i18n }}</th>
          </tr>
        </ng-container>
        <ng-template body>
          @for (e of events(); track i; let i = $index) {
            <tr bitRow alignContent="top">
              <td bitCell class="tw-whitespace-nowrap">{{ e.date | date: "medium" }}</td>
              <td bitCell>
                <bit-icon
                  class="bwi-lg tw-text-muted"
                  [name]="asBitwardenIcon(e.appIcon)"
                  aria-hidden="true"
                ></bit-icon>
                <span class="tw-sr-only">{{ e.appName }}, {{ e.ip }}</span>
              </td>
              <td bitCell>
                <span title="{{ e.userEmail }}">{{ e.userName }}</span>
              </td>
              <td bitCell [innerHTML]="e.message"></td>
            </tr>
          }
        </ng-template>
      </bit-table>
    }
    @if (continuationToken) {
      <button bitButton type="button" buttonType="primary" [bitAction]="loadMoreEvents">
        @if (loading()) {
          <bit-icon
            class="bwi-lg bwi-spin tw-text-muted"
            name="bwi-spinner"
            aria-hidden="true"
          ></bit-icon>
        }
        <span>{{ "loadMore" | i18n }}</span>
      </button>
    }
  </ng-container>
}
