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

<app-help-message-show
  [help_message_content]="'alert.help.silence' | i18n"
  [guild_link]="'alert.help.silence.link' | i18n"
  [module_name]="'menu.alert.silence'"
  [icon_name]="'audio-muted'"
></app-help-message-show>

<nz-divider></nz-divider>

<app-toolbar>
  <ng-template #left>
    <button nz-button nzType="primary" (click)="sync()" nz-tooltip [nzTooltipTitle]="'common.refresh' | i18n">
      <i nz-icon nzType="sync" nzTheme="outline"></i>
    </button>
    <button nz-button nzType="primary" (click)="onNewAlertSilence()">
      <i nz-icon nzType="appstore-add" nzTheme="outline"></i>
      {{ 'common.button.new' | i18n }}
    </button>
    <button nz-button nz-dropdown [nzDropdownMenu]="more_menu">
      <span nz-icon nzType="ellipsis"></span>
    </button>
    <nz-dropdown-menu #more_menu="nzDropdownMenu">
      <ul nz-menu>
        <li nz-menu-item>
          <button nz-button nzDanger (click)="onDeleteAlertSilences()">
            <i nz-icon nzType="delete" nzTheme="outline"></i>
            {{ 'common.button.delete' | i18n }}
          </button>
        </li>
      </ul>
    </nz-dropdown-menu>
  </ng-template>
  <ng-template #right>
    <app-multi-func-input
      groupStyle="width: 250px;"
      [placeholder]="'alert.silence.name' | i18n"
      [(value)]="search"
      (keydown.enter)="onFilterChange()"
      (cleared)="onFilterChange()"
    />
    <button nz-button nzType="primary" (click)="onFilterChange()" class="mobile-hide">
      {{ 'common.search' | i18n }}
    </button>
  </ng-template>
</app-toolbar>

<nz-table
  #fixedTable
  [nzData]="silences"
  [nzPageIndex]="pageIndex"
  [nzPageSize]="pageSize"
  [nzTotal]="total"
  nzFrontPagination="false"
  [nzLoading]="tableLoading"
  nzShowSizeChanger
  [nzShowTotal]="rangeTemplate"
  [nzPageSizeOptions]="[8, 15, 25]"
  (nzQueryParams)="onTablePageChange($event)"
  nzShowPagination="true"
  [nzScroll]="{ x: '1240px' }"
>
  <thead>
    <tr>
      <th nzAlign="center" nzLeft nzWidth="3%" [(nzChecked)]="checkedAll" (nzCheckedChange)="onAllChecked($event)"></th>
      <th nzAlign="center" nzWidth="14%">{{ 'alert.silence.name' | i18n }}</th>
      <th nzAlign="center" nzWidth="12%">{{ 'alert.silence.type' | i18n }}</th>
      <th nzAlign="center" nzWidth="12%">{{ 'alert.silence.times' | i18n }}</th>
      <th nzAlign="center" nzWidth="12%" nzRight>{{ 'common.enable' | i18n }}</th>
      <th nzAlign="center" nzWidth="14%">{{ 'common.edit-time' | i18n }}</th>
      <th nzAlign="center" nzWidth="12%" nzRight>{{ 'common.edit' | i18n }}</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let data of fixedTable.data">
      <td nzAlign="center" nzLeft [nzChecked]="checkedSilenceIds.has(data.id)" (nzCheckedChange)="onItemChecked(data.id, $event)"></td>
      <td nzAlign="center">
        <span> {{ data.name }} </span>
      </td>
      <td nzAlign="center">
        <nz-tag *ngIf="data.type == 0" nzColor="processing">
          <i nz-icon nzType="clock-circle" nzTheme="outline"></i>
          <span>{{ 'alert.silence.type.once' | i18n }}</span>
        </nz-tag>
        <nz-tag *ngIf="data.type == 1" nzColor="processing">
          <i nz-icon nzType="sync" nzTheme="outline"></i>
          <span>{{ 'alert.silence.type.cyc' | i18n }}</span>
        </nz-tag>
      </td>
      <td nzAlign="center">
        <nz-tag nzColor="processing">
          <i nz-icon nzType="audio-muted" nzTheme="outline"></i>
          <span>{{ data.times == undefined ? 0 : data.times }}</span>
        </nz-tag>
      </td>
      <td nzAlign="center" nzRight>
        <nz-switch [(ngModel)]="data.enable" (ngModelChange)="updateAlertSilence(data)" name="enable"></nz-switch>
      </td>
      <td nzAlign="center">{{ (data.gmtUpdate ? data.gmtUpdate : data.gmtCreate) | date : 'YYYY-MM-dd HH:mm:ss' }}</td>
      <td nzAlign="center" nzRight>
        <div class="actions">
          <button
            nz-button
            nzType="primary"
            (click)="onEditAlertSilence(data.id)"
            nz-tooltip
            [nzTooltipTitle]="'alert.silence.edit' | i18n"
          >
            <i nz-icon nzType="edit" nzTheme="outline"></i>
          </button>
          <button nz-button nz-dropdown [nzDropdownMenu]="more_menu">
            <span nz-icon nzType="ellipsis"></span>
          </button>
          <nz-dropdown-menu #more_menu="nzDropdownMenu">
            <ul nz-menu>
              <li nz-menu-item>
                <button
                  nz-button
                  nzDanger
                  (click)="onDeleteOneAlertSilence(data.id)"
                  nz-tooltip
                  [nzTooltipTitle]="'alert.silence.delete' | i18n"
                >
                  <i nz-icon nzType="delete" nzTheme="outline"></i>
                </button>
              </li>
            </ul>
          </nz-dropdown-menu>
        </div>
      </td>
    </tr>
  </tbody>
