<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one
  ~ or more contributor license agreements.  See the NOTICE file
  ~ distributed with this work for additional information
  ~ regarding copyright ownership.  The ASF licenses this file
  ~ to you 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.
  -->

<nz-tabs [nzSize]="'small'" [nzAnimated]="{ inkBar: true, tabPane: false }">
  <nz-tab nzTitle="Root Exception">
    <ng-template nz-tab>
      <nz-code-editor
        flinkAutoResize
        [nzLoading]="isLoading"
        [ngModel]="rootException"
        [nzEditorOption]="editorOptions"
      ></nz-code-editor>
    </ng-template>
  </nz-tab>
  <nz-tab nzTitle="Exception History">
    <ng-template nz-tab>
      <nz-table
        class="no-border small"
        [nzSize]="'small'"
        [nzData]="exceptionHistory"
        [nzShowPagination]="false"
        [nzFrontPagination]="false"
      >
        <thead>
          <tr>
            <th nzWidth="60px"></th>
            <th>Time</th>
            <th>Exception</th>
            <th>Task Name</th>
            <th>Location</th>
          </tr>
        </thead>
        <tbody>
          <ng-container *ngFor="let item of exceptionHistory; trackBy: trackByTimestamp">
            <tr>
              <td nzShowExpand [(nzExpand)]="item.expand"></td>
              <td>{{ item.selected.timestamp | date: 'yyyy-MM-dd HH:mm:ss' }}</td>
              <td>
                <div class="name">{{ item.selected.exceptionName }}</div>
                <nz-tag
                  *ngFor="let label of item.selected.failureLabels | keyvalue"
                  [nzColor]="'#ff0000'"
                >
                  {{ label.key }}:{{ label.value }}
                </nz-tag>
              </td>
              <td class="select-td">
                <nz-select
                  class="exception-select"
                  nzSize="small"
                  [nzDisabled]="item.exceptions.length === 1"
                  nzShowSearch
                  [(ngModel)]="item.selected"
                >
                  <nz-option
                    *ngFor="let ex of item.exceptions"
                    nzCustomContent
                    [nzLabel]="ex.taskName"
                    [nzValue]="ex"
                  >
                    <span
                      nz-tooltip
                      [nzTooltipTitle]="ex.taskName"
                      nzTooltipMouseEnterDelay="0.5"
                      nzTooltipPlacement="left"
                    >
                      {{ ex.taskName }}
                    </span>
                  </nz-option>
                </nz-select>
              </td>
              <td class="clickable" (click)="navigateTo(item.selected.taskManagerId)">
                <a>
                  {{
                    item.selected.endpoint
                      ? item.selected.taskManagerId + '(' + item.selected.endpoint + ')'
                      : '(unassigned)'
                  }}
                </a>
              </td>
            </tr>
            <tr [nzExpand]="item.expand">
              <td colspan="5" class="expand-td">
                <nz-code-editor
                  *ngIf="item.expand"
                  flinkAutoResize
                  class="subtask"
                  [ngModel]="item.selected.stacktrace"
                  [nzEditorOption]="editorOptions"
                ></nz-code-editor>
              </td>
            </tr>
          </ng-container>
          <tr *ngIf="exceptionHistory.length > 0">
            <td colspan="5">
              <i nz-icon nzType="info-circle" nzTheme="fill"></i>
              &nbsp;
              <i>
                The exception history is limited to the most recent failures that caused parts of
                the job or the entire job to restart. The maximum history size can be configured via
                the Flink configuration property
                <b>web.exception-history-size</b>
                .
              </i>
            </td>
          </tr>
          <tr *ngIf="truncated">
            <td colspan="5">
              <button
                nz-button
                nzBlock
                nzType="primary"
                nzGhost
                (click)="loadMore()"
                [nzLoading]="isLoading"
              >
                Load More
              </button>
            </td>
          </tr>
        </tbody>
      </nz-table>
    </ng-template>
  </nz-tab>
</nz-tabs>
