{{!
* 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.
}}

{{#if view.isLoaded}}

  <div id="alert-definition-details">
  <div class="row-fluid">

    {{! Left column }}
    <div class="span9">
      {{! Alert Definition Name }}
      <div class="definition-name row-fluid">
        {{#if controller.editing.label.isEditing}}
          <div {{bindAttr class="controller.editing.label.isError:error :span5 :name-text-field :control-group"}}>
            {{view Em.TextField valueBinding="controller.editing.label.value"}}
          </div>
          <div class="edit-buttons">
            <button {{action cancelEdit controller.editing.label target="controller"}}
                class="btn cancel-button">{{t common.cancel}}</button>
            <button {{bindAttr disabled="controller.editing.label.isError"}} {{action saveEdit controller.editing.label target="controller"}}
                class="btn btn-primary save-button">{{t common.save}}
            </button>
          </div>
        {{else}}
          <span>
            {{controller.content.label}}
          </span>
        {{/if}}
        {{#unless controller.editing.label.isEditing}}
          {{#isAccessible ADMIN}}
            <a {{action edit controller.editing.label target="controller"}} class="edit-description-button"><i
                class="icon-pencil"></i></a>
          {{/isAccessible}}
        {{/unless}}
      </div>
      {{! Alert Definition Name end }}

      <div><a href="javascript:void(null)" data-toggle="modal" {{action back}}><i
          class="icon-arrow-left"></i>&nbsp;{{t common.back}}</a></div>

      {{! Alert Definition Configs }}
      <div class="box">
        <div class="box-header">
          <div class="pull-left">
            <h4>{{t common.configuration}}</h4>
          </div>
          <div class="pull-right span5 row-fluid" style="padding:0 10px;">
            {{#isAccessible ADMIN}}
              {{#unless App.router.mainAlertDefinitionConfigsController.canEdit}}
                <a {{action editConfigs target="App.router.mainAlertDefinitionConfigsController"}}
                    class="pull-right edit-link">
                  {{t common.edit}}
                </a>
              {{/unless}}
            {{/isAccessible}}
          </div>
        </div>
        {{view App.AlertDefinitionConfigsView contentBinding="view.controller.content" alertDefinitionTypeBinding="view.controller.content.type" canEdit=false}}
        {{#if App.router.mainAlertDefinitionConfigsController.canEdit}}
          <div class="edit-buttons">
            <button {{action cancelEditConfigs target="App.router.mainAlertDefinitionConfigsController"}}
                class="btn">{{t common.cancel}}</button>
            <button {{bindAttr disabled="App.router.mainAlertDefinitionConfigsController.hasErrors"}} {{action saveConfigs target="App.router.mainAlertDefinitionConfigsController"}}
                class="btn btn-primary">{{t common.save}}
            </button>
          </div>
        {{/if}}
      </div>
      {{! Alert Definition Configs end }}
    </div>
    {{! Left column end }}

    {{! Right column }}
    <div class="span3 right-column">
      <div class="status">
        {{{controller.content.status}}}
      </div>
      <div class="properties-list">
        <div class="row-fluid">
          <div class="span4 property-name">{{t alerts.table.state}}:</div>
          <div class="span8">
            {{#if controller.content.enabled}}
              {{#isAccessible ADMIN}}
                <a href="#" {{action "toggleState" controller.content target="controller"}} {{bindAttr class="controller.content.enabled:alert-definition-enable:alert-definition-disable"}}>
                  <span
                      class="enable-disable-button" {{translateAttr data-original-title="alerts.table.state.enabled.tooltip"}}>
                    {{view.enabledDisplay}}
                  </span>
                </a>
              {{/isAccessible}}
              {{#isAccessible NON_ADMIN}}
                <span {{bindAttr class="controller.content.enabled:alert-definition-enable:alert-definition-disable"}}>
                  {{t alerts.table.state.enabled}}
                </span>
              {{/isAccessible}}
            {{else}}
              {{#isAccessible ADMIN}}
                <a href="#" {{action "toggleState" controller.content target="controller"}} {{bindAttr class="controller.content.enabled:alert-definition-enable:alert-definition-disable"}}>
                  <span
                      class="enable-disable-button" {{translateAttr data-original-title="alerts.table.state.disabled.tooltip"}}>
                    {{view.disabledDisplay}}
                  </span>
                </a>
              {{/isAccessible}}
              {{#isAccessible NON_ADMIN}}
                <span {{bindAttr class="controller.content.enabled:alert-definition-enable:alert-definition-disable"}}>
                  {{t alerts.table.state.disabled}}
                </span>
              {{/isAccessible}}
            {{/if}}
          </div>
        </div>
        {{#if controller.content.serviceDisplayName}}
          <div class="row-fluid">
            <div class="span4 property-name">{{t common.service}}:</div>
            <div class="span8"><span>{{controller.content.serviceDisplayName}}</span></div>
          </div>
        {{/if}}
        {{#if controller.content.componentNameFormatted}}
          <div class="row-fluid">
            <div class="span4 property-name">{{t common.component}}:</div>
            <div class="span8">{{controller.content.componentNameFormatted}}</div>
          </div>
        {{/if}}
        <div class="row-fluid">
          <div class="span4 property-name">{{t common.type}}:</div>
          <div class="span8">
            <span {{bindAttr class=":type-icon  controller.content.typeIconClass"}}></span> {{controller.content.type}}
          </div>
        </div>
        {{#if controller.groupsList.length}}
          <div class="row-fluid">
            <div class="span4 property-name">{{t alerts.definition.details.groups}}:</div>
            <div class="span8">
              <ul>
                {{#each groupName in controller.groupsList}}
                  <li>{{groupName}}</li>
                {{/each}}
              </ul>
            </div>
          </div>
        {{/if}}
        <div class="row-fluid">
          <div class="span4 property-name">{{t alerts.table.header.lastTrigger}}:</div>
          <div class="span8">{{controller.content.lastTriggeredFormatted}}</div>
        </div>
      </div>
    </div>
  </div>
    {{! Right column end }}

    {{! List of alerts }}
  <div class="definition-details-block">
  <span>
    <strong>{{t alerts.definition.details.instances}}</strong>
  </span>
    <hr>
    <div>

      {{#if App.router.mainAlertInstancesController.isLoaded}}

        <table class="table advanced-header-table table-bordered table-striped alerts-table" id="alert-instances-table">
          <thead>
          <tr>
            <th class="first">{{t alerts.definition.details.serviceHost}}</th>
            <th>{{t common.status}}</th>
            <th>{{t alerts.definition.details.24-hour}}</th>
            <th>{{t alerts.table.header.check.response}}</th>
          </tr>
          </thead>
          <tbody>
            {{#if view.pageContent}}
              {{#each instance in view.pageContent}}
                {{#view view.instanceTableRow}}
                <td class="first">
                  <a {{action goToService instance.service target="controller"}}
                      href="#">{{instance.service.displayName}}</a>
                  {{#if instance.hostName}}
                    {{#if instance.service.displayName}}
                      &nbsp;/&nbsp;
                    {{/if}}
                    <a {{action goToHostAlerts instance.host target="controller"}} href="#">
                      {{instance.hostName}}
                    </a>
                  {{/if}}
                </td>
                <td>{{{instance.status}}}
                  <time class="timeago"
                        rel="tooltip"  {{bindAttr data-original-title="instance.statusChangedAndLastCheckedFormatted"}}>{{instance.lastTriggeredForFormatted}}</time>
                </td>
                <td>{{view view.parentView.lastDayCount hostNameBinding="instance.hostName"}}</td>
                <td><span class="alert-text" rel="tooltip" {{bindAttr data-original-title="instance.text"}}
                          class="alert-text">{{instance.text}}</span></td>
                {{/view}}
              {{/each}}
            {{else}}
            <tr>
              <td class="first" colspan="5">
                {{t alerts.definition.details.noAlerts}}
              </td>
            </tr>
            {{/if}}
          </tbody>
        </table>

        <div class="page-bar">
          <div class="items-on-page">
            <label>{{t common.show}}: {{view view.rowsPerPageSelectView selectionBinding="view.displayLength"}}</label>
          </div>
          <div class="info">{{view.paginationInfo}}</div>
          <div class="paging_two_button">
            <a {{bindAttr class="view.paginationLeftClass"}}{{action previousPage target="view"}}><i
                class="icon-arrow-left"></i></a>
            <a {{bindAttr class="view.paginationRightClass"}}{{action nextPage target="view"}}><i
                class="icon-arrow-right"></i></a>
          </div>
        </div>

      {{else}}
        <div class="spinner"></div>
      {{/if}}

    </div>
  </div>
    {{! List of alerts end }}

  </div>
{{else}}
  <div class="spinner"></div>
{{/if}}