</nz-table>

<ng-template #rangeTemplate> {{ 'common.total' | i18n }} {{ total }} </ng-template>

<!-- new or update alert silence pop-up box -->
<nz-modal
  [(nzVisible)]="isManageModalVisible"
  [nzTitle]="isManageModalAdd ? ('alert.silence.new' | i18n) : ('alert.silence.edit' | i18n)"
  (nzOnCancel)="onManageModalCancel()"
  (nzOnOk)="onManageModalOk()"
  nzMaskClosable="false"
  nzWidth="40%"
  [nzOkLoading]="isManageModalOkLoading"
>
  <div *nzModalContent class="-inner-content">
    <form nz-form #ruleForm="ngForm">
      <nz-form-item>
        <nz-form-label [nzSpan]="7" nzFor="rule_name" nzRequired="true">{{ 'alert.silence.name' | i18n }}</nz-form-label>
        <nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
          <input [(ngModel)]="silence.name" nz-input required name="rule_name" type="text" id="rule_name" />
        </nz-form-control>
      </nz-form-item>
      <nz-form-item>
        <nz-form-label nzSpan="7" nzRequired="true" nzFor="matchAll">{{ 'alert.silence.match-all' | i18n }}</nz-form-label>
        <nz-form-control nzSpan="12">
          <nz-switch [(ngModel)]="silence.matchAll" name="matchAll" id="matchAll"></nz-switch>
        </nz-form-control>
      </nz-form-item>
      <nz-form-item *ngIf="!silence.matchAll">
        <nz-form-label nzSpan="7" nzFor="labels" nzRequired="true">{{ 'alert.silence.labels' | i18n }}</nz-form-label>
        <nz-form-control nzSpan="12" [nzErrorTip]="'validation.required' | i18n">
          <app-form-field
            [item]="{
              field: 'labels',
              type: 'label-selector',
              required: true
            }"
            [name]="'labels'"
            [(ngModel)]="silence.labels"
          />
        </nz-form-control>
      </nz-form-item>
      <nz-form-item>
        <nz-form-label nzSpan="7" nzFor="type">{{ 'alert.silence.type' | i18n }}</nz-form-label>
        <nz-form-control nzSpan="12">
          <nz-radio-group [(ngModel)]="silence.type" nzButtonStyle="solid" id="type" name="type">
            <label nz-radio-button [nzValue]="0">{{ 'alert.silence.type.once' | i18n }}</label>
            <label nz-radio-button [nzValue]="1">{{ 'alert.silence.type.cyc' | i18n }}</label>
          </nz-radio-group>
        </nz-form-control>
      </nz-form-item>
      <nz-form-item *ngIf="silence.type == 0">
        <nz-form-label nzSpan="7" nzFor="date">{{ 'alert.silence.time' | i18n }}</nz-form-label>
        <nz-form-control nzSpan="12">
          <nz-range-picker
            [nzShowTime]="{ nzFormat: 'HH:mm' }"
            nzFormat="yyyy-MM-dd HH:mm"
            [(ngModel)]="silenceDates"
            id="date"
            name="date"
          ></nz-range-picker>
        </nz-form-control>
      </nz-form-item>
      <nz-form-item *ngIf="silence.type == 1">
        <nz-form-label nzSpan="7" nzFor="days">{{ 'alert.notice.rule.period-chose' | i18n }}</nz-form-label>
        <nz-form-control nzSpan="12">
          <nz-checkbox-group name="days" id="days" [(ngModel)]="dayCheckOptions"></nz-checkbox-group>
        </nz-form-control>
      </nz-form-item>
      <nz-form-item *ngIf="silence.type == 1">
        <nz-form-label nzSpan="7" nzFor="time">{{ 'alert.silence.time' | i18n }}</nz-form-label>
        <nz-form-control nzSpan="12">
          <nz-time-picker [(ngModel)]="silence.periodStart" nzFormat="HH:mm" name="periodStart" id="periodStart"></nz-time-picker>
          <span nz-icon nzType="right-circle" nzTheme="outline" style="margin-right: 6px; margin-left: 6px"></span>
          <nz-time-picker [(ngModel)]="silence.periodEnd" nzFormat="HH:mm" name="periodEnd" id="periodEnd"></nz-time-picker>
        </nz-form-control>
      </nz-form-item>
      <nz-form-item>
        <nz-form-label nzSpan="7" nzRequired="true" nzFor="enable">{{ 'common.enable' | i18n }}</nz-form-label>
        <nz-form-control nzSpan="12">
          <nz-switch [(ngModel)]="silence.enable" name="enable" id="enable"></nz-switch>
        </nz-form-control>
      </nz-form-item>
    </form>
  </div>
</nz-modal>
