<?php
declare(strict_types=1);

require_once dirname(__DIR__) . '/includes/functions.php';

function adminHeader(string $title): void
{
    requireAdmin();
    echo '<!doctype html><html lang="tr"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>' . e($title) . ' | ASSK Admin</title><link rel="stylesheet" href="' . e(url('assets/css/style.css')) . '"></head><body class="admin-body"><aside class="admin-sidebar"><a class="brand" href="dashboard.php"><span class="brand-mark">A</span><span><strong>ASSK</strong><small>Admin</small></span></a><nav><a href="dashboard.php">Dashboard</a><a href="leads.php">Gelen Formlar</a><a href="branches.php">Sube Yonetimi</a><a href="blog.php">Blog Yonetimi</a><a href="services.php">Hizmet Yonetimi</a><a href="../index.php">Siteye Don</a><a href="logout.php">Cikis</a></nav></aside><main class="admin-main"><h1>' . e($title) . '</h1>';
    if (!empty($_SESSION['flash'])) {
        echo '<div class="flash">' . e($_SESSION['flash']) . '</div>';
        unset($_SESSION['flash']);
    }
}

function adminFooter(): void
{
    echo '</main></body></html>';
}

function pdoOrFail(): PDO
{
    $pdo = db();
    if (!$pdo) {
        adminHeader('Veritabani baglantisi gerekli');
        echo '<div class="admin-card"><p>Admin paneli icin MySQL baglantisi gerekir. Once <code>database.sql</code> dosyasini ice aktarin ve <code>includes/config.php</code> ayarlarini kontrol edin.</p></div>';
        adminFooter();
        exit;
    }
    return $pdo;
}
