<!--
 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.
-->

<div id="search-box">
  <nz-card
    [nzActions]="[actionstar, actionDelete]"
    class="workflow-section-title subsection-grid-container">
    <nz-input-group
      [nzPrefix]="prefixPopoverButton"
      [nzSuffix]="suffixIconSearch"
      class="texera-dashboard-saved-workflow-search-box-input">
      <input
        (keyup.enter)="searchExecution()"
        (ngModelChange)="searchInputOnChange($event)"
        [(ngModel)]="executionSearchValue"
        [nzAutocomplete]="auto"
        autocomplete="on"
        nz-input
        placeholder="Search all executions"
        type="text" />
      <ng-template #suffixIconSearch>
        <i
          nz-icon
          nzType="search"></i>
      </ng-template>
      <ng-template #popContent>
        We support the following search criteria:
        <ul>
          <li>Search by Execution Name: <strong>executionName</strong></li>
          <li>Search by Execution User: <strong>user:John</strong></li>
          <li>
            Search by Execution Status: <strong>status:initializing/running/paused/completed/failed/killed</strong>
          </li>
        </ul>
        <strong>
          For any execution name and user name, if the name contains space, using double quotes to enclose the name is
          required.
          <br />
        </strong>
        <br />
        Example: "Untitled Execution" user:John<br />
        Meaning: Search for the execution with name Untitled Execution and run by user called John.
      </ng-template>
      <ng-template #prefixPopoverButton>
        <i
          [nzPopoverContent]="popContent"
          nz-icon
          nz-popover
          nzPopoverTitle="Search Instructions"
          nzTheme="outline"
          nzType="question-circle"
          style="vertical-align: baseline; margin-right: 10px"></i>
      </ng-template>
      <nz-autocomplete
        #auto
        [nzDataSource]="filteredExecutionInfo"
        [nzDefaultActiveFirstOption]="false"
        nzBackfill></nz-autocomplete>
    </nz-input-group>
  </nz-card>
</div>
<ng-template #actionstar>
  <i
    (click)="setBookmarked()"
    *ngIf="setOfEid.size !== 0"
    [nzTooltipTitle]="executionTooltip['Group Bookmarking']"
    nz-icon
    nz-tooltip
    nzType="star"></i>
</ng-template>
<ng-template #actionDelete>
  <i
    (nzOnConfirm)="onGroupDelete()"
    *ngIf="setOfEid.size !== 0"
    [nzTooltipTitle]="executionTooltip['Group Deletion']"
    nz-icon
    nz-popconfirm
    nz-tooltip
    nzPopconfirmTitle="Confirm to delete this execution group."
    nzType="delete"></i>
