<!--
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>Pods</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()"
               matSort
               matSortActive="age"
               matSortDisableClear
               matSortDirection="asc">
      <ng-container matColumnDef="statusicon">
        <mat-header-cell *matHeaderCellDef></mat-header-cell>
        <mat-cell *matCellDef="let pod; let index=index">
          <mat-icon [ngClass]="getStatus(pod).iconClass">
            <ng-container *ngIf="showHoverIcon(index, pod); else showStatus">
              remove_red_eye
            </ng-container>

            <ng-template #showStatus>
              {{getStatus(pod).iconName}}
            </ng-template>
          </mat-icon>
        </mat-cell>
      </ng-container>

      <ng-container matColumnDef="name">
        <mat-header-cell *matHeaderCellDef
                         mat-sort-header
                         disableClear="true"
                         i18n>Name</mat-header-cell>
        <mat-cell *matCellDef="let pod">
          <a (click)="$event.stopPropagation()"
             [routerLink]="getDetailsHref(pod.objectMeta.name, pod.objectMeta.namespace)"
             queryParamsHandling="preserve">
            {{pod.objectMeta.name}}
          </a>
        </mat-cell>
      </ng-container>

      <ng-container *ngIf="shouldShowColumn('namespace')"
                    matColumnDef="namespace">
        <mat-header-cell *matHeaderCellDef
                         i18n>Namespace</mat-header-cell>
        <mat-cell *matCellDef="let pod">{{pod.objectMeta.namespace}}</mat-cell>
      </ng-container>

      <ng-container matColumnDef="labels">
        <mat-header-cell *matHeaderCellDef
                         i18n>Labels</mat-header-cell>
        <mat-cell *matCellDef="let pod">
          <kd-chips [map]="pod.objectMeta.labels"></kd-chips>
        </mat-cell>
      </ng-container>

      <ng-container matColumnDef="node">
        <mat-header-cell *matHeaderCellDef
                         i18n>Node</mat-header-cell>
        <mat-cell *matCellDef="let pod">{{pod.nodeName}}</mat-cell>
      </ng-container>

      <ng-container matColumnDef="status">
        <mat-header-cell *matHeaderCellDef
                         mat-sort-header
                         disableClear="true"
                         i18n>Status</mat-header-cell>
        <mat-cell *matCellDef="let pod">{{getDisplayStatus(pod)}}</mat-cell>
      </ng-container>

      <ng-container matColumnDef="restarts">
        <mat-header-cell *matHeaderCellDef
                         i18n>Restarts</mat-header-cell>
        <mat-cell *matCellDef="let pod">
          {{pod.restartCount}}
        </mat-cell>
      </ng-container>

      <ng-container matColumnDef="cpu">
        <mat-header-cell *matHeaderCellDef
                         i18n>CPU Usage (cores)</mat-header-cell>
        <mat-cell *matCellDef="let pod">
          <kd-cpu-sparkline *ngIf="pod.metrics?.cpuUsageHistory"
                            [timeseries]="pod.metrics.cpuUsageHistory"></kd-cpu-sparkline>
          <span *ngIf="!pod.metrics?.cpuUsageHistory">-</span>
        </mat-cell>
      </ng-container>

      <ng-container matColumnDef="mem">
        <mat-header-cell *matHeaderCellDef
                         i18n>Memory Usage (bytes)</mat-header-cell>
        <mat-cell *matCellDef="let pod">
          <kd-memory-sparkline *ngIf="pod.metrics?.memoryUsageHistory"
                               [timeseries]="pod.metrics.memoryUsageHistory"></kd-memory-sparkline>
          <span *ngIf="!pod.metrics?.memoryUsageHistory">-</span>
        </mat-cell>
      </ng-container>

      <ng-container matColumnDef="age">
        <mat-header-cell *matHeaderCellDef
                         mat-sort-header
                         disableClear="true"
                         i18n>Age</mat-header-cell>
        <mat-cell *matCellDef="let pod">
          <kd-date [date]="pod.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 pod">
          <kd-dynamic-cell [component]="col.component"
                           [resource]="pod"></kd-dynamic-cell>
        </mat-cell>
      </ng-container>

      <mat-header-row *matHeaderRowDef="getColumns()"></mat-header-row>
      <mat-row #matrow
               (mouseover)="onRowOver(index)"
               (mouseleave)="onRowLeave()"
               (click)="expand(index, row)"
               [ngClass]="{'kd-no-bottom-border': isRowExpanded(index), 'kd-clickable': hasErrors(row)}"
               *matRowDef="let row; columns: getColumns(); let index=index"></mat-row>
    </mat-table>

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

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