drink-it-cold/views/availability.php

128 lines
6.5 KiB
PHP
Raw Normal View History

<?php
/** @var string $from */
/** @var string $to */
/** @var bool $checked */
/** @var array $errors */
/** @var array $groups */
/** @var int $freeCount */
/** @var int $total */
$fieldError = static fn (string $key): string => (string) ($errors[$key] ?? '');
$bookingUrl = static fn (array $trailer): string => '/bookings/new?trailer=' . (int) $trailer['id']
. '&start=' . rawurlencode($from) . '&end=' . rawurlencode($to);
$groupMeta = [
'trailer' => ['title' => 'Anhänger', 'empty' => 'Keine Anhänger hinterlegt.'],
'chest' => ['title' => 'Truhen', 'empty' => 'Keine Truhen hinterlegt.'],
];
?>
<div class="page-head page-head--compact">
<div>
<p class="eyebrow">Übersicht</p>
<h1>Verfügbarkeit</h1>
<p class="page-head__lede">Zeitraum wählen – die App zeigt, welche Anhänger und Truhen in dieser Zeit frei sind.</p>
</div>
<div class="page-head__actions">
<a class="btn" href="<?= e(url('/calendar?view=month&date=' . $from)) ?>">
<svg class="ico" aria-hidden="true"><use href="#i-calendar"/></svg> Im Kalender
</a>
<a class="btn" href="<?= e(url('/')) ?>">
<svg class="ico" aria-hidden="true"><use href="#i-grid"/></svg> Zur Übersicht
</a>
</div>
</div>
<section class="card">
<div class="card__head">
<h2>Zeitraum</h2>
<p>Ein Wechseltag ist erlaubt: Endet eine Buchung am Tag des neuen Beginns, gilt das Fahrzeug als frei.</p>
</div>
<form method="get" action="<?= e(url('/availability')) ?>" class="form-grid">
<label class="field<?= $fieldError('from') !== '' ? ' has-error' : '' ?>">
<span>Von</span>
<input type="date" name="from" value="<?= e($from) ?>" required>
<?php if ($fieldError('from')): ?><em class="field-error"><?= e($fieldError('from')) ?></em><?php endif; ?>
</label>
<label class="field<?= $fieldError('to') !== '' ? ' has-error' : '' ?>">
<span>Bis</span>
<input type="date" name="to" value="<?= e($to) ?>" required>
<?php if ($fieldError('to')): ?><em class="field-error"><?= e($fieldError('to')) ?></em><?php endif; ?>
</label>
<div class="field field--wide">
<button class="btn primary" type="submit">
<svg class="ico" aria-hidden="true"><use href="#i-search"/></svg> Verfügbarkeit prüfen
</button>
</div>
</form>
</section>
<?php if ($checked && !$errors): ?>
<section class="card">
<div class="card__head card__head--row">
<div>
<h2>Ergebnis</h2>
<p><?= e(fmt_date_range($from, $to)) ?> · <?= (int) $freeCount ?> von <?= (int) $total ?> frei</p>
</div>
<a class="btn small" href="<?= e(url('/bookings/new?start=' . rawurlencode($from) . '&end=' . rawurlencode($to))) ?>">
<svg class="ico" aria-hidden="true"><use href="#i-plus"/></svg> Neue Buchung
</a>
</div>
<div class="availability-grid">
<?php foreach ($groupMeta as $key => $meta): ?>
<?php
$rows = $groups[$key];
$free = count(array_filter($rows, static fn (array $row): bool => $row['free']));
?>
<div class="availability-group">
<h3>
<?= e($meta['title']) ?>
<span class="muted small"><?= $free ?> von <?= count($rows) ?> frei</span>
</h3>
<?php if (!$rows): ?>
<p class="muted small"><?= e($meta['empty']) ?></p>
<?php else: ?>
<ul class="availability-list">
<?php foreach ($rows as $row): ?>
<?php $trailer = $row['trailer']; ?>
<li class="availability-row<?= $row['free'] ? ' is-free' : ' is-busy' ?>">
<span class="availability-row__name">
<span class="trailer-chip" style="--trailer: <?= e($trailer['color_hex']) ?>; --trailer-text: <?= e(contrast_color($trailer['color_hex'])) ?>">
<span class="trailer-dot" style="--trailer: <?= e($trailer['color_hex']) ?>"></span>
<?= e($trailer['label']) ?> · <?= e($trailer['color_name']) ?>
</span>
</span>
<?php if ($row['free']): ?>
<span class="pill pill--green">frei</span>
<a class="btn small primary" href="<?= e(url($bookingUrl($trailer))) ?>">Buchen</a>
<?php else: ?>
<?php $conflict = $row['conflicts'][0]; ?>
<span class="pill pill--grey">belegt</span>
<span class="availability-row__hint muted small">
<?= e(fmt_date_range((string) $conflict['start_date'], (string) $conflict['end_date'])) ?>
· <?= e((string) $conflict['customer_name']) ?>
<?php if (count($row['conflicts']) > 1): ?>
(+<?= count($row['conflicts']) - 1 ?>)
<?php endif; ?>
</span>
<a class="link-button small" href="<?= e(url('/bookings/' . (int) $conflict['id'])) ?>">Ansehen</a>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
</section>
<?php elseif ($checked): ?>
<div class="alert alert--error" role="alert">
<svg class="ico" aria-hidden="true"><use href="#i-alert"/></svg>
<div><strong>Bitte den Zeitraum prüfen.</strong>
<?php foreach ($errors as $message): ?><p><?= e($message) ?></p><?php endforeach; ?>
</div>
</div>
<?php else: ?>
<div class="empty-state small">
<p>Zeitraum wählen und „Verfügbarkeit prüfen" – danach kannst du direkt buchen.</p>
</div>
<?php endif; ?>