<?php
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 (! $notifications->hasResult()): ?>
    <p><?= $this->translate('No notifications found matching the filter.') ?></p>
</div>
<?php return; endif ?>
    <table data-base-target="_next" class="table-row-selectable state-table">
    <tbody>
    <?php foreach ($notifications->peekAhead($this->compact) as $notification):
        if (isset($notification->service_description)) {
            $isService = true;
            $stateLabel = Service::getStateText($notification->notification_state, true);
            $stateName = Service::getStateText($notification->notification_state);
        } else {
            $isService = false;
            $stateLabel = Host::getStateText($notification->notification_state, true);
            $stateName = Host::getStateText($notification->notification_state);
        }
    ?>
        <tr href="<?= $this->href('monitoring/event/show', ['id' => $notification->id, 'type' => 'notify']) ?>">
            <td class="state-col state-<?= $stateName ?>">
                <div class="state-label"><?= $stateLabel ?></div>
                <div class="state-meta">
                    <?= $this->formatDateTime($notification->notification_timestamp) ?>
                </div>
            </td>
            <td>
                <div class="state-header">
                <?php if ($isService) {
                    echo '<span class="service-on">';
                    echo sprintf(
                        $this->translate('%s on %s', 'service on host'),
                        $this->qlink(
                            $notification->service_display_name,
                            'monitoring/service/show',
                            [
                                'host' => $notification->host_name,
                                'service' => $notification->service_description
                            ],
                            [
                                'title' => sprintf(
                                    $this->translate('Show detailed information for service %s on host %s'),
                                    $notification->service_display_name,
                                    $notification->host_display_name
                                )
                            ]
                        ),
                        $this->qlink(
                            $notification->host_display_name,
                            'monitoring/host/show',
                            ['host' => $notification->host_name],
                            [
                                'title' => sprintf(
                                    $this->translate('Show detailed information for host %s'),
                                    $notification->host_display_name
                                )
                            ]
                        )
                    );
                    echo '</span>';
                } else {
                    echo $this->qlink(
                        $notification->host_display_name,
                        'monitoring/host/show',
                        ['host' => $notification->host_name],
                        [
                            'title' => sprintf(
                                $this->translate('Show detailed information for host %s'),
                                $notification->host_display_name
                            )
                        ]
                    );
                } ?>
                <?php if (! $this->contact): ?>
                    <div class="notification-recipient">
                    <?php if ($notification->notification_contact_name): ?>
                        <?= sprintf(
                            $this->translate('Sent to %s'),
                            $this->qlink(
                                $notification->notification_contact_name,
                                'monitoring/show/contact',
                                array('contact_name' => $notification->notification_contact_name)
                            )
                        ) ?>
                    <?php else: ?>
                        <?= $this->translate('Not sent out to any contact') ?>
                    <?php endif ?>
                    </div>
                <?php endif ?>
                </div>
                <p class="overview-plugin-output"><?= $this->pluginOutput($this->ellipsis($notification->notification_output, 10000), true) ?></p>
            </td>
        </tr>
    <?php endforeach ?>
    </tbody>
    </table>
<?php if ($notifications->hasMore()): ?>
    <div class="action-links">
        <?= $this->qlink(
            $this->translate('Show More'),
            $this->url(isset($notificationsUrl) ? $notificationsUrl : null)->without(array('showCompact', 'limit')),
            null,
            array(
                'class'             => 'action-link',
                'data-base-target'  => '_next'
            )
        ); ?>
    </div>
<?php endif ?>
</div>
