<?php
declare(strict_types=1);
require_once __DIR__ . '/app/Helpers/bootstrap.php';
require_once __DIR__ . '/app/Helpers/countries.php';
ob_start(static function (string $html): string {
    return strtr($html, [
        '<html lang="de">' => '<html lang="en">', 'Sehenswürdigkeiten · TripTale' => t('attraction.title') . ' · TripTale',
        '>← Dashboard<' => '>' . t('attraction.back') . '<', '>Sehenswürdigkeiten<' => '>' . t('attraction.title') . '<',
        '>+ Neue Sehenswürdigkeit<' => '>' . t('attraction.new') . '<', '>Suche<input' => '>' . t('attraction.search') . '<input',
        'placeholder="Name oder Ort eingeben …"' => 'placeholder="' . htmlspecialchars(t('attraction.search_hint'), ENT_QUOTES, 'UTF-8') . '"',
        '>Kategorie<select' => '>' . t('category.filter') . '<select', '>Alle Kategorien<' => '>' . t('category.all') . '<',
        '>Land<select' => '>' . t('attraction.country') . '<select', '>Alle Länder<' => '>' . t('attraction.all_countries') . '<',
        '>Noch keine Sehenswürdigkeiten<' => '>' . t('attraction.none_title') . '<',
        '>Lege einen ersten wiederverwendbaren Eintrag an.<' => '>' . t('attraction.none_hint') . '<',
        '>Kategorien: ' => '>' . t('attraction.categories') . ' ', '>Aktiv<' => '>' . t('attraction.active') . '<',
        '>Deaktiviert<' => '>' . t('attraction.inactive') . '<', '>Bearbeiten<' => '>' . t('common.edit') . '<',
        '>Löschen<' => '>' . t('attraction.delete') . '<',
        "confirm(\"Diese Sehenswürdigkeit inklusive aller Einträge in Reiseplänen, Hinweise und Bilder wirklich endgültig löschen?\")" => 'confirm(' . tjs('attraction.confirm_delete') . ')',
        '>Die Sehenswürdigkeit wurde gelöscht.<' => '>' . t('attraction.deleted') . '<',
    ]);
});
ob_start(static function (string $html): string {
    $notice = isset($_GET['saved']) ? t('attraction.saved') : (isset($_GET['deleted']) ? t('attraction.deleted') : '');
    return $notice === '' ? $html : str_replace('</div><form class="filters"', '</div><p class="success">' . htmlspecialchars($notice, ENT_QUOTES, 'UTF-8') . '</p><form class="filters"', $html);
});
$user = require_login();
if (!in_array($user['role'], ['main_admin', 'admin'], true)) { http_response_code(403); exit(t('image.error_access')); }
$items = db()->query('SELECT id, name, address, city, country_code, status, image_path FROM ' . table('attractions') . ' ORDER BY name')->fetchAll();
$categories = db()->query("SELECT id,name FROM " . table('categories') . " WHERE type='attraction' AND status='active' ORDER BY name")->fetchAll();
$categoryFilter = filter_input(INPUT_GET, 'category', FILTER_VALIDATE_INT, ['options'=>['min_range'=>1]]) ?: 0;
foreach ($items as &$item) { $q=db()->prepare('SELECT c.id,c.name FROM '.table('attraction_categories').' m JOIN '.table('categories').' c ON c.id=m.category_id WHERE m.attraction_id=:id ORDER BY c.name'); $q->execute([':id'=>$item['id']]); $item['category_rows']=$q->fetchAll(); $item['category_ids']=implode(',',array_column($item['category_rows'],'id')); $item['category_names']=implode(' · ',array_column($item['category_rows'],'name')); } unset($item);
if ($categoryFilter) { $items=array_values(array_filter($items, static fn(array $item): bool => in_array($categoryFilter, array_map('intval', explode(',', $item['category_ids'] ?: '0')), true))); }
?>
<!doctype html><html lang="de"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Sehenswürdigkeiten · TripTale</title><style>body{font-family:system-ui,sans-serif;background:#f4f6f8;margin:0;padding:32px;color:#17202a}main{max-width:900px;margin:auto}.top{display:flex;justify-content:space-between;align-items:center;gap:15px}.card{background:#fff;padding:18px;margin:10px 0;border-radius:12px;border:1px solid #d6e0e4;border-left:4px solid #75aeb9;box-shadow:0 3px 12px #0001}.button{display:inline-block;padding:9px 12px;background:#126e82;color:#fff;border:0;border-radius:7px;text-decoration:none;font:inherit;font-weight:700;cursor:pointer}.back{background:#e3ebee;color:#126e82}.delete{background:#9d2d2d}.muted{color:#59636d}.thumb{width:96px;height:70px;object-fit:contain;background:#e8eef0;border-radius:8px;float:right;margin-left:12px}.actions{margin-top:12px;display:flex;gap:8px;flex-wrap:wrap}.actions form{margin:0}.filters{display:flex;gap:12px;flex-wrap:wrap;margin:14px 0}.filters label{font-weight:700;flex:1;min-width:200px}.filters input,.filters select{box-sizing:border-box;width:100%;margin-top:5px;padding:10px;border:1px solid #b9c1c8;border-radius:7px;font:inherit}*{box-sizing:border-box}html,body{max-width:100%;overflow-x:hidden}@media(max-width:600px){body{padding:12px}main{margin:18px auto}.top{flex-wrap:wrap}.top>*{width:100%}h1{font-size:1.6rem}}</style><link rel="stylesheet" href="/assets/triptale-ui.css?v=19"></head><body><main><a class="button back" href="/dashboard.php">← Dashboard</a><div class="top"><h1>Sehenswürdigkeiten</h1><a class="button" href="/attraction-edit.php">+ Neue Sehenswürdigkeit</a></div><form class="filters" method="get"><label>Suche<input id="entry-search" type="search" placeholder="Name oder Ort eingeben …"></label><label>Kategorie<select name="category" onchange="this.form.submit()"><option value="">Alle Kategorien</option><?php foreach($categories as $category): ?><option value="<?= (int)$category['id'] ?>"<?= $categoryFilter===(int)$category['id']?' selected':'' ?>><?= htmlspecialchars($category['name'],ENT_QUOTES,'UTF-8') ?></option><?php endforeach; ?></select></label><label>Land<select id="country-filter"><option value="">Alle Länder</option><?php foreach(triptale_countries() as $code=>$country):if($code!==''): ?><option value="<?= $code ?>"><?= htmlspecialchars($country,ENT_QUOTES,'UTF-8') ?></option><?php endif;endforeach; ?></select></label></form><?php if (!$items): ?><section class="card"><h2>Noch keine Sehenswürdigkeiten</h2><p class="muted">Lege einen ersten wiederverwendbaren Eintrag an.</p></section><?php endif; ?><?php foreach ($items as $item): ?><section class="card" data-search="<?= htmlspecialchars(strtolower($item['name'].' '.$item['city'].' '.$item['category_names']),ENT_QUOTES,'UTF-8') ?>" data-country="<?= htmlspecialchars($item['country_code']??'',ENT_QUOTES,'UTF-8') ?>"><?php if ($item['image_path']): ?><img class="thumb" src="/<?= htmlspecialchars($item['image_path'],ENT_QUOTES,'UTF-8') ?>" alt=""><?php endif; ?><strong><?= htmlspecialchars($item['name'], ENT_QUOTES, 'UTF-8') ?></strong><?php if ($item['address'] || $item['city'] || $item['country_code']): ?><p class="muted"><?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; ?></p><?php endif; ?><?php if($item['category_names']): ?><p class="muted">Kategorien: <?= htmlspecialchars($item['category_names'],ENT_QUOTES,'UTF-8') ?></p><?php endif; ?><p class="muted"><?= $item['status'] === 'active' ? 'Aktiv' : 'Deaktiviert' ?></p><div class="actions"><a class="button" href="/attraction-edit.php?id=<?= (int)$item['id'] ?>">Bearbeiten</a><form method="post" action="/catalog-delete.php" onsubmit='return confirm("Diese Sehenswürdigkeit inklusive aller Einträge in Reiseplänen, Hinweise und Bilder wirklich endgültig löschen?");'><input type="hidden" name="csrf_token" value="<?= htmlspecialchars(csrf_token(),ENT_QUOTES,'UTF-8') ?>"><input type="hidden" name="type" value="attraction"><input type="hidden" name="id" value="<?= (int)$item['id'] ?>"><button class="button delete" type="submit">Löschen</button></form></div></section><?php endforeach; ?></main><script>const search=document.getElementById('entry-search'),country=document.getElementById('country-filter');function filterEntries(){const term=search.value.toLowerCase(),land=country.value;document.querySelectorAll('.card[data-search]').forEach(card=>card.hidden=!(card.dataset.search.includes(term)&&(!land||card.dataset.country===land)));}search?.addEventListener('input',filterEntries);country?.addEventListener('change',filterEntries);</script></body></html>
