<?php
declare(strict_types=1);
require_once __DIR__ . '/app/Helpers/bootstrap.php';
require_once __DIR__ . '/app/Helpers/countries.php';

$user = require_login();
$tripId = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
if (!$tripId) { http_response_code(404); exit('Reise nicht gefunden.'); }

if ($user['role'] === 'main_admin') {
    $statement = db()->prepare('SELECT * FROM ' . table('trips') . ' WHERE id = :trip_id LIMIT 1');
    $statement->execute([':trip_id' => $tripId]);
} else {
    $statement = db()->prepare('SELECT t.* FROM ' . table('trips') . ' t WHERE t.id = :trip_id AND (t.created_by = :creator_id OR EXISTS (SELECT 1 FROM ' . table('trip_admins') . ' ta WHERE ta.trip_id = t.id AND ta.user_id = :admin_id) OR EXISTS (SELECT 1 FROM ' . table('trip_participants') . ' tp WHERE tp.trip_id = t.id AND tp.user_id = :participant_id)) LIMIT 1');
    $statement->execute([':trip_id' => $tripId, ':creator_id' => $user['id'], ':admin_id' => $user['id'], ':participant_id' => $user['id']]);
}
$trip = $statement->fetch();
if (!$trip) { http_response_code(403); exit('Du hast keinen Zugriff auf diese Reise.'); }

$canEdit = $user['role'] === 'main_admin' || (int)$trip['created_by'] === (int)$user['id'];
if (!$canEdit) {
    $adminCheck = db()->prepare('SELECT 1 FROM ' . table('trip_admins') . ' WHERE trip_id = :trip_id AND user_id = :user_id');
    $adminCheck->execute([':trip_id' => $tripId, ':user_id' => $user['id']]);
    $canEdit = (bool)$adminCheck->fetchColumn();
}
$canManageLifecycle = $user['role'] === 'main_admin' || (int)$trip['created_by'] === (int)$user['id'];

