cat << 'EOF' > /var/www/html/admin.php
<?php
session_start();

if (!isset($_SESSION['user']) || !isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
header("Location: admin_login.php");
exit;
}

$kansio = "/var/www/html/viestit";

if (isset($_POST['poista_tiedosto'])) {
$poistettava = basename($_POST['poista_tiedosto']);
$polku = $kansio . "/" . $poistettava;
if (file_exists($polku) && is_file($polku)) {
unlink($polku);
header("Location: admin.php");
exit;
}
}

$viestit = [];
if (is_dir($kansio)) {
$tiedostot = glob($kansio . "/*.json");
if ($tiedostot) {
foreach ($tiedostot as $t) {
$sisalto = file_get_contents($t);
$data = json_decode($sisalto, true);
if ($data) {
$data['tiedosto_nimi'] = basename($t);
$viestit[] = $data;
}
}
}
}
?>
<!DOCTYPE html>
<html lang="fi">
<head>
<meta charset="UTF-8">
<title>Admin - Livekamera</title>
<style>
body { background-color: #0d0d1a; color: #fff; font-family: 'Courier New', monospace; padding: 2rem; }
.container { max-width: 800px; margin: 0 auto; }
.nav-bar { display: flex; justify-content: space-between; align-items: center; border-bottom: 2px solid #00ffff; padding-bottom: 1rem; margin-bottom: 2rem; }
h1 { color: #00ffff; text-shadow: 0 0 10px #00ffff; }
.logout-btn { background: transparent; color: #ff007f; border: 2px solid #ff007f; padding: 0.5rem 1rem; cursor: pointer; font-weight: bold; text-transform: uppercase; }
.logout-btn:hover { background: #ff007f; color: #000; box-shadow: 0 0 15px #ff007f; }
.card { background: #120c1f; border: 1px solid #39ff14; padding: 1.5rem; margin-bottom: 1.5rem; }
.card h3 { color: #39ff14; margin-top: 0; }
.msg { background: #0d0d1a; padding: 1rem; border-left: 3px solid #ff007f; margin-bottom: 1rem; }
.delete-btn { background: transparent; color: #ff0055; border: 1px solid #ff0055; padding: 0.4rem 1rem; cursor: pointer; font-weight: bold; text-transform: uppercase; }
.delete-btn:hover { background: #ff0055; color: #000; box-shadow: 0 0 10px #ff0055; }
</style>
</head>
<body>
<div class="container">
<div class="nav-bar">
<h1>Ylläpitopaneeli</h1>
<a href="logout.php"><button class="logout-btn">Kirjaudu ulos</button></a>
</div>

<h2>Saapuneet viestit</h2>
<?php if (empty($viestit)): ?>
<p style="color: #666; text-align: center; padding: 2rem; border: 1px dashed #444;">Ei uusia viestejä kansion sisällä.</p>
<?php else: ?>
<?php foreach ($viestit as $v): ?>
<div class="card">
<h3>Lähettäjä: <?php echo htmlspecialchars($v['nimi'] ?? 'Tuntematon'); ?> (<?php echo htmlspecialchars($v['sahkoposti'] ?? '-'); ?>)</h3>
<div class="msg"><?php echo htmlspecialchars($v['viesti'] ?? ''); ?></div>
<form action="" method="POST" style="margin: 0;">
<input type="hidden" name="poista_tiedosto" value="<?php echo htmlspecialchars($v['tiedosto_nimi']); ?>">
<button type="submit" class="delete-btn" onclick="return confirm('Haluatko varmasti poistaa viestin?');">Poista viesti</button>
</form>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</body>
</html>
EOF
