<?php
declare(strict_types=1);

function admin_nav_active(string $key): string
{
    global $adminNav;
    return (($adminNav ?? '') === $key) ? 'is-active' : '';
}

function admin_layout_start(string $title, string $nav = 'dashboard'): void
{
    global $adminNav, $pageTitle, $pageClass;
    $adminNav = $nav;
    $pageTitle = $title;
    $pageClass = 'page-admin';
    $admin = current_user();
    require __DIR__ . '/header.php';
    $flash = get_flash();
    ?>
    <div class="admin-shell" data-admin-shell>
        <div class="admin-backdrop" data-admin-close hidden></div>

        <header class="admin-mobile-bar">
            <button type="button" class="admin-burger" data-admin-open aria-label="Menüyü aç">
                <span></span><span></span><span></span>
            </button>
            <div class="admin-mobile-brand">
                <strong><?= e(site_name()) ?></strong>
                <small><?= e($title) ?></small>
            </div>
            <?php if ($nav !== 'product-add'): ?>
                <a class="admin-btn sm" href="<?= e(url('admin/urun-ekle.php')) ?>">+</a>
            <?php else: ?>
                <span class="admin-mobile-spacer"></span>
            <?php endif; ?>
        </header>

        <aside class="admin-side" data-admin-side>
            <div class="admin-side-brand">
                <span><?= e(mb_strtoupper(mb_substr(site_name(), 0, 1))) ?></span>
                <div>
                    <strong><?= e(site_name()) ?></strong>
                    <small>Yönetim paneli</small>
                </div>
                <button type="button" class="admin-side-close" data-admin-close aria-label="Menüyü kapat">×</button>
            </div>
            <nav class="admin-nav">
                <a class="<?= admin_nav_active('dashboard') ?>" href="<?= e(url('admin/index.php')) ?>">Özet</a>
                <a class="<?= admin_nav_active('product-add') ?>" href="<?= e(url('admin/urun-ekle.php')) ?>">Ürün Ekle</a>
                <a class="<?= admin_nav_active('products') ?>" href="<?= e(url('admin/urunler.php')) ?>">Ürünler</a>
                <a class="<?= admin_nav_active('orders') ?>" href="<?= e(url('admin/siparisler.php')) ?>">Siparişler</a>
                <div class="nav-label">İçerik</div>
                <a class="<?= admin_nav_active('sellers') ?>" href="<?= e(url('admin/saticilar.php')) ?>">Satıcılar</a>
                <a class="<?= admin_nav_active('categories') ?>" href="<?= e(url('admin/kategoriler.php')) ?>">Kategoriler</a>
                <a class="<?= admin_nav_active('users') ?>" href="<?= e(url('admin/uyeler.php')) ?>">Üyeler / Admin</a>
                <a class="<?= admin_nav_active('ibans') ?>" href="<?= e(url('admin/ibanlar.php')) ?>">IBAN</a>
                <div class="nav-label">Görünüm</div>
                <a class="<?= admin_nav_active('theme') ?>" href="<?= e(url('admin/tema.php')) ?>">Tema</a>
                <a class="<?= admin_nav_active('menu') ?>" href="<?= e(url('admin/menu.php')) ?>">Menü</a>
                <a class="<?= admin_nav_active('share') ?>" href="<?= e(url('admin/paylasim.php')) ?>">Paylaşım Kartı</a>
                <div class="nav-label">Sistem</div>
                <a class="<?= admin_nav_active('settings') ?>" href="<?= e(url('admin/ayarlar.php')) ?>">Site Ayarları</a>
                <a class="<?= admin_nav_active('control') ?>" href="<?= e(url('admin/kontrol.php')) ?>">Sıfırlama</a>
            </nav>
            <div class="admin-side-foot">
                <a href="<?= e(url('index.php')) ?>" target="_blank" rel="noopener">Vitrini aç ↗</a>
                <a href="<?= e(url('admin/cikis.php')) ?>">Çıkış yap</a>
            </div>
        </aside>

        <div class="admin-main">
            <header class="admin-top">
                <div>
                    <h1><?= e($title) ?></h1>
                    <p>Hoş geldin, <?= e($admin['name'] ?? 'Admin') ?></p>
                </div>
                <?php if ($nav !== 'product-add'): ?>
                    <a class="admin-btn admin-top-add" href="<?= e(url('admin/urun-ekle.php')) ?>">+ Ürün Ekle</a>
                <?php endif; ?>
            </header>
            <?php if ($flash): ?>
                <div class="admin-alert <?= e(($flash['type'] ?? '') === 'error' ? 'error' : 'ok') ?>">
                    <?= e($flash['message'] ?? '') ?>
                </div>
            <?php endif; ?>
            <div class="admin-content">
    <?php
}

function admin_layout_end(): void
{
    global $pageClass;
    ?>
            </div>
        </div>
    </div>
    <?php
    require __DIR__ . '/footer.php';
}

function admin_status_badge(string $status): string
{
    $status = $status !== '' ? $status : 'active';
    $map = [
        'active' => ['Aktif', ''],
        'hidden' => ['Gizli', 'off'],
        'sold' => ['Satıldı', 'sold'],
    ];
    [$label, $cls] = $map[$status] ?? [$status, 'off'];
    return '<span class="admin-badge ' . e($cls) . '">' . e($label) . '</span>';
}
