<?php
$addLink = false;
if ($this->hasPermission('monitoring/command/downtime/schedule')) {
    /** @var \Icinga\Module\Monitoring\Object\MonitoredObject $object */
    if ($object->getType() === $object::TYPE_HOST) {
        $addLink = $this->qlink(
            $this->translate('Schedule downtime'),
            'monitoring/host/schedule-downtime',
            array('host' => $object->getName()),
            array(
                'class'             => 'action-link',
                'data-base-target'  => '_self',
                'icon'              => 'plug',
                'title'             => $this->translate(
                    'Schedule a downtime to suppress all problem notifications within a specific period of time'
                )
            )
        );
    } else {
        $addLink = $this->qlink(
            $this->translate('Schedule downtime'),
            'monitoring/service/schedule-downtime',
            array('host' => $object->getHost()->getName(), 'service' => $object->getName()),
            array(
                'class'             => 'action-link',
                'data-base-target'  => '_self',
                'icon'              => 'plug',
                'title'             => $this->translate(
                    'Schedule a downtime to suppress all problem notifications within a specific period of time'
                )
            )
        );
    }
}
if (empty($object->comments) && ! $addLink) {
    return;
}
?>
<tr>
    <th><?php
    echo $this->translate('Downtimes');
    if (! empty($object->downtimes) && $addLink) {
        echo '<br>' . $addLink;
    }
    ?></th>
    <td data-base-target="_self">
    <?php if (empty($object->downtimes)):
        echo $addLink;
    else: ?>
        <dl class="comment-list">
        <?php foreach ($object->downtimes as $downtime):
            if ((bool) $downtime->is_in_effect) {
                $state = sprintf(
                    $this->translate('expires %s', 'Last format parameter represents the downtime expire time'),
                    $this->timeUntil($downtime->end, false, true)
                );
            } else {
                if ($downtime->start <= time()) {
                    $state = sprintf(
                        $this->translate('ends %s', 'Last format parameter represents the end time'),
                        $this->timeUntil($downtime->is_flexible ? $downtime->scheduled_end : $downtime->end, false, true)
                    );
                } else {
                    $state = sprintf(
                        $this->translate('scheduled %s', 'Last format parameter represents the time scheduled'),
                        $this->timeUntil($downtime->start, false, true)
                    ) . ' ' . sprintf(
                        $this->translate('expires %s', 'Last format parameter represents the downtime expire time'),
                        $this->timeUntil($downtime->is_flexible ? $downtime->scheduled_end : $downtime->end, false, true)
                    );
                }
            }
        ?>
            <dt>
                <?= $this->escape(sprintf(
                    $downtime->is_flexible
                        ? $this->translate('Flexible downtime by %s')
                        : $this->translate('Fixed downtime by %s'),
                    $downtime->author_name
                )) ?>
                <span class="comment-time">
                    <?= $state ?>
                    <span aria-hidden="true">&#448;</span>
                    <?= $this->translate('created') ?>
                    <?= $this->timeAgo($downtime->entry_time) ?>
                </span>
                <?php if (isset($delDowntimeForm)) {
                    // Form is unset if the current user lacks the respective permission
                    $deleteButton = clone($delDowntimeForm);
                    /** @var \Icinga\Module\Monitoring\Forms\Command\Object\DeleteDowntimeCommandForm $deleteButton */
                    $deleteButton->setAttrib('class', $deleteButton->getAttrib('class') . ' remove-action');
                    $deleteButton->populate(
                        array(
                            'downtime_id'           => $downtime->id,
                            'downtime_is_service'   => $object->getType() === $object::TYPE_SERVICE,
                            'downtime_name'         => $downtime->name
                        )
                    );
                    echo $deleteButton;
                } ?>
            </dt>
            <dd>
                <?= $this->nl2br($this->createTicketLinks($this->markdown($downtime->comment))) ?>
            </dd>
        <?php endforeach ?>
        </dl>
    <?php endif ?>
    </td>
</tr>
