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

$user = require_login();
$canManage = in_array($user['role'], ['main_admin', 'admin'], true);
try {
    if ($user['role'] === 'main_admin') {
        $statement = db()->query('SELECT * FROM ' . table('trips') . ' ORDER BY start_date DESC,id DESC');
    } else {
        $statement = db()->prepare('SELECT t.* FROM ' . table('trips') . ' t WHERE 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) ORDER BY t.start_date DESC,t.id DESC');
        $statement->execute([':creator_id' => $user['id'], ':admin_id' => $user['id'], ':participant_id' => $user['id']]);
    }
    $trips = $statement->fetchAll();
} catch (Throwable $exception) {
    http_response_code(500);
    if (isset($_GET['debug'])) { exit('Dashboard-Fehler: ' . htmlspecialchars($exception->getMessage(), ENT_QUOTES, 'UTF-8')); }
    exit('Das Dashboard konnte nicht geladen werden.');
}
$activeTrips = [];
$archivedTrips = [];
foreach ($trips as $trip) {
    if ($trip['status'] === 'archived') { $archivedTrips[] = $trip; } else { $activeTrips[] = $trip; }
}
function dashboard_trip_card(array $trip): void { ?>
<a class="card" href="/trip.php?id=<?= (int)$trip['id'] ?>">
<?php $imagePath = (string)($trip['image_path'] ?? $trip['cover_image'] ?? ''); ?>
<?php if ($imagePath): ?><img class="thumb" src="/<?= htmlspecialchars($imagePath, ENT_QUOTES, 'UTF-8') ?>" alt=""><?php endif; ?>
<h2><?= htmlspecialchars($trip['title'], ENT_QUOTES, 'UTF-8') ?></h2>
<?php if ($trip['subtitle']): ?><p><?= htmlspecialchars($trip['subtitle'], ENT_QUOTES, 'UTF-8') ?></p><?php endif; ?>
<p class="muted date"><?= htmlspecialchars(localized_date($trip['start_date']), ENT_QUOTES, 'UTF-8') ?> – <?= htmlspecialchars(localized_date($trip['end_date']), ENT_QUOTES, 'UTF-8') ?> · <?= htmlspecialchars(t('status.' . $trip['status']), ENT_QUOTES, 'UTF-8') ?></p>
</a>
<?php }
?>
<!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>TripTale</title>
<style>
body{font-family:system-ui,sans-serif;background:#f4f6f8;margin:0;color:#17202a}header{padding:18px 7%;background:#126e82;color:#fff;display:flex;justify-content:space-between;align-items:center}header a{color:#fff}main{max-width:960px;margin:40px auto;padding:0 24px}.top{display:flex;justify-content:space-between;align-items:center;gap:16px;margin-bottom:20px}.card{display:block;background:#fff;border-radius:14px;padding:22px;margin:12px 0;border:1px solid #d6e0e4;border-left:4px solid #75aeb9;box-shadow:0 3px 12px #0001;color:#17202a;text-decoration:none}.card:hover{transform:translateY(-1px);box-shadow:0 7px 20px #0002}.muted{color:#59636d}.date{font-size:1.05rem;font-weight:700;color:#33414a}.button{display:inline-block;background:#126e82;color:#fff;padding:11px 15px;border-radius:7px;text-decoration:none;font-weight:700;margin:4px}.thumb{width:120px;height:82px;object-fit:contain;background:#e8eef0;border-radius:8px;float:left;margin:0 14px 8px 0}.archive{margin-top:34px;padding-top:8px;border-top:1px solid #ccd8dc}.archive summary{cursor:pointer;font-size:1.2rem;font-weight:700;color:#126e82;padding:14px 0}.archive-note{margin:0 0 12px;color:#59636d}.resource-menu{display:inline-block;position:relative;margin:4px}.resource-menu summary{list-style:none;cursor:pointer;display:inline-block;background:#126e82;color:#fff;padding:11px 15px;border-radius:7px;font-weight:700}.resource-menu summary::-webkit-details-marker{display:none}.resource-menu summary::after{content:'⌄';margin-left:8px}.resource-menu[open] summary::after{content:'⌃'}.resource-links{position:absolute;right:0;z-index:5;min-width:250px;margin-top:6px;padding:8px;background:#fff;border:1px solid #d6e0e4;border-radius:10px;box-shadow:0 6px 20px #0002}.resource-links a{display:block;padding:10px 12px;color:#17202a;text-decoration:none;border-radius:6px;font-weight:650}.resource-links a:hover{background:#eaf3f4;color:#126e82}@media(max-width:600px){body{padding:12px}header{padding:14px 16px;flex-wrap:wrap;gap:10px}main{width:100%;margin:18px auto;padding:16px}.top{display:block}.top>div{width:100%}.button{display:block;width:100%;text-align:center;margin:8px 0}.resource-menu{display:block;width:100%;margin:8px 0}.resource-menu summary{display:block;width:100%;text-align:center}.resource-links{position:static;width:100%;margin-top:5px;box-shadow:none}.resource-links a{text-align:center;padding:12px}.card{padding:16px}h1{font-size:1.6rem}}*{box-sizing:border-box}html,body{max-width:100%;overflow-x:hidden}
</style><link rel="stylesheet" href="/assets/triptale-ui.css?v=19">
</head>
<body>
<header><strong>TripTale</strong></header>
<main>
<div class="top"><div><h1><?= htmlspecialchars(t('dashboard.title'), ENT_QUOTES, 'UTF-8') ?></h1><p class="muted"><?= htmlspecialchars(t('dashboard.welcome'), ENT_QUOTES, 'UTF-8') ?> <?= htmlspecialchars($user['first_name'], ENT_QUOTES, 'UTF-8') ?>.</p></div>
<?php if ($canManage): ?><div>
<a class="button" href="/trip-create.php"><?= htmlspecialchars(t('dashboard.new_trip'), ENT_QUOTES, 'UTF-8') ?></a>
<details class="resource-menu"><summary><?= htmlspecialchars(t('dashboard.places_program'), ENT_QUOTES, 'UTF-8') ?></summary><nav class="resource-links"><a href="/attractions.php"><?= htmlspecialchars(t('dashboard.attractions'), ENT_QUOTES, 'UTF-8') ?></a><a href="/catalog.php?type=accommodation"><?= htmlspecialchars(t('dashboard.accommodations'), ENT_QUOTES, 'UTF-8') ?></a><a href="/catalog.php?type=restaurant"><?= htmlspecialchars(t('dashboard.food_drink'), ENT_QUOTES, 'UTF-8') ?></a><a href="/custom-points.php"><?= htmlspecialchars(t('dashboard.custom_points'), ENT_QUOTES, 'UTF-8') ?></a></nav></details>
<a class="button" href="/categories.php"><?= htmlspecialchars(t('dashboard.categories'), ENT_QUOTES, 'UTF-8') ?></a>
<?php if ($user['role'] === 'main_admin'): ?><a class="button" href="/users.php"><?= htmlspecialchars(t('dashboard.users'), ENT_QUOTES, 'UTF-8') ?></a><?php endif; ?>
</div><?php endif; ?></div>
<?php if (!$activeTrips): ?><section class="card"><h2><?= htmlspecialchars(t('dashboard.no_active'), ENT_QUOTES, 'UTF-8') ?></h2><p class="muted"><?= htmlspecialchars(t('dashboard.no_active_hint'), ENT_QUOTES, 'UTF-8') ?></p></section><?php endif; ?>
<?php foreach ($activeTrips as $trip): dashboard_trip_card($trip); endforeach; ?>
<?php if ($archivedTrips): ?><details class="archive"><summary><?= htmlspecialchars(t('dashboard.archived'), ENT_QUOTES, 'UTF-8') ?> (<?= count($archivedTrips) ?>)</summary><p class="archive-note"><?= htmlspecialchars(t('dashboard.archived_hint'), ENT_QUOTES, 'UTF-8') ?></p><?php foreach ($archivedTrips as $trip): dashboard_trip_card($trip); endforeach; ?></details><?php endif; ?>
</main>
</body>
</html>
