<!--
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<kd-graph-metrics [metrics]="cumulativeMetrics"
                  *ngIf="showMetrics"></kd-graph-metrics>

<kd-card role="table"
         [hidden]="isHidden()">
  <div title
       fxLayout="row"
       i18n>Nodes</div>
  <div description><span class="kd-muted-light"
          i18n>Items:&nbsp;</span>{{totalItems}}</div>
  <div actions>
    <kd-card-list-filter></kd-card-list-filter>
  </div>

  <div content
       [hidden]="showZeroState()">
    <div kdLoadingSpinner
         [isLoading]="isLoading"></div>

    <mat-table [dataSource]="getData()"
               [trackBy]="trackByResource"
               matSort
               [matSortActive]="getColumns()[9]"
               matSortDisableClear
               matSortDirection="asc">
      <ng-container [matColumnDef]="getColumns()[0]">
        <mat-header-cell *matHeaderCellDef></mat-header-cell>
        <mat-cell *matCellDef="let node">
          <mat-icon [ngClass]="getStatus(node).iconClass"
                    [matTooltip]="getStatus(node).iconTooltip">
            {{getStatus(node).iconName}}
          </mat-icon>
        </mat-cell>
      </ng-container>

      <ng-container [matColumnDef]="getColumns()[1]">
        <mat-header-cell *matHeaderCellDef
                         mat-sort-header
                         disableClear="true"
                         class="kd-col-gt"
                         i18n>Name</mat-header-cell>
        <mat-cell *matCellDef="let node"
                  class="kd-col-gt">
          <a [routerLink]="getDetailsHref(node.objectMeta.name)"
             queryParamsHandling="preserve">
            {{node.objectMeta.name}}
          </a>
        </mat-cell>
      </ng-container>

      <ng-container [matColumnDef]="getColumns()[2]">
        <mat-header-cell *matHeaderCellDef
                         class="kd-col-gt"
                         i18n>Labels</mat-header-cell>
        <mat-cell *matCellDef="let node"
                  class="kd-col-gt">
          <kd-chips [map]="node.objectMeta.labels"></kd-chips>
        </mat-cell>
      </ng-container>

      <ng-container [matColumnDef]="getColumns()[3]">
        <mat-header-cell *matHeaderCellDef
                         class="kd-col-xs"
                         i18n>Ready</mat-header-cell>
        <mat-cell *matCellDef="let node"
                  class="kd-col-xs">{{node.ready}}</mat-cell>
      </ng-container>

      <ng-container [matColumnDef]="getColumns()[4]">
        <mat-header-cell *matHeaderCellDef
                         class="kd-col-sm"
                         i18n>CPU requests (cores)</mat-header-cell>
        <mat-cell *matCellDef="let node"
                  class="kd-col-sm">
          {{node.allocatedResources.cpuRequests | kdCores}} ({{node.allocatedResources.cpuRequestsFraction | number:"1.2-2"}}%)
        </mat-cell>
      </ng-container>

      <ng-container [matColumnDef]="getColumns()[5]">
        <mat-header-cell *matHeaderCellDef
                         class="kd-col-sm"
                         i18n>CPU limits (cores)</mat-header-cell>
        <mat-cell *matCellDef="let node"
                  class="kd-col-sm">
          {{node.allocatedResources.cpuLimits | kdCores}} ({{node.allocatedResources.cpuLimitsFraction | number:"1.2-2"}}%)
        </mat-cell>
      </ng-container>

      <ng-container [matColumnDef]="getColumns()[6]">
        <mat-header-cell *matHeaderCellDef
                         class="kd-col-sm"
                         i18n>Memory requests (bytes)</mat-header-cell>
        <mat-cell *matCellDef="let node"
                  class="kd-col-sm">
          {{node.allocatedResources.memoryRequests | kdMemory}} ({{node.allocatedResources.memoryRequestsFraction | number:"1.2-2"}}%)
        </mat-cell>
      </ng-container>

      <ng-container [matColumnDef]="getColumns()[7]">
        <mat-header-cell *matHeaderCellDef
                         class="kd-col-sm"
                         i18n>Memory limits (bytes)</mat-header-cell>
        <mat-cell *matCellDef="let node"
                  class="kd-col-sm">
          {{node.allocatedResources.memoryLimits | kdMemory}} ({{node.allocatedResources.memoryLimitsFraction | number:"1.2-2"}}%)
        </mat-cell>
      </ng-container>

      <ng-container [matColumnDef]="getColumns()[8]">
        <mat-header-cell *matHeaderCellDef
                         class="kd-col-sm"
                         i18n>Pods</mat-header-cell>
        <mat-cell *matCellDef="let node"
                  class="kd-col-sm">
          {{node.allocatedResources.allocatedPods}} ({{node.allocatedResources.podFraction | number:"1.2-2"}}%)
        </mat-cell>
      </ng-container>

      <ng-container [matColumnDef]="getColumns()[9]">
        <mat-header-cell *matHeaderCellDef
                         mat-sort-header
                         disableClear="true"
                         class="kd-col-xs"
                         i18n>Created</mat-header-cell>
        <mat-cell *matCellDef="let node"
                  class="kd-col-xs">
          <kd-date [date]="node.objectMeta.creationTimestamp"
                   relative></kd-date>
        </mat-cell>
      </ng-container>

      <ng-container *ngFor="let col of getActionColumns()"
                    [matColumnDef]="col.name">
        <mat-header-cell *matHeaderCellDef></mat-header-cell>
        <mat-cell *matCellDef="let node">
          <kd-dynamic-cell [component]="col.component"
                           [resource]="node"></kd-dynamic-cell>
        </mat-cell>
      </ng-container>

      <mat-header-row *matHeaderRowDef="getColumns()"></mat-header-row>
      <mat-row *matRowDef="let row; columns: getColumns();"></mat-row>
    </mat-table>

    <div [hidden]="totalItems <= itemsPerPage">
      <mat-paginator [length]="totalItems"
                     [pageSize]="itemsPerPage"
                     hidePageSize
                     showFirstLastButtons></mat-paginator>
    </div>
  </div>

  <div content
       [hidden]="!showZeroState()">
    <kd-list-zero-state></kd-list-zero-state>
  </div>
</kd-card>
