<!-- if cvcHeight not specified, render table without card height container & viewport target -->
<ng-container *ngIf="!cvcHeight; else setHeightCard">
  <!-- adjust nz-card's cvcAutoHeightCard to equalize padding around this table's container e.g. entity-home's nz-page-header components -->
  <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="250px"
          nzLeft
          [nzColumnKey]="sortColumns.Name"
          [nzSortFn]="true">
          Name
        </th>
        <th nzWidth="600px">Variants</th>
        <th nzWidth="150px">Features</th>
        <th
          nzWidth="75px"
          nzRight
          nzAlign="right"
          [nzColumnKey]="sortColumns.VariantCount"
          [nzSortFn]="true"
          [nzSortDirections]="['descend', 'ascend', null]"
          nzSortOrder="descend"
          nz-tooltip
          nzTooltipTitle="Variant Count">
          <i
            nz-icon
            nzType="civic-variant"></i>
          Count
        </th>
        <th
          nzWidth="75px"
          nzRight
          nzAlign="right"
          [nzColumnKey]="sortColumns.EvidenceItemCount"
          [nzSortFn]="true"
          [nzSortDirections]="['descend', 'ascend', null]"
          nz-tooltip
          nzTooltipTitle="Evidence Count">
          <i
            nz-icon
            nzType="civic-evidence"></i>
          Count
        </th>
      </tr>
      <tr class="filter-row">
        <th nzLeft>
          <cvc-clearable-input-filter
            [(inputModel)]="nameInput"
            (inputModelChange)="
              filterChange$.next()
            "></cvc-clearable-input-filter>
        </th>
        <th>
          <cvc-clearable-input-filter
            [(inputModel)]="variantNameInput"
            (inputModelChange)="
              filterChange$.next()
            "></cvc-clearable-input-filter>
        </th>
        <th>
          <cvc-clearable-input-filter
            [(inputModel)]="featureNameInput"
            (inputModelChange)="
              filterChange$.next()
            "></cvc-clearable-input-filter>
        </th>
        <th nzRight></th>
        <th nzRight></th>
      </tr>
    </thead>
    <tbody>
      <ng-template
        nz-virtual-scroll
        let-vg
        let-index="index">
        <tr class="data-row">
          <td nzLeft>
            <cvc-variant-group-tag
              [variantgroup]="vg"
              [enablePopover]="!isScrolling"
              popoverPlacement="right"></cvc-variant-group-tag>
          </td>
          <td
            class="overflow-ellipsis"
            nz-tooltip
            [nzTooltipTitle]="!isScrolling ? variantNames : ''">
            <ng-template #variantNames>
              <ng-container
                *ngFor="let variant of vg.variantNames; last as isLast">
                {{ variant }}{{ isLast ? '' : ', ' }}
              </ng-container>
            </ng-template>
            <ng-container [ngTemplateOutlet]="variantNames"></ng-container>
          </td>
          <td>
            <span *ngFor="let feature of vg.featureNames; last as isLast">
              {{ feature }}{{ isLast ? '' : ', ' }}
            </span>
          </td>
          <td
            nzRight
            nzAlign="right">
            {{ vg.variantCount }}
          </td>
          <td
            nzRight
            nzAlign="right">
            {{ vg.evidenceItemCount }}
          </td>
        </tr>
      </ng-template>
    </tbody>
  </nz-table>
</ng-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>

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