<?php
// admin/_auth.php – Delas av alla adminsidor
require __DIR__ . '/../config/config.php';

session_name('radio_admin');
session_start();

function requireLogin(): void {
    if (empty($_SESSION['admin'])) {
        header('Location: ' . dirname($_SERVER['SCRIPT_NAME']) . '/login.php');
        exit;
    }
}

function db(): PDO {
    static $pdo;
    if (!$pdo) {
        $pdo = new PDO('sqlite:' . DB_PATH);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->exec("PRAGMA journal_mode=WAL;");
    }
    return $pdo;
}

function flash(string $key, string $msg = null): ?string {
    if ($msg !== null) {
        $_SESSION['flash'][$key] = $msg;
        return null;
    }
    $v = $_SESSION['flash'][$key] ?? null;
    unset($_SESSION['flash'][$key]);
    return $v;
}

function adminHeader(string $title): void { ?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title><?= htmlspecialchars($title) ?> – Radio Ultra Admin</title>
<link rel="stylesheet" href="../assets/css/style.css">
</head>
<body>
<div class="admin-wrap">
<div class="admin-header">
  <h1>🎙️ <?= htmlspecialchars($title) ?></h1>
  <div style="display:flex;gap:10px;align-items:center">
    <a href="index.php" class="btn btn-secondary">📋 Kanaler</a>
    <a href="../index.php" class="btn btn-secondary" target="_blank">🎵 Spelaren</a>
    <a href="logout.php" class="btn btn-secondary">Logga ut</a>
  </div>
</div>
<?php }

function adminFooter(): void { ?>
</div></body></html>
<?php }
