<?php
declare(strict_types=1);
require_once __DIR__ . '/app/Helpers/bootstrap.php';
require_once __DIR__ . '/app/Helpers/trip-changes.php';
ob_start(static function (string $html): string {
    if (language() === 'de') { return $html; }
    return strtr($html, ['<html lang="de">' => '<html lang="en">', 'Sehenswürdigkeit hinzufügen · TripTale' => t('program.add_attraction') . ' · TripTale', '>← Zurück zur Reise<' => '>' . t('common.back_to_trip') . '<', '>Sehenswürdigkeit hinzufügen<' => '>' . t('program.add_attraction') . '<', '>Sehenswürdigkeit<select' => '>' . t('type.attraction') . '<select', '>Bitte auswählen …<' => '>' . t('team.select') . '<', '>Uhrzeit (optional)<input' => '>Time (optional)<input', '>Zum Reiseplan hinzufügen<' => '>' . t('custom_points.add_to_trip') . '<', '>Zum Suchen Namen oder Ort eingeben – anschließend den passenden Eintrag in der Liste auswählen.<' => '>To search, enter a name or place and then select the matching entry from the list.<', 'placeholder="Namen eingeben …"' => 'placeholder="Enter a name …"', '>Bitte eine Sehenswürdigkeit auswählen.<' => '>Please select an attraction.<', '>Bitte eine gültige Uhrzeit eingeben.<' => '>Please enter a valid time.<', '>Diese Sehenswürdigkeit ist nicht verfügbar.<' => '>This attraction is not available.', '>Es gibt noch keine aktiven Sehenswürdigkeiten. Lege zuerst eine unter „Sehenswürdigkeiten“ an.<' => '>There are no active attractions yet. Create one under “Attractions” first.']);
});
$user = require_login();
$dayId = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
if (!$dayId) { http_response_code(404); exit('Reisetag nicht gefunden.'); }
$dayQuery = db()->prepare('SELECT d.id, d.trip_id, d.day_number, d.date, d.title, t.created_by FROM ' . table('trip_days') . ' d JOIN ' . table('trips') . ' t ON t.id = d.trip_id WHERE d.id = :id');
$dayQuery->execute([':id' => $dayId]); $day = $dayQuery->fetch();
if (!$day) { http_response_code(404); exit('Reisetag nicht gefunden.'); }
$canEdit = $user['role'] === 'main_admin' || (int)$day['created_by'] === (int)$user['id'];
if (!$canEdit) { $check = db()->prepare('SELECT 1 FROM ' . table('trip_admins') . ' WHERE trip_id=:trip_id AND user_id=:user_id'); $check->execute([':trip_id'=>$day['trip_id'], ':user_id'=>$user['id']]); $canEdit=(bool)$check->fetchColumn(); }
if (!$canEdit) { http_response_code(403); exit('Du darfst diesen Reisetag nicht bearbeiten.'); }
$attractions = db()->query('SELECT id, name, city FROM ' . table('attractions') . ' WHERE status = \'active\' ORDER BY name')->fetchAll();
$error = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    verify_csrf(); $attractionId = filter_input(INPUT_POST, 'attraction_id', FILTER_VALIDATE_INT, ['options'=>['min_range'=>1]]); $time=trim((string)($_POST['time'] ?? ''));
    if (!$attractionId) { $error='Bitte eine Sehenswürdigkeit auswählen.'; }
    elseif ($time !== '' && !preg_match('/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/', $time)) { $error='Bitte eine gültige Uhrzeit eingeben.'; }
    else { $exists=db()->prepare('SELECT name FROM ' . table('attractions') . ' WHERE id=:id AND status=\'active\''); $exists->execute([':id'=>$attractionId]); $attractionName=$exists->fetchColumn(); if (!$attractionName) { $error='Diese Sehenswürdigkeit ist nicht verfügbar.'; } else { $order=(int)db()->query('SELECT COALESCE(MAX(sort_order),0)+1 FROM ' . table('trip_day_items') . ' WHERE trip_day_id='.(int)$dayId)->fetchColumn(); $insert=db()->prepare('INSERT INTO ' . table('trip_day_items') . ' (trip_day_id,item_type,reference_id,time,sort_order) VALUES (:day_id,\'attraction\',:attraction_id,:time,:sort_order)'); $insert->execute([':day_id'=>$dayId,':attraction_id'=>$attractionId,':time'=>$time?:null,':sort_order'=>$order]); $changeId=triptale_log_trip_change((int)$day['trip_id'],(int)$user['id'],'item_added','Sehenswürdigkeit „'.$attractionName.'“ wurde zu Tag '.(int)$day['day_number'].' hinzugefügt.'); header('Location: /day-program.php?id='.(int)$dayId.'&saved=1'); exit; } }
}
?>
<!doctype html><html lang="de"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Sehenswürdigkeit hinzufügen · TripTale</title><style>body{font-family:system-ui,sans-serif;background:#f4f6f8;margin:0;padding:32px;color:#17202a}main{max-width:650px;margin:auto;background:#fff;padding:28px;border-radius:14px;box-shadow:0 4px 18px #0001}label{display:block;margin-top:16px;font-weight:600}select,input{box-sizing:border-box;width:100%;padding:11px;margin-top:6px;border:1px solid #b9c1c8;border-radius:7px;font:inherit}.button{display:inline-block;padding:8px 11px;background:#e3ebee;color:#126e82;border-radius:7px;text-decoration:none;font-weight:700}.save{margin-top:22px;padding:12px 16px;border:0;border-radius:7px;background:#126e82;color:#fff;font-weight:700}.error{background:#ffe6e3;padding:12px;border-radius:7px}*{box-sizing:border-box}html,body{max-width:100%;overflow-x:hidden}img,video{max-width:100%;height:auto}input,textarea,select,.editor{max-width:100%}@media(max-width:600px){body{padding:12px!important}header{padding:14px 16px!important;flex-wrap:wrap;gap:10px}main{width:100%!important;margin:18px auto!important;padding:16px!important}.top{flex-wrap:wrap;align-items:flex-start}.top>div{width:100%}.button{margin:4px 4px 4px 0!important}.card,.hero,.day{padding:16px!important}h1{font-size:1.6rem}h2{font-size:1.25rem}}@media(max-width:600px){.top{display:block!important}.top .button{display:block!important;width:100%!important;margin:8px 0!important;text-align:center}input[type="date"],input[type="time"]{min-width:0!important;width:100%!important;max-width:100%!important;font-size:16px!important}}@media(max-width:600px){form,label{min-width:0!important;max-width:100%!important}input[type="date"],input[type="time"]{min-inline-size:0!important;inline-size:100%!important;max-inline-size:100%!important;overflow:hidden!important}input[type="date"]::-webkit-date-and-time-value,input[type="time"]::-webkit-date-and-time-value{min-width:0!important;text-align:left!important}}</style><link rel="stylesheet" href="/assets/triptale-ui.css?v=19"></head><body><main><a class="button back" href="/trip.php?id=<?= (int)$day['trip_id'] ?>">← Zurück zur Reise</a><h1>Sehenswürdigkeit hinzufügen</h1><p>Tag <?= (int)$day['day_number'] ?> · <?= htmlspecialchars($day['title'], ENT_QUOTES, 'UTF-8') ?></p><?php if ($error): ?><p class="error"><?= htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?></p><?php endif; ?><?php if (!$attractions): ?><p>Es gibt noch keine aktiven Sehenswürdigkeiten. Lege zuerst eine unter „Sehenswürdigkeiten“ an.</p><?php else: ?><form method="post"><input type="hidden" name="csrf_token" value="<?= htmlspecialchars(csrf_token(), ENT_QUOTES, 'UTF-8') ?>"><label>Sehenswürdigkeit<select name="attraction_id" required><option value="">Bitte auswählen …</option><?php foreach ($attractions as $attraction): ?><option value="<?= (int)$attraction['id'] ?>"<?= ((int)($_POST['attraction_id'] ?? 0) === (int)$attraction['id']) ? ' selected' : '' ?>><?= htmlspecialchars($attraction['name'] . ($attraction['city'] ? ' · ' . $attraction['city'] : ''), ENT_QUOTES, 'UTF-8') ?></option><?php endforeach; ?></select></label><label>Uhrzeit (optional)<input name="time" type="time" value="<?= htmlspecialchars((string)($_POST['time'] ?? ''), ENT_QUOTES, 'UTF-8') ?>"></label><button class="save" type="submit">Zum Reiseplan hinzufügen</button></form><?php endif; ?></main><script>document.querySelectorAll("select[name=attraction_id],select[name=entry_id]").forEach(select=>{const search=document.createElement("input");search.type="search";search.placeholder="Namen eingeben …";search.style.marginBottom="4px";select.parentNode.insertBefore(search,select);const help=document.createElement("small");help.textContent="Zum Suchen Namen oder Ort eingeben – anschließend den passenden Eintrag in der Liste auswählen.";help.style.cssText="display:block;margin:0 0 8px;color:#5d6872;font-weight:400";select.parentNode.insertBefore(help,select);search.addEventListener("input",()=>{const term=search.value.toLowerCase();[...select.options].forEach((option,index)=>{if(index){option.hidden=!option.textContent.toLowerCase().includes(term);}});});});</script><script>document.querySelectorAll("select[name=attraction_id],select[name=entry_id]").forEach(select=>{const search=select.parentNode.querySelector("input[type=search]");if(!search)return;const all=[...select.options].map(o=>({value:o.value,text:o.textContent}));search.addEventListener("input",()=>{const selected=select.value;const term=search.value.toLowerCase();select.replaceChildren(...all.filter((o,i)=>!i||o.text.toLowerCase().includes(term)).map(o=>new Option(o.text,o.value,false,o.value===selected)));});});</script></body></html>
