<?php if (false === $this->compact): ?>
<div class="controls">
  <?= $this->tabs->render($this); ?>
  <div style="margin: 1em" class="dontprint">
    <?= $this->translate('Sort by'); ?> <?= $this->sortControl->render($this); ?>
    <?php if (! $this->filterEditor): ?>
        <?= $this->filterPreview ?>
    <?php endif; ?>
  </div>
  <?= $this->widget('limiter', array('url' => $this->url, 'max' => $downtimes->count())); ?>
  <?= $this->paginationControl($downtimes, null, null, array('preserve' => $this->preserve)); ?>
</div>
<?php endif ?>

<div class="content">
<?= $this->filterEditor ?>
<?php if (count($downtimes) === 0): ?>
  <?= $this->translate('No active downtimes'); ?>
</div>
<?php return; endif ?>

  <table data-base-target="_next" class="action">
    <tbody>
    <?php foreach ($downtimes as $downtime): ?>
    <?php
      if (isset($downtime->service)) {
          $stateName = strtolower($this->util()->getServiceStateName($downtime->service_state));
      } else {
          $stateName = strtolower($this->util()->getHostStateName($downtime->host_state));
      }
    ?>
      <tr class="state <?= $stateName; ?><?= $downtime->is_in_effect ? ' handled' : ''; ?>">
        <td class="state">
          <strong><?= $downtime->is_in_effect ? $this->translate('Expires') : $this->translate('Starts'); ?></strong>
          <br>
            <?=
            $this->dateTimeRenderer(
                ($downtime->is_in_effect ? $downtime->end : $downtime->start),
                true
            )->render(
                $this->translate('on %s', 'datetime'),
                $this->translate('at %s', 'time'),
                $this->translate('in %s', 'timespan')
            );
            ?>
        </td>
        <td>
        <?php if (isset($downtime->service)): ?>
          <a href="<?= $this->href('monitoring/service/show', array(
            'host'      => $downtime->host,
            'service'   => $downtime->service
          )); ?>">
            <?= $downtime->service; ?>
          </a>
          <small>
            <?= $this->translate('on'); ?> <?= $downtime->host; ?>
          </small>
        <?php else: ?>
          <a href="<?= $this->href('monitoring/host/show', array(
            'host' => $downtime->host
          )); ?>">
            <?= $downtime->host; ?>
          </a>
        <?php endif ?>
          <br>
          <?= $this->icon('comment'); ?> [<?= $downtime->author; ?>] <?= $downtime->comment; ?>
          <br>
          <small>
        <?php if ($downtime->is_flexible): ?>
          <?php if ($downtime->is_in_effect): ?>
            <?= sprintf(
              $this->translate('This flexible downtime was started on %s at %s and lasts for %s until %s at %s.'),
              date('d.m.y', $downtime->start),
              date('H:i', $downtime->start),
              $this->format()->duration($downtime->duration),
              date('d.m.y', $downtime->end),
              date('H:i', $downtime->end)
            ); ?>
          <?php else: ?>
            <?= sprintf(
              $this->translate('This flexible downtime has been scheduled to start between %s - %s and to last for %s.'),
              date('d.m.y H:i', $downtime->scheduled_start),
              date('d.m.y H:i', $downtime->scheduled_end),
              $this->format()->duration($downtime->duration)
            ); ?>
          <?php endif ?>
        <?php else: ?>
          <?php if ($downtime->is_in_effect): ?>
            <?= sprintf(
              $this->translate('This fixed downtime was started on %s at %s and expires on %s at %s.'),
              date('d.m.y', $downtime->start),
              date('H:i', $downtime->start),
              date('d.m.y', $downtime->end),
              date('H:i', $downtime->end)
            ); ?>
          <?php else: ?>
            <?= sprintf(
              $this->translate('This fixed downtime has been scheduled to start on %s at %s and to end on %s at %s.'),
              date('d.m.y', $downtime->scheduled_start),
              date('H:i', $downtime->scheduled_start),
              date('d.m.y', $downtime->scheduled_end),
              date('H:i', $downtime->scheduled_end)
            ); ?>
          <?php endif ?>
        <?php endif ?>
          </small>
        </td>
        <td style="width: 2em" data-base-target="self">
          <?php
          $delDowntimeForm = clone $delDowntimeForm;
          $delDowntimeForm->populate(array('downtime_id' => $downtime->id, 'redirect' => $this->url));
          if (! isset($downtime->service)) {
            $delDowntimeForm->setAction($this->url('monitoring/host/delete-downtime', array('host' => $downtime->host)));
          } else {
            $delDowntimeForm->setAction($this->url('monitoring/service/delete-downtime', array('host' => $downtime->host, 'service' => $downtime->service)));
          }
          echo $delDowntimeForm;
          ?>
        </td>
      </tr>
<?php endforeach ?>
    </tbody>
  </table>
</div>