</ng-template>
<div id="exection-history-table">
  <nz-tabs nzCentered>
    <nz-tab nzTitle="Executions Details">
      <nz-table
        (nzPageIndexChange)="onPageIndexChange($event)"
        (nzPageSizeChange)="onPageSizeChange($event)"
        *ngIf="workflowExecutionsDisplayedList"
        [nzData]="workflowExecutionsDisplayedList"
        [nzFrontPagination]="false"
        [nzPageIndex]="currentPageIndex"
        [nzPageSizeOptions]="pageSizeOptions"
        [nzPageSize]="pageSize"
        [nzShowPagination]="true"
        [nzShowSizeChanger]="true"
        [nzTotal]="allExecutionEntries.length">
        <thead>
          <tr>
            <th
              (nzCheckedChange)="onAllChecked($event)"
              [(nzChecked)]="checked"
              [ngStyle]="{'width': customColumnWidth['']}"></th>
            <th
              *ngFor="let column of executionsTableHeaders; let i=index"
              [ngStyle]="{'width': customColumnWidth[column]}"
              [nzTooltipTitle]="executionTooltip[column]"
              nz-tooltip
              nzTooltipPlacement="top">
              {{column}}
              <span *ngIf="column !== '' && column !== 'Status' && column !== 'Runtime Statistics'">
                <button
                  (click)="onHit(column, i)"
                  *ngIf="this.showORhide[i]"
                  nz-button>
                  <i
                    nz-icon
                    nzTheme="outline"
                    nzType="sort-ascending"></i>
                </button>
                <button
                  (click)="onHit(column, i)"
                  *ngIf="!this.showORhide[i]"
                  nz-button>
                  <i
                    nz-icon
                    nzTheme="outline"
                    nzType="sort-descending"></i>
                </button>
              </span>
            </th>
          </tr>
        </thead>

        <tbody>
          <tr *ngFor="let row of workflowExecutionsDisplayedList; let i=index;">
            <td
              (nzCheckedChange)="onItemChecked(row, $event)"
              [nzChecked]="setOfEid.has(row.eId)"
              nzEllipsis></td>
            <td nzEllipsis>
              <i
                (click)="onBookmarkToggle(row)"
                [nzTheme]="(row.bookmarked || currentlyHoveredExecution === row) ? 'fill' : 'outline'"
                class="bookmark-icon"
                nz-icon
                nzType="star"></i>
            </td>
            <td nzEllipsis>
              <texera-user-avatar
                [googleAvatar]="row.googleAvatar"
                userColor="setAvatarColor(row.userName)"
                [userName]="abbreviate(row.userName || 'anonymous', false)"></texera-user-avatar>
            </td>
            <td nzEllipsis>
              <label
                *ngIf="workflowExecutionsIsEditingName.indexOf(i) === -1; else customeWorkflow "
                class="execution-description workflow-name"
                >{{ abbreviate(row.name, true) }} ({{row.eId}})</label
              >
              <ng-template #customeWorkflow>
                <input
                  #customName
                  (focusout)="confirmUpdateWorkflowExecutionsCustomName(row, customName.value, i)"
                  (keyup.enter)="confirmUpdateWorkflowExecutionsCustomName(row, customName.value, i)"
                  placeholder="{{ row.name }}"
                  value="{{ row.name }}" />
              </ng-template>
              <button
                (click)="workflowExecutionsIsEditingName.push(i)"
                class="rename-icon"
                nz-button
                nzSize="small"
                nzType="text">
                <i
                  nz-icon
                  nz-tooltip
                  nzTheme="outline"
                  nzTooltipPlacement="top"
                  nzTooltipTitle='Rename Execution "{{row.name}}"'
                  nzType="edit"></i>
              </button>
            </td>
            <td nzEllipsis>{{ row.cuId }}</td>
            <td
              nzEllipsis
              nzTooltipTitle="{{row.startingTime | date:'zzz'}}"
              nz-tooltip
              nzTooltipPlacement="top">
              {{row.startingTime | date:'MM/dd/yyyy HH:mm:ss'}}
            </td>
            <td
              nzEllipsis
              nzTooltipTitle="{{row.completionTime | date:'zzz'}}"
              nz-tooltip
              nzTooltipPlacement="top">
              {{row.completionTime | date:'MM/dd/yyyy HH:mm:ss'}}
            </td>
            <td nzEllipsis>
              <i
                [ngStyle]="{'color': getExecutionStatus(row.status)[2]}"
                [nzTooltipTitle]="getExecutionStatus(row.status)[0]"
                [nzType]="getExecutionStatus(row.status)[1]"
                class="status-icon"
                nz-icon
                nz-tooltip
                nzTheme="outline"
                nzTooltipPlacement="top"></i>
            </td>
            <td nzEllipsis>
              <button
                nz-button
                nzType="default"
                nzSize="large"
                (click)="showRuntimeStatistics(row.eId, row.cuId)"
                nz-tooltip
                nzTooltipTitle="Display Runtime Statistics"
                nzTooltipPlacement="top">
                <span
                  nz-icon
                  nzType="line-chart"></span>
              </button>
            </td>
            <td nzEllipsis>
              <button
                nz-button
                nzType="default"
                nzSize="large"
                (nzOnConfirm)="onDelete(row)"
                nz-tooltip
                nzTooltipPlacement="top"
                nzTooltipTitle='Delete the Execution "{{row.name}}"'
                nz-popconfirm
                nzPopconfirmTitle="Confirm to delete this execution.">
                <span
                  nz-icon
                  nzType="delete"></span>
              </button>
            </td>
          </tr>
        </tbody>
      </nz-table>
    </nz-tab>
    <nz-tab
      nzForceRender
      nzTitle="Executions Statistic Charts">
      <div
        nz-row
        nzJustify="center">
        <div nz-col>
          <div id="{{ usernamePieChartId }}"></div>
        </div>
        <div nz-col>
          <div id="{{ statusPieChartId }}"></div>
        </div>
      </div>
      <div
        nz-row
        nzJustify="center">
        <div nz-col>
          <div id="{{ processTimeBarChart }}"></div>
        </div>
      </div>
    </nz-tab>
  </nz-tabs>
</div>
