<?php
use Icinga\Date\DateFormatter;
use Icinga\Module\Monitoring\Object\Host;
use Icinga\Module\Monitoring\Object\Service;

if (! $this->compact): ?>
<div class="controls">
    <?= $this->tabs ?>
    <?= $this->paginator ?>
    <div class="sort-controls-container">
        <?= $this->limiter ?>
        <?= $this->sortBox ?>
    </div>
    <?= $this->filterEditor ?>
</div>
<?php endif ?>
<div class="content">
<?php if (! $services->hasResult()): ?>
    <p><?= $this->translate('No services found matching the filter.') ?></p>
</div>
<?php return; endif ?>
    <table data-base-target="_next"
           class="table-row-selectable state-table multiselect<?php if ($this->compact): ?> compact<?php endif ?>"
           data-icinga-multiselect-url="<?= $this->href('monitoring/services/show') ?>"
           data-icinga-multiselect-controllers="<?= $this->href('monitoring/services') ?>"
           data-icinga-multiselect-data="service,host">
    <tbody>
    <?php foreach ($services->peekAhead($this->compact) as $service):
        $serviceLink = $this->href(
            'monitoring/service/show',
            array(
                'host'      => $service->host_name,
                'service'   => $service->service_description
            )
        );
        $hostLink = $this->href(
            'monitoring/host/show',
            array(
                'host' => $service->host_name,
            )
        );
        $serviceStateName = Service::getStateText($service->service_state);
        $serviceCheckOverdue = $service->service_next_update < time(); ?>
        <tr<?= $serviceCheckOverdue ? ' class="state-outdated"' : '' ?>>
            <td class="state-col state-<?= $serviceStateName ?><?= $service->service_handled ? ' handled' : '' ?>">
                <div class="state-label">
                <?php if ($serviceCheckOverdue): ?>
                    <?= $this->icon('clock', sprintf($this->translate('Overdue %s'), DateFormatter::timeSince($service->service_next_update))) ?>
                <?php endif ?>
                    <?= Service::getStateText($service->service_state, true) ?>
                </div>
            <?php if ((int) $service->service_state !== 99): ?>
                <div class="state-meta">
                    <?= $this->timeSince($service->service_last_state_change, $this->compact) ?>
                <?php if ((int) $service->service_state > 0 && (int) $service->service_state_type === 0): ?>
                    <div><?= $this->translate('Soft', 'Soft state') ?> <?= $service->service_attempt ?></div>
                <?php endif ?>
                </div>
            <?php endif ?>
            </td>

            <td>
                <div class="state-header">
                  <span class="service-on">
                  <?= $this->iconImage()->service($service) ?>
                  <?php
                  if ($this->showHost) {
                      echo sprintf(
                          $this->translate('%s on %s', 'service on host'),
                          $this->qlink(
                              $service->service_display_name,
                              $serviceLink,
                              null,
                              array(
                                  'title' => sprintf(
                                      $this->translate('Show detailed information for service %s on host %s'),
                                      $service->service_display_name,
                                      $service->host_display_name
                                  ),
                                  'class' => 'rowaction'
                              )
                          ),
                              $this->qlink(
                                  $service->host_display_name
                                  . ($service->host_state != 0 ? ' (' . Host::getStateText($service->host_state, true) . ')' : ''),
                                  $hostLink,
                                  null,
                                  [
                                      'title' => sprintf(
                                          $this->translate('Show detailed information for host %s'),
                                          $service->host_display_name
                                      )
                                  ]
                              )
                          );
                  } else {
                      echo $this->qlink(
                          $service->service_display_name,
                          $serviceLink,
                          null,
                          array(
                              'title' => sprintf(
                                  $this->translate('Show detailed information for service %s on host %s'),
                                  $service->service_display_name,
                                  $service->host_display_name
                              ),
                              'class' => 'rowaction'
                          )
                      );
                  }
                  ?>
                  </span>
                  <span class="state-icons"><?= $this->serviceFlags($service) ?></span>
                </div>
                <div class="overview-plugin-output-container">
                    <div class="overview-performance-data">
                        <?= $this->perfdata($service->service_perfdata, true, 5) ?>
                    </div>
                    <p class="overview-plugin-output"><?= $this->pluginOutput($this->ellipsis($service->service_output, 10000), true, $service->service_check_command) ?></p>
                </div>
            </td>
        <?php foreach($this->addColumns as $col): ?>
            <td><?= $this->escape($service->$col) ?></td>
        <?php endforeach ?>
        </tr>
    <?php endforeach ?>
    </tbody>
    </table>
<?php if ($services->hasMore()): ?>
<div class="action-links">
    <?= $this->qlink(
        $this->translate('Show More'),
        $this->url()->without(array('showCompact', 'limit')),
        null,
        array(
            'class'             => 'action-link',
            'data-base-target'  => '_next'
        )
    ) ?>
</div>
<?php endif ?>
</div>
<?php if (! $this->compact): ?>
<div class="monitoring-statusbar dont-print">
    <?= $this->render('list/components/servicesummary.phtml') ?>
    <?= $this->render('list/components/selectioninfo.phtml') ?>
</div>
<?php endif ?>
