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

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 (! $hosts->hasResult()): ?>
    <p><?= $this->translate('No hosts found matching the filter.') ?></p>
</div>
<?php return; endif ?>
    <table data-base-target="_next"
           class="table-row-selectable state-table multiselect"
           data-icinga-multiselect-url="<?= $this->href('monitoring/hosts/show') ?>"
           data-icinga-multiselect-controllers="<?= $this->href("monitoring/hosts") ?>"
           data-icinga-multiselect-data="host">
    <tbody>
    <?php foreach($hosts->peekAhead($this->compact) as $host):
        $hostStateName = Host::getStateText($host->host_state);
        $hostLink = $this->href('monitoring/host/show', array('host' => $host->host_name));
        $hostCheckOverdue = $host->host_next_update < time();?>
        <tr<?= $hostCheckOverdue ? ' class="state-outdated"' : '' ?>>
            <td class="state-col state-<?= $hostStateName ?><?= $host->host_handled ? ' handled' : '' ?>">
                <div class="state-label">
                <?php if ($hostCheckOverdue): ?>
                    <?= $this->icon('clock', sprintf($this->translate('Overdue %s'), DateFormatter::timeSince($host->host_next_update))) ?>
                <?php endif ?>
                    <?= Host::getStateText($host->host_state, true) ?>
                </div>
                <?php if ((int) $host->host_state !== 99): ?>
                    <div class="state-meta">
                        <?= $this->timeSince($host->host_last_state_change, $this->compact) ?>
                    <?php if ((int) $host->host_state > 0 && (int) $host->host_state_type === 0): ?>
                        <div><?= $this->translate('Soft', 'Soft state') ?> <?= $host->host_attempt ?></div>
                    <?php endif ?>
                    </div>
                <?php endif ?>
            </td>
            <td>
                <div class="state-header">
                    <?= $this->iconImage()->host($host) ?>
                    <?= $this->qlink(
                        $host->host_display_name,
                        $hostLink,
                        null,
                        array(
                            'title' => sprintf(
                                $this->translate('Show detailed information for host %s'),
                                $host->host_display_name
                            ),
                            'class' => 'rowaction'
                        )
                    ) ?>
                <?php if (isset($summary[$host->host_name])): ?>
                    <span class="host-services-incidents"> &#40;<?= $this->qlink(
                          sprintf(
                              $this->translatePlural(
                                  '%u unhandled service', '%u unhandled services', $summary[$host->host_name]
                              ),
                              $summary[$host->host_name]
                          ),
                          'monitoring/list/services',
                          array(
                              'host'               => $host->host_name,
                              'service_problem'    => 1,
                              'service_handled'    => 0
                          ),
                          array(
                              'title' => sprintf(
                                  $this->translatePlural(
                                      'List %s unhandled service problem on host %s',
                                      'List %s unhandled service problems on host %s',
                                      $summary[$host->host_name]
                                  ),
                                  $summary[$host->host_name],
                                  $host->host_name
                              )
                          )
                    ) ?>&#41;</span>
                <?php endif ?>
                    <span class="state-icons"><?= $this->hostFlags($host) ?></span>
                </div>
                <p class="overview-plugin-output"><?= $this->pluginOutput($this->ellipsis($host->host_output, 10000), true, $host->host_check_command) ?></p>
            </td>
        <?php foreach($this->addColumns as $col): ?>
          <?php if ($host->$col && preg_match('~^_(host|service)_([a-zA-Z0-9_]+)$~', $col, $m)): ?>
            <td><?= $this->escape(\Icinga\Module\Monitoring\Object\MonitoredObject::protectCustomVars([$m[2] => $host->$col])[$m[2]]) ?></td>
          <?php else: ?>
            <td><?= $this->escape($host->$col) ?></td>
          <?php endif ?>
        <?php endforeach ?>
        </tr>
    <?php endforeach ?>
    </tbody>
    </table>
<?php if ($hosts->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/hostssummary.phtml') ?>
    <?= $this->render('list/components/selectioninfo.phtml') ?>
</div>
<?php endif ?>