$dayStatement = db()->prepare('SELECT id, day_number, date, title, description FROM ' . table('trip_days') . ' WHERE trip_id = :trip_id ORDER BY day_number');
$dayStatement->execute([':trip_id' => $tripId]);
$days = $dayStatement->fetchAll();
$itemsByDay = [];
if ($days) {
    $dayIds = array_map(static fn(array $day): int => (int)$day['id'], $days);
    $placeholders = implode(',', array_fill(0, count($dayIds), '?'));
    $shortDescriptionField = "''";
    try { $shortColumn = db()->query("SHOW COLUMNS FROM " . table('attractions') . " LIKE 'short_description'"); $shortDescriptionField = $shortColumn->fetch() ? "a.short_description" : "''"; } catch (Throwable $exception) {}
    $itemStatement = db()->prepare('SELECT i.trip_day_id, i.id, i.item_type, i.reference_id, i.title, i.time, i.trip_additional_info, i.image_path AS custom_image, a.name AS attraction_name, ac.name AS accommodation_name, r.name AS restaurant_name, cp.name AS custom_point_name, a.image_path AS attraction_image, ac.image_path AS accommodation_image, r.image_path AS restaurant_image, cp.image_path AS custom_point_image, COALESCE(' . $shortDescriptionField . ', cp.short_description) AS short_description, COALESCE(a.description, ac.description, r.description, cp.description) AS standard_description, COALESCE(a.address,ac.address,r.address,cp.address) AS address, COALESCE(a.city,ac.city,r.city,cp.city) AS city, COALESCE(a.country_code,ac.country_code,r.country_code,cp.country_code) AS country_code, COALESCE(a.google_maps_url,ac.google_maps_url,r.google_maps_url,cp.google_maps_url) AS google_maps_url, COALESCE(a.plus_code,ac.plus_code,r.plus_code,cp.plus_code) AS plus_code, COALESCE(ac.website,r.website,cp.website) AS website, COALESCE(ac.phone,r.phone,cp.phone) AS phone FROM ' . table('trip_day_items') . ' i LEFT JOIN ' . table('attractions') . ' a ON i.item_type = \'attraction\' AND a.id = i.reference_id LEFT JOIN ' . table('accommodations') . ' ac ON i.item_type = \'accommodation\' AND ac.id = i.reference_id LEFT JOIN ' . table('restaurants') . ' r ON i.item_type = \'restaurant\' AND r.id = i.reference_id LEFT JOIN ' . table('custom_points') . ' cp ON i.item_type = \'catalog_custom\' AND cp.id = i.reference_id WHERE i.trip_day_id IN (' . $placeholders . ') ORDER BY i.trip_day_id, i.sort_order, i.id');
    $itemStatement->execute($dayIds);
    $categoryMaps = ['attraction'=>['attraction_categories','attraction_id'], 'accommodation'=>['accommodation_categories','accommodation_id'], 'restaurant'=>['restaurant_categories','restaurant_id'], 'catalog_custom'=>['custom_point_categories','custom_point_id'], 'custom'=>['trip_day_item_categories','trip_day_item_id']];
    foreach ($itemStatement->fetchAll() as $item) {
        $item['categories'] = '';
        try {
            if (isset($categoryMaps[$item['item_type']])) {
                [$mapTable, $itemColumn] = $categoryMaps[$item['item_type']];
                $sourceId = $item['item_type'] === 'custom' ? (int)$item['id'] : (int)$item['reference_id'];
                $q = db()->prepare('SELECT c.name FROM ' . table($mapTable) . ' m JOIN ' . table('categories') . ' c ON c.id=m.category_id WHERE m.' . $itemColumn . '=:id ORDER BY c.name');
                $q->execute([':id'=>$sourceId]);
                $item['categories'] = implode(' · ', array_column($q->fetchAll(), 'name'));
            }
        } catch (Throwable $exception) { $item['categories'] = ''; }
        $itemsByDay[(int)$item['trip_day_id']][] = $item;
    }
}
?>
<!doctype html>
<html lang="<?= htmlspecialchars(language(), ENT_QUOTES, 'UTF-8') ?>">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?= htmlspecialchars($trip['title'], ENT_QUOTES, 'UTF-8') ?> · TripTale</title>
<style>
body{font-family:system-ui,sans-serif;background:#f4f6f8;margin:0;color:#17202a}header{padding:18px 7%;background:#126e82;color:white}main{max-width:960px;margin:36px auto;padding:0 24px}.hero,.day{background:white;border-radius:14px;padding:24px;margin-bottom:14px;box-shadow:0 4px 18px #0001}.date,.muted{color:#59636d;font-weight:600}.hero>.date{font-size:1.06rem;font-weight:750;color:#33414a}.day h2{margin:4px 0}a{color:#126e82}.button{display:inline-block;background:#126e82;color:white;padding:10px 14px;border-radius:7px;text-decoration:none;font-weight:700;margin-right:8px}.small{padding:7px 10px;font-size:.9rem}.button.back{margin-bottom:22px}.items{margin-top:18px;padding-top:14px;border-top:1px solid #dbe1e5}.items h3{margin:0 0 10px}.item{padding:10px 0;border-top:1px solid #edf0f2}.item:first-of-type{border-top:0}.time{font-weight:700;color:#126e82}.item-details{margin-top:10px;color:#33414a}.item-short-detail{margin:10px 0;font-weight:600;color:#33414a}.item-categories{margin:14px 0;padding:9px 11px;border-top:1px solid #dbe1e5;border-bottom:1px solid #dbe1e5;background:#f8fafb;color:#59636d;font-size:1rem}.item-note{margin-top:10px;padding:10px;background:#eef6f7;border-left:3px solid #75aeb9;border-radius:5px}.meta{margin-top:8px;font-size:1rem;color:#59636d}.meta a{font-weight:700}.item-image{display:block;width:100%;max-height:240px;object-fit:contain;background:#edf2f3;border-radius:9px;margin:12px 0}
*{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}.button.back{margin-bottom:22px!important}}.card,.hero,.day,.choice{border:1px solid #d6e0e4!important;box-shadow:0 3px 12px rgba(23,32,42,.08)!important}.card{border-left:4px solid #75aeb9!important}.card:hover{transform:translateY(-1px);box-shadow:0 7px 20px rgba(23,32,42,.13)!important}.day{border-left:4px solid #75aeb9!important}.choice{border-left:4px solid #75aeb9!important}.item{background:#f8fafb;border:1px solid #dfe7ea!important;border-radius:9px;padding:14px!important;margin-top:10px}.item:first-of-type{border-top:1px solid #dfe7ea!important}.trip-cover{display:block;width:auto;height:auto;max-width:100%;max-height:300px;object-fit:initial;background:transparent;border-radius:14px;clip-path:inset(0 round 14px);margin:0 auto 16px}.item{padding:0!important;overflow:hidden}.item-toggle{width:100%;display:grid;grid-template-columns:88px minmax(0,1fr) 28px;gap:12px;align-items:center;padding:12px;border:0;background:#f8fafb;color:#17202a;text-align:left;font:inherit;cursor:pointer}.item-toggle:hover{background:#eef4f5}.item-thumb{width:88px;height:64px;object-fit:contain;background:#e8eef0;border-radius:7px}.item-thumb.empty{display:block}.item-summary{display:block;color:#59636d;font-size:.9rem;margin-top:3px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.item-chevron{font-size:1.25rem;color:#126e82;text-align:center}.item-panel{display:none;grid-template-columns:minmax(240px,42%) 1fr;gap:18px;padding:16px;background:#fff;border-top:1px solid #dbe4e8}.day.open .item.open{background:#dfecef!important;border-color:#a7c9cf!important}.day.open .item.open .item-toggle{background:#dfecef}.day.open .item.open .item-panel{background:#e7f1f3}.item.open .item-panel{display:grid}.item.open .item-chevron{transform:rotate(180deg)}.item-panel .item-image{margin:0;max-height:340px}.item-content{min-width:0}.item-content .item-image{display:none}@media(max-width:600px){.item-toggle{grid-template-columns:70px minmax(0,1fr) 22px;padding:10px}.item-thumb{width:70px;height:58px}.item-panel{grid-template-columns:1fr;padding:12px}.item-panel .item-image{max-height:260px}.item-summary{white-space:normal;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical}}.day{padding:0!important;overflow:hidden}.day-toggle{width:100%;display:flex;justify-content:space-between;align-items:center;gap:12px;padding:16px;border:0;background:#fff;color:#17202a;text-align:left;font:inherit;cursor:pointer}.day-toggle:hover{background:#f4f8f9}.day-title{display:block;font-size:1.1rem;font-weight:700}.day-count{color:#59636d;font-size:.9rem}.day-panel{display:none;padding:0 16px 16px}.day.open{background:#edf5f6!important;border-color:#bad5da!important}.day.open .day-toggle{background:#edf5f6}.day.open .day-panel{display:block}.day-chevron{font-size:1.4rem;color:#126e82}.day.open .day-chevron{transform:rotate(180deg)}.day-panel>.date,.day-panel>h2,.item-content>strong:first-child{display:none}.trip-note-actions{display:flex;align-items:center;gap:8px;margin:14px 0}.trip-note-actions .button{margin:0}.notes-menu{display:block;position:relative;margin:0}.notes-menu summary{list-style:none;cursor:pointer;display:inline-block;background:#126e82;color:#fff;padding:10px 14px;border-radius:7px;font-weight:700}.notes-menu summary::-webkit-details-marker{display:none}.notes-links{position:absolute;right:0;z-index:10;min-width:210px;margin-top:6px;padding:8px;background:#fff;border:1px solid #d6e0e4;border-radius:10px;box-shadow:0 6px 20px #0002}.notes-links a{display:block;padding:10px 12px;color:#17202a!important;text-decoration:none!important;border-radius:6px;font-weight:700}.notes-links a:hover{background:#eaf3f4;color:#126e82!important}@media(max-width:600px){.trip-note-actions{display:block}.trip-note-actions .button,.trip-note-actions .notes-menu{display:block;width:100%;margin:8px 0}.notes-menu summary{display:block;text-align:left}}.trip-management{margin:22px 0}.trip-management form{display:block;margin:10px 0 0}.trip-management button{padding:12px 16px;border:0;border-radius:7px;background:#126e82;color:#fff;font:inherit;font-weight:700;cursor:pointer}.trip-management button.delete-trip{background:#9d2d2d}</style><link rel="stylesheet" href="/assets/triptale-ui.css?v=19"><style>.hero img.trip-cover{width:auto!important;height:auto!important;max-width:100%!important;max-height:300px!important;object-fit:initial!important;background:transparent!important;border-radius:14px!important;clip-path:inset(0 round 14px)!important;overflow:hidden!important}</style>
<style>.calendar-icon{width:42px!important;height:38px!important;padding:7px!important;vertical-align:middle!important;display:inline-flex!important;align-items:center!important;justify-content:center!important}.calendar-icon svg{width:20px;height:20px;display:block}.item-panel p,.day-panel p{display:flex;flex-wrap:wrap;gap:8px;align-items:center}.item-panel p .button,.day-panel p .button{margin:0!important}@media(max-width:600px){.item-panel p,.day-panel p{display:flex!important;align-items:flex-start}.calendar-icon{width:42px!important;flex:0 0 42px!important}}</style>
<style>
.item{border-left:5px solid #91bf98!important}.item[data-type="attraction"]{border-left-color:#e4a06f!important}.item[data-type="accommodation"]{border-left-color:#91c2d8!important}.item[data-type="restaurant"]{border-left-color:#b9a2cd!important}.item[data-type="custom"],.item[data-type="catalog_custom"]{border-left-color:#91bf98!important}.day.open .item.open[data-type="attraction"]{border-left-color:#e4a06f!important}.day.open .item.open[data-type="accommodation"]{border-left-color:#91c2d8!important}.day.open .item.open[data-type="restaurant"]{border-left-color:#b9a2cd!important}.day.open .item.open[data-type="custom"],.day.open .item.open[data-type="catalog_custom"]{border-left-color:#91bf98!important}.item-title-line{display:flex;align-items:center;gap:8px;flex-wrap:wrap}.item-type-label{display:inline-block;padding:3px 7px;border-radius:999px;font-size:.74rem;line-height:1.2;font-weight:400;white-space:nowrap}.item-type-label.attraction{background:#f5ddce;color:#8b421a;border:1px solid #8b421a}.item-type-label.accommodation{background:#d7eaf2;color:#25637e;border:1px solid #25637e}.item-type-label.restaurant{background:#e8dfee;color:#5d3d72;border:1px solid #5d3d72}.item-type-label.custom,.item-type-label.catalog_custom{background:#deede0;color:#35673e;border:1px solid #35673e}@media(max-width:600px){.item-title-line{gap:6px}.item-type-label{font-size:.7rem;padding:3px 6px}}
</style>
</head>
<body>
<header><strong>TripTale</strong></header>
<main>
<a class="button back" href="/dashboard.php"><?= htmlspecialchars(t('trip.back'), ENT_QUOTES, 'UTF-8') ?></a>
<section class="hero">
<?php if (!empty($trip['image_path'])): ?><img class="trip-cover" src="/<?= htmlspecialchars($trip['image_path'],ENT_QUOTES,'UTF-8') ?>" alt=""><?php endif; ?><h1><?= htmlspecialchars($trip['title'], ENT_QUOTES, 'UTF-8') ?></h1>
<?php if ($trip['subtitle']): ?><p><?= htmlspecialchars($trip['subtitle'], ENT_QUOTES, 'UTF-8') ?></p><?php endif; ?>
<p class="date"><?= htmlspecialchars(localized_date($trip['start_date']), ENT_QUOTES, 'UTF-8') ?> – <?= htmlspecialchars(localized_date($trip['end_date']), ENT_QUOTES, 'UTF-8') ?></p>
<?php if ($trip['description']): ?><div><?= render_rich_text($trip['description']) ?></div><?php endif; ?>
<div class="trip-note-actions"><?php if (!empty($trip['group_chat_url'])): ?><a class="button" href="<?= htmlspecialchars($trip['group_chat_url'], ENT_QUOTES, 'UTF-8') ?>" target="_blank" rel="noopener noreferrer"><?= htmlspecialchars(t('trip.group'), ENT_QUOTES, 'UTF-8') ?></a><?php endif; ?><details class="notes-menu"><summary><?= htmlspecialchars(t('trip.comments_all_menu'), ENT_QUOTES, 'UTF-8') ?></summary><nav class="notes-links"><a href="/trip-comments.php?id=<?= (int)$trip['id'] ?>"><?= htmlspecialchars(t('trip.all_comments'), ENT_QUOTES, 'UTF-8') ?></a><a href="/trip-comments.php?id=<?= (int)$trip['id'] ?>&view=trip"><?= htmlspecialchars(t('trip.to_trip'), ENT_QUOTES, 'UTF-8') ?></a><a href="/trip-comments.php?id=<?= (int)$trip['id'] ?>&view=days"><?= htmlspecialchars(t('trip.to_days'), ENT_QUOTES, 'UTF-8') ?></a><a href="/trip-comments.php?id=<?= (int)$trip['id'] ?>&view=items"><?= htmlspecialchars(t('trip.to_items'), ENT_QUOTES, 'UTF-8') ?></a></nav></details><details class="notes-menu"><summary><?= htmlspecialchars(t('trip.notes_private_menu'), ENT_QUOTES, 'UTF-8') ?></summary><nav class="notes-links"><a href="/trip-notes.php?id=<?= (int)$trip['id'] ?>"><?= htmlspecialchars(t('trip.all_notes'), ENT_QUOTES, 'UTF-8') ?></a><a href="/trip-notes.php?id=<?= (int)$trip['id'] ?>&view=trip"><?= htmlspecialchars(t('trip.to_trip'), ENT_QUOTES, 'UTF-8') ?></a><a href="/trip-notes.php?id=<?= (int)$trip['id'] ?>&view=days"><?= htmlspecialchars(t('trip.to_days'), ENT_QUOTES, 'UTF-8') ?></a><a href="/trip-notes.php?id=<?= (int)$trip['id'] ?>&view=items"><?= htmlspecialchars(t('trip.to_items'), ENT_QUOTES, 'UTF-8') ?></a></nav></details></div>
<?php if ($canEdit && $trip["status"] !== "archived"): ?><p><a class="button small" href="/trip-edit.php?id=<?= (int)$trip['id'] ?>"><?= htmlspecialchars(t('trip.edit'), ENT_QUOTES, 'UTF-8') ?></a><a class="button small" href="/trip-users.php?id=<?= (int)$trip['id'] ?>"><?= htmlspecialchars(t('trip.manage_group'), ENT_QUOTES, 'UTF-8') ?></a></p><?php elseif ($canManageLifecycle): ?><section class="trip-management"><strong><?= htmlspecialchars(t('trip.manage'), ENT_QUOTES, 'UTF-8') ?></strong><form method="post" action="/trip-manage.php"><input type="hidden" name="csrf_token" value="<?= htmlspecialchars(csrf_token(),ENT_QUOTES,'UTF-8') ?>"><input type="hidden" name="trip_id" value="<?= (int)$trip['id'] ?>"><input type="hidden" name="action" value="restore"><button type="submit"><?= htmlspecialchars(t('trip.reactivate'), ENT_QUOTES, 'UTF-8') ?></button></form><form method="post" action="/trip-manage.php" onsubmit='return confirm(<?= htmlspecialchars(tjs('trip.confirm_delete'), ENT_QUOTES, 'UTF-8') ?>);'><input type="hidden" name="csrf_token" value="<?= htmlspecialchars(csrf_token(),ENT_QUOTES,'UTF-8') ?>"><input type="hidden" name="trip_id" value="<?= (int)$trip['id'] ?>"><input type="hidden" name="action" value="delete"><button class="delete-trip" type="submit"><?= htmlspecialchars(t('trip.delete'), ENT_QUOTES, 'UTF-8') ?></button></form></section><?php endif; ?>
</section>
<h2><?= htmlspecialchars(t('trip.itinerary'), ENT_QUOTES, 'UTF-8') ?></h2>
<?php foreach ($days as $day): ?>
<section class="day">
<p class="date"><?= htmlspecialchars(localized_date($day['date'], true), ENT_QUOTES, 'UTF-8') ?></p>
<h2><?= htmlspecialchars(t('trip.day'), ENT_QUOTES, 'UTF-8') ?> <?= (int)$day['day_number'] ?> · <?= htmlspecialchars($day['title'], ENT_QUOTES, 'UTF-8') ?></h2>
<?php if ($day['description']): ?><div><?= render_rich_text($day['description']) ?></div><?php else: ?><p class="muted"><?= htmlspecialchars(t('trip.no_day_description'), ENT_QUOTES, 'UTF-8') ?></p><?php endif; ?>
<p><a class="button small" href="/trip-notes.php?id=<?= (int)$trip['id'] ?>&day=<?= (int)$day['id'] ?>"><?= htmlspecialchars(t('trip.notes_day'), ENT_QUOTES, 'UTF-8') ?></a><a class="button small" href="/trip-comments.php?id=<?= (int)$trip['id'] ?>&day=<?= (int)$day['id'] ?>"><?= htmlspecialchars(t('trip.comments_day'), ENT_QUOTES, 'UTF-8') ?></a><a class="button small calendar-icon" href="/calendar-export.php?type=day&id=<?= (int)$day['id'] ?>" aria-label="<?= htmlspecialchars(t('trip.calendar_day'), ENT_QUOTES, 'UTF-8') ?>" title="<?= htmlspecialchars(t('trip.calendar_day'), ENT_QUOTES, 'UTF-8') ?>"><svg viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="5" width="18" height="16" rx="2"></rect><path d="M16 3v4M8 3v4M3 10h18M8 14h3M8 17h6"></path></svg></a></p>
<?php if (!empty($itemsByDay[(int)$day['id']])): ?>
<div class="items"><h3><?= htmlspecialchars(t('trip.items'), ENT_QUOTES, 'UTF-8') ?></h3>
<?php foreach ($itemsByDay[(int)$day['id']] as $item): ?>
<span hidden class="trip-item-type" data-trip-item-type="<?= htmlspecialchars($item['item_type'], ENT_QUOTES, 'UTF-8') ?>"></span>
<div class="item" data-short="<?= htmlspecialchars((string)($item['short_description'] ?? ''), ENT_QUOTES, 'UTF-8') ?>">
<?php $itemImage=$item['custom_image']?:$item['attraction_image']?:$item['accommodation_image']?:$item['restaurant_image']?:$item['custom_point_image']?:null; ?>
<?php if($itemImage): ?><img class="item-image" src="/<?= htmlspecialchars($itemImage,ENT_QUOTES,'UTF-8') ?>" alt=""><?php endif; ?>
<strong><?php if ($item['time']): ?><span class="time"><?= htmlspecialchars(substr($item['time'], 0, 5), ENT_QUOTES, 'UTF-8') ?> – </span><?php endif; ?><?= htmlspecialchars($item['title'] ?: $item['attraction_name'] ?: $item['accommodation_name'] ?: $item['restaurant_name'] ?: $item['custom_point_name'] ?: t('type.custom'), ENT_QUOTES, 'UTF-8') ?></strong>
<?php if ($item['short_description']): ?><div class="item-short-detail"><?= htmlspecialchars((string)$item['short_description'], ENT_QUOTES, 'UTF-8') ?></div><?php endif; ?>
<?php if ($item['item_type'] === 'accommodation'): ?><span class="muted">· <?= htmlspecialchars(t('type.accommodation'), ENT_QUOTES, 'UTF-8') ?></span><?php elseif ($item['item_type'] === 'restaurant'): ?><span class="muted">· <?= htmlspecialchars(t('type.restaurant'), ENT_QUOTES, 'UTF-8') ?></span><?php elseif ($item['item_type'] === 'catalog_custom'): ?><span class="muted">· <?= htmlspecialchars(t('type.custom'), ENT_QUOTES, 'UTF-8') ?></span><?php endif; ?>
<?php if ($item['standard_description']): ?><div class="item-details"><?= render_rich_text($item['standard_description']) ?></div><?php endif; ?>
<?php if ($item['categories']): ?><div class="item-categories"><strong><?= htmlspecialchars(t('trip.categories'), ENT_QUOTES, 'UTF-8') ?>:</strong> <?= htmlspecialchars($item['categories'], ENT_QUOTES, 'UTF-8') ?></div><?php endif; ?>
<?php if ($item['address'] || $item['city'] || $item['plus_code'] || $item['website'] || $item['phone']): ?><div class="meta"><?php if ($item['address']): ?><?= htmlspecialchars($item['address'],ENT_QUOTES,'UTF-8') ?><?php endif; ?><?php if ($item['city']): ?><?= $item['address'] ? ' · ' : '' ?><?= htmlspecialchars($item['city'],ENT_QUOTES,'UTF-8') ?><?php endif; ?><?php if ($item['country_code']): ?><?= ($item['address'] || $item['city']) ? ' · ' : '' ?><?= htmlspecialchars(triptale_countries()[$item['country_code']] ?? $item['country_code'],ENT_QUOTES,'UTF-8') ?><?php endif; ?><?php if ($item['plus_code']): ?><?= ($item['address'] || $item['city'] || $item['country_code']) ? ' · ' : '' ?><?= htmlspecialchars(t('trip.plus_code'), ENT_QUOTES, 'UTF-8') ?>: <?= htmlspecialchars($item['plus_code'],ENT_QUOTES,'UTF-8') ?><?php endif; ?><?php if ($item['website']): ?> · <a href="<?= htmlspecialchars($item['website'],ENT_QUOTES,'UTF-8') ?>" target="_blank" rel="noopener"><?= htmlspecialchars(t('trip.website'), ENT_QUOTES, 'UTF-8') ?></a><?php endif; ?><?php if ($item['phone']): ?> · <a href="tel:<?= htmlspecialchars($item['phone'],ENT_QUOTES,'UTF-8') ?>"><?= htmlspecialchars($item['phone'],ENT_QUOTES,'UTF-8') ?></a><?php endif; ?></div><?php endif; ?>
<?php if ($item['google_maps_url']): ?><p><a class="button small" href="<?= htmlspecialchars($item['google_maps_url'],ENT_QUOTES,'UTF-8') ?>" target="_blank" rel="noopener"><?= htmlspecialchars(t('trip.open_maps'), ENT_QUOTES, 'UTF-8') ?></a></p><?php endif; ?>
<?php if ($item['trip_additional_info']): ?><div class="item-note"><strong><?= htmlspecialchars(t('trip.note_for_trip'), ENT_QUOTES, 'UTF-8') ?></strong><br><?= render_rich_text($item['trip_additional_info']) ?></div><?php endif; ?>
<p><a class="button small" href="/trip-notes.php?id=<?= (int)$trip['id'] ?>&item=<?= (int)$item['id'] ?>"><?= htmlspecialchars(t('trip.note_item'), ENT_QUOTES, 'UTF-8') ?></a><a class="button small" href="/trip-comments.php?id=<?= (int)$trip['id'] ?>&item=<?= (int)$item['id'] ?>"><?= htmlspecialchars(t('trip.comments'), ENT_QUOTES, 'UTF-8') ?></a></p>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if ($canEdit && $trip["status"] !== "archived"): ?><p><a class="button small" href="/day-edit.php?id=<?= (int)$day['id'] ?>"><?= htmlspecialchars(t('trip.edit_day'), ENT_QUOTES, 'UTF-8') ?></a><a class="button small" href="/day-program.php?id=<?= (int)$day['id'] ?>"><?= htmlspecialchars(t('trip.edit_programme'), ENT_QUOTES, 'UTF-8') ?></a></p><?php endif; ?>
</section>
<?php endforeach; ?>
</main>
<script>document.querySelectorAll(".items .item").forEach(item=>{const source=[...item.childNodes];const originalImage=item.querySelector(".item-image");const title=item.querySelector("strong");const header=document.createElement("button");header.type="button";header.className="item-toggle";const thumb=originalImage?originalImage.cloneNode(true):document.createElement("span");thumb.classList.add("item-thumb");if(!originalImage){thumb.classList.add("empty");}else{thumb.classList.remove("item-image");}const text=document.createElement("span");const titleCopy=document.createElement("strong");titleCopy.textContent=title?title.textContent:<?= tjs('type.custom') ?>;const summary=document.createElement("span");summary.className="item-summary";const short=item.dataset.short||"";summary.textContent=short?(short.length>50?short.slice(0,50).trimEnd()+"…":short):<?= tjs('trip.show_details') ?>;text.append(titleCopy,summary);const chevron=document.createElement("span");chevron.className="item-chevron";chevron.textContent="⌄";header.append(thumb,text,chevron);const panel=document.createElement("div");panel.className="item-panel";source.forEach(node=>panel.append(node));const fullImage=panel.querySelector(".item-image");const content=document.createElement("div");content.className="item-content";[...panel.childNodes].forEach(node=>{if(node!==fullImage)content.append(node);});panel.replaceChildren();if(fullImage)panel.append(fullImage);panel.append(content);item.replaceChildren(header,panel);header.addEventListener("click",()=>{const wasOpen=item.classList.contains("open");document.querySelectorAll(".items .item.open").forEach(open=>open.classList.remove("open"));if(!wasOpen)item.classList.add("open");});});</script><script>const days=[...document.querySelectorAll("main > .day")],itemSingular=<?= tjs('trip.item_singular') ?>,itemPlural=<?= tjs('trip.item_plural') ?>;days.forEach((day,index)=>{const content=[...day.childNodes];const date=day.querySelector(".date");const heading=day.querySelector("h2");const count=day.querySelectorAll(".items .item").length;const toggle=document.createElement("button");toggle.type="button";toggle.className="day-toggle";const text=document.createElement("span");text.innerHTML="<span class=\"day-title\">"+(heading?heading.textContent:<?= tjs('trip.day') ?>)+"</span><span class=\"day-count\">"+(date?date.textContent+" · ":"")+count+" "+(count===1?itemSingular:itemPlural)+"</span>";const chevron=document.createElement("span");chevron.className="day-chevron";chevron.textContent="⌄";toggle.append(text,chevron);const panel=document.createElement("div");panel.className="day-panel";content.forEach(node=>panel.append(node));day.replaceChildren(toggle,panel);if(index===0)day.classList.add("open");toggle.addEventListener("click",()=>{const open=day.classList.contains("open");days.forEach(other=>other.classList.remove("open"));if(!open)day.classList.add("open");});});</script></body>
<script>document.querySelectorAll('main > .day.open').forEach(function(day){day.classList.remove('open');});</script>
<script>document.querySelectorAll('a[href*="trip-notes.php"][href*="&item="]').forEach(function(noteLink){noteLink.textContent=<?= tjs('trip.note_item') ?>;var commentsLink=noteLink.parentElement.querySelector('a[href*="trip-comments.php"]');if(commentsLink)commentsLink.textContent=<?= tjs('trip.comments') ?>;var itemId=new URL(noteLink.href).searchParams.get('item');if(!itemId||noteLink.parentElement.querySelector('[data-calendar-item]'))return;var calendarLink=document.createElement('a');calendarLink.className='button small calendar-icon';calendarLink.dataset.calendarItem='1';calendarLink.href='/calendar-export.php?type=item&id='+encodeURIComponent(itemId);calendarLink.setAttribute('aria-label',<?= tjs('trip.calendar_item') ?>);calendarLink.title=<?= tjs('trip.calendar_item') ?>;calendarLink.innerHTML='<svg viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="5" width="18" height="16" rx="2"></rect><path d="M16 3v4M8 3v4M3 10h18M8 14h3M8 17h6"></path></svg>';noteLink.parentElement.append(calendarLink);});</script>
</html>
<script>(function(){var labels=<?= json_encode(['attraction'=>t('type.attraction'),'accommodation'=>t('type.accommodation'),'restaurant'=>t('type.restaurant'),'custom'=>t('type.custom'),'catalog_custom'=>t('type.custom')], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?>;document.querySelectorAll('.items .item').forEach(function(item){var source=item.previousElementSibling;var type=source&&source.dataset.tripItemType||'custom';item.dataset.type=type;var title=item.querySelector('.item-toggle strong');if(title&&labels[type]&&!title.parentElement.querySelector('.item-type-label')){var line=document.createElement('span');line.className='item-title-line';title.parentNode.insertBefore(line,title);line.appendChild(title);var label=document.createElement('span');label.className='item-type-label '+type;label.textContent=labels[type];line.appendChild(label);}item.querySelectorAll('.item-content .muted').forEach(function(note){if(/^·/.test(note.textContent.trim()))note.remove();});});})();</script>
<?php if ($canEdit): ?><script>(function(){var edit=document.querySelector('a[href^="/trip-edit.php"]');if(!edit||document.getElementById('trip-changes-link'))return;var id=new URL(location.href).searchParams.get('id');if(!id)return;var link=document.createElement('a');link.id='trip-changes-link';link.className='button small';link.href='/trip-changes.php?id='+encodeURIComponent(id);link.textContent=<?= tjs('trip.changes') ?>;edit.parentElement.append(link);})();</script><?php endif; ?>
<script>(function(){var actions=document.querySelector('.trip-note-actions');if(!actions||document.getElementById('trip-checklist-link'))return;var id=new URL(location.href).searchParams.get('id');if(!id)return;var link=document.createElement('a');link.id='trip-checklist-link';link.className='button';link.href='/trip-checklist.php?id='+encodeURIComponent(id);link.textContent=<?= tjs('trip.checklist') ?>;actions.append(link);})();</script>
<script>(function(){var actions=document.querySelector('.trip-note-actions');if(!actions||document.getElementById('trip-pdf-link'))return;var id=new URL(location.href).searchParams.get('id');if(!id)return;var link=document.createElement('a');link.id='trip-pdf-link';link.className='button';link.href='/trip-pdf.php?id='+encodeURIComponent(id);link.textContent=<?= tjs('trip.pdf') ?>;actions.append(link);})();</script>
