<!-- 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>
  <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
          [nzColumnKey]="sortColumns.Name"
          [nzSortFn]="true">
          Name
        </th>
        <th
          nzWidth="100px"
          nzLeft
          [nzColumnKey]="sortColumns.Doid"
          [nzSortFn]="true">
          DOID
        </th>
        <th nzWidth="200px">Aliases</th>
        <th nzWidth="300px">Features</th>
        <th
          nzWidth="75px"
          nzRight
          nzAlign="right"
          [nzColumnKey]="sortColumns.FeatureCount"
          [nzSortFn]="true"
          [nzSortDirections]="['descend', 'ascend', null]"
          nz-tooltip
          nzTooltipTitle="Feature Count">
          <i
            nz-icon
            nzType="civic-feature"></i>
          Count
        </th>
        <th
          nzWidth="75px"
          nzRight
          nzAlign="right"
          [nzColumnKey]="sortColumns.VariantCount"
          [nzSortFn]="true"
          [nzSortDirections]="['descend', 'ascend', null]"
          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]"
          nzSortOrder="descend"
          nz-tooltip
          nzTooltipTitle="Evidence Count">
          <i
            nz-icon
            nzType="civic-evidence"></i>
          Count
        </th>
        <th
          nzWidth="75px"
          nzRight
          [nzColumnKey]="sortColumns.AssertionCount"
          [nzSortFn]="true"
          [nzSortDirections]="['descend', 'ascend', null]"
          nz-tooltip
          nzTooltipTitle="Assertion Count">
          <i
            nz-icon
            nzType="civic-assertion"></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 nzLeft>
          <cvc-clearable-input-filter
            [(inputModel)]="doidInput"
            (inputModelChange)="
              filterChange$.next()
            "></cvc-clearable-input-filter>
        </th>
        <th>
          <cvc-clearable-input-filter
            [(inputModel)]="diseaseAliasInput"
            (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>
        <th nzRight></th>
        <th nzRight></th>
      </tr>
    </thead>
    <tbody>
      <ng-template
        nz-virtual-scroll
        let-disease
        let-index="index">
        <tr class="data-row">
          <td nzLeft>
            <cvc-disease-tag
              [disease]="disease"
              [enablePopover]="!isScrolling"
              popoverPlacement="right"></cvc-disease-tag>
          </td>
          <td nzLeft>
            <cvc-link-tag
              *ngIf="disease.doid; else noDoid"
              [href]="disease.diseaseUrl"
              tooltip="View on disease-ontology.org">
              {{ 'DOID:' + disease.doid }}
            </cvc-link-tag>

            <ng-template #noDoid> -- </ng-template>
          </td>
          <td class="overflow-ellipsis">
            <cvc-plain-tag-overflow
              [maxDisplayCount]="1"
              [tags]="disease.diseaseAliases"
              [matchingText]="diseaseAliasInput"></cvc-plain-tag-overflow>
          </td>
          <td class="overflow-ellipsis">
            <cvc-tag-overflow
              [enablePopover]="!isScrolling"
              tagType="feature"
              [tags]="disease.features"
              [maxDisplayCount]="5"
              [matchingText]="featureNameInput">
            </cvc-tag-overflow>
          </td>
          <td
            nzRight
            nzAlign="right">
            {{ disease.featureCount }}
          </td>
          <td
            nzRight
            nzAlign="right">
            {{ disease.variantCount }}
          </td>
          <td
            nzRight
            nzAlign="right">
            {{ disease.evidenceItemCount }}
          </td>
          <td
            nzRight
            nzAlign="right">
            {{ disease.assertionCount }}
          </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>
