<!-- if cvcHeight not specified, render table with viewport target, without card height container -->
<ng-container *ngIf="!cvcHeight; else setHeightCard">
  <nz-card
    [nzTitle]="titleTemplate"
    [nzExtra]="extraTemplate"
    nzSize="small"
    cvcAutoHeightCard
    cvcAutoHeightTarget="viewport">
    <ng-template [ngTemplateOutlet]="table"> </ng-template>
  </nz-card>
</ng-container>

<!-- if cvcHeight is specified, render table in container w/ specified height-->
<ng-template #setHeightCard>
  <div
    class="card-container"
    [ngStyle]="{ height: cvcHeight }">
    <nz-card
      [nzTitle]="titleTemplate"
      [nzExtra]="extraTemplate"
      cvcAutoHeightCard
      nzSize="small">
      <ng-template [ngTemplateOutlet]="table"> </ng-template>
    </nz-card>
  </div>
</ng-template>

<ng-template #table>
  <!-- nzVirtualItemSize can be determined by inspecting the row element in Dev tools, and noting the height calculated in its Styles tab. this probably shouldn't change much, if at all. -->
  <nz-table
    #virtualTable
    nzSize="small"
    [nzData]="(row$ | ngrxPush) || []"
    [nzLoading]="!(row$ | ngrxPush)"
    [cvcAutoHeightTable]="25"
    [nzVirtualItemSize]="29"
    cvcTableScroll
    (cvcTableScrollOnScroll)="scrollEvent$.next($event)"
    [cvcTableScrollQueryRef]="queryRef"
    [cvcTableScrollPageInfo]="pageInfo$ | ngrxPush"
    [cvcTableScrollToIndex]="(scrollIndex$ | ngrxPush)!"
    [nzVirtualForTrackBy]="trackByIndex"
    [nzScroll]="{ x: '680px', y: '800px' }"
    [nzFrontPagination]="false"
    [nzShowPagination]="false">
    <thead (nzSortOrderChange)="sortChange$.next($event)">
      <tr class="col-header-row">
        <th
          nzWidth="200px"
          nzLeft>
          User
        </th>
        <th
          nzWidth="150px"
          [nzColumnKey]="sortColumns.Name"
          [nzSortFn]="true">
          Name
        </th>
        <th nzWidth="300px">Organizations</th>
        <th
          nzWidth="80px"
          [nzColumnKey]="sortColumns.Role"
          [nzSortFn]="true">
          Role
        </th>
        <th
          nzWidth="85px"
          nzAlign="right"
          [nzColumnKey]="sortColumns.LastAction"
          [nzSortFn]="true">
          Last Action
        </th>
        <th
          nzWidth="75px"
          nzRight
          nzAlign="right"
          nz-tooltip
          [nzColumnKey]="sortColumns.EvidenceCount"
          [nzSortFn]="true"
          nzTooltipTitle="Evidence Count">
          <i
            nz-icon
            nzType="civic-evidence"></i>
          Count
        </th>
        <th
          nzWidth="75px"
          nzRight
          nzAlign="right"
          nz-tooltip
          [nzColumnKey]="sortColumns.RevisionCount"
          [nzSortFn]="true"
          nzTooltipTitle="Revision Count">
          <i
            nz-icon
            nzType="civic-revision"></i>
          Count
        </th>
      </tr>
      <tr class="filter-row">
        <th nzLeft></th>
        <th>
          <cvc-clearable-input-filter
            [(inputModel)]="nameInput"
            (inputModelChange)="
              filterChange$.next()
            "></cvc-clearable-input-filter>
        </th>
        <th>
          <cvc-clearable-input-filter
            [(inputModel)]="orgNameInput"
            (inputModelChange)="
              filterChange$.next()
            "></cvc-clearable-input-filter>
        </th>
        <th>
          <nz-select
            nzAllowClear
            nzSize="small"
            nzPlaceHolder="Any"
            [nzDropdownMatchSelectWidth]="false"
            (ngModelChange)="filterChange$.next()"
            [(ngModel)]="roleInput">
            <nz-option
              nzValue="ADMIN"
              nzLabel="Admin"></nz-option>
            <nz-option
              nzValue="CURATOR"
              nzLabel="Curator"></nz-option>
            <nz-option
              nzValue="EDITOR"
              nzLabel="Editor"></nz-option>
          </nz-select>
        </th>
        <th></th>
        <th nzRight></th>
        <th nzRight></th>
      </tr>
    </thead>
    <tbody>
      <ng-template
        nz-virtual-scroll
        let-user
        let-index="index">
        <tr class="data-row">
          <td nzLeft>
            <cvc-user-tag
              [user]="user"
              [enablePopover]="!isScrolling"
              popoverPlacement="right"></cvc-user-tag>
          </td>
          <td class="overflow-ellipsis">
            <ng-container *ngIf="user.name; else nameElse">
              {{ user.name }}
            </ng-container>
            <ng-template #nameElse>
              <i
                nz-typography
                nzType="secondary">
                Not specified
              </i>
            </ng-template>
          </td>
          <td>
            <div *ngIf="user.organizations.length > 0; else orgElse">
              <cvc-tag-overflow
                tagType="organization"
                [maxDisplayCount]="1"
                [enablePopover]="!isScrolling"
                [tags]="user.organizations"
                [matchingText]="orgNameInput">
              </cvc-tag-overflow>
            </div>
            <ng-template #orgElse>
              <i
                nz-typography
                nzType="secondary"
                >None specified</i
              >
            </ng-template>
          </td>
          <td>
            {{ user.role | enumToTitle }}
          </td>
          <td nzAlign="right">
            <ng-container
              *ngIf="user.mostRecentActivityTimestamp; else eventElse">
              {{ user.mostRecentActivityTimestamp | timeAgo }}
            </ng-container>
            <ng-template #eventElse> -- </ng-template>
          </td>
          <td
            nzRight
            nzAlign="right">
            {{ user.evidenceCount }}
          </td>
          <td
            nzRight
            nzAlign="right">
            {{ user.revisionCount }}
          </td>
        </tr>
      </ng-template>
    </tbody>
  </nz-table>
</ng-template>

<!-- card's title template -->
<ng-template #titleTemplate>
  <ng-container *ngIf="cvcTitleTemplate">
    <ng-template [ngTemplateOutlet]="cvcTitleTemplate"></ng-template>
  </ng-container>
  <ng-container *ngIf="cvcTitle">
    {{ cvcTitle }}
  </ng-container>

  <span class="table-info">
    <cvc-table-counts
      [cvcTableCountsConnection]="connection$"></cvc-table-counts>
  </span>
</ng-template>

<!-- card's extra template -->
<ng-template #extraTemplate>
  <nz-tag
    *ngIf="moreLoading$ | ngrxPush"
    nzColor="processing">
    <i
      nz-icon
      nzType="sync"
      nzSpin></i>
    <span>Loading…</span>
  </nz-tag>
  <cvc-no-more-rows [cvcShowTag]="noMoreRows$ | ngrxPush"></cvc-no-more-rows>
</ng-template>
