<?php
declare(strict_types=1);
/** @var string|null $appBackUrl */
$appBackUrl = $appBackUrl ?? url('index.php');
$cats = categories();
$searchQ = trim((string) ($_GET['q'] ?? ''));
?>
<div class="app-chrome" data-app-chrome>
    <div class="app-top" data-app-top>
        <button type="button" class="app-menu-btn" data-menu-open aria-label="Menü" aria-expanded="false" aria-controls="appSideMenu">☰</button>
        <button type="button" class="app-menu-btn app-menu-close" data-menu-close aria-label="Kapat" hidden>✕</button>

        <a class="app-logo" href="<?= e(url('index.php')) ?>" data-app-logo>
            <img src="<?= e(site_logo_url()) ?>" alt="<?= e(site_name()) ?>">
        </a>

        <form class="app-search-form" action="<?= e(url('index.php')) ?>" method="get" data-search-form>
            <input
                type="search"
                name="q"
                value="<?= e($searchQ) ?>"
                placeholder="Ürün, @üye ara"
                autocomplete="off"
                data-search-input
            >
            <button type="submit" class="app-search-submit" aria-label="Ara">⌕</button>
        </form>

        <button type="button" class="app-search" data-search-open aria-label="Ara">⌕</button>
        <button type="button" class="app-search-close" data-search-close aria-label="Kapat" hidden>✕</button>
    </div>

    <?php $activeCat = trim((string) ($_GET['cat'] ?? '')); ?>
    <?php /* Geçici: tıklanınca açılma / gezinme yok — siparişte yanlış tık sorunu. Sonra düzenlenecek. */ ?>
    <div class="cat-bar sticky-cat is-inert" aria-disabled="true">
        <div class="cat-bar-label" role="presentation">KATEGORİLER ▾</div>
        <?php /* Dropdown şimdilik kapalı
        <div class="cat-dropdown">
            <a href="<?= e(url('index.php')) ?>" class="<?= $activeCat === '' ? 'is-on' : '' ?>">Tümü</a>
            <?php foreach ($cats as $key => $label): ?>
                <a href="<?= e(url('index.php?cat=' . urlencode((string) $key))) ?>"
                   class="<?= $activeCat === $key ? 'is-on' : '' ?>"><?= e($label) ?></a>
            <?php endforeach; ?>
        </div>
        */ ?>
    </div>

    <?php
    // "Ödemeye devam et" şeridi geçici olarak kapalı (yanlışlıkla tıklama / dikkat dağıtma).
    // Geri açmak için aşağıdaki bloğun yorumunu kaldır.
    /*
    $pendingCheckout = function_exists('checkout_pending_order') ? checkout_pending_order() : null;
    if ($pendingCheckout):
        $pendingTitle = (string) ($pendingCheckout['items'][0]['title'] ?? 'Sipariş');
        $pendingUrl = url('odeme.php?order=' . rawurlencode((string) ($pendingCheckout['id'] ?? '')) . '&step=pay');
        $onPayPage = str_contains((string) ($_SERVER['SCRIPT_NAME'] ?? ''), 'odeme.php')
            && (string) ($_GET['step'] ?? '') === 'pay'
            && (string) ($_GET['order'] ?? '') === (string) ($pendingCheckout['id'] ?? '');
        if (!$onPayPage):
    ?>
        <a class="checkout-resume" href="<?= e($pendingUrl) ?>">
            <span class="checkout-resume-text">
                <strong>Ödemeye devam et</strong>
                <em><?= e(mb_strlen($pendingTitle) > 36 ? mb_substr($pendingTitle, 0, 34) . '…' : $pendingTitle) ?></em>
            </span>
            <span class="checkout-resume-go" aria-hidden="true">›</span>
        </a>
    <?php
        endif;
    endif;
    */
    ?>
</div>

<div class="app-menu-backdrop" data-drawer-backdrop hidden></div>
<aside class="app-side-menu" id="appSideMenu" data-drawer hidden>
    <nav class="app-menu-nav" aria-label="Menü">
        <?php foreach (menu_items() as $mi): ?>
            <a href="<?= e(menu_href((string) ($mi['url'] ?? ''))) ?>"><?= e((string) ($mi['label'] ?? 'Link')) ?></a>
        <?php endforeach; ?>
    </nav>
    <a class="app-side-logo" href="<?= e(url('index.php')) ?>">
        <img src="<?= e(site_logo_url()) ?>" alt="<?= e(site_name()) ?>">
    </a>
</aside>
