</div>
</main>
<?php if (!str_contains($pageClass ?? '', 'page-admin')):
    $footerCats = function_exists('categories') ? categories() : [];
    $footerCatLinks = [];
    foreach ($footerCats as $slug => $label) {
        $footerCatLinks[] = [
            'label' => (string) $label,
            'href' => url('index.php?cat=' . urlencode((string) $slug)),
        ];
    }
    if (!$footerCatLinks) {
        $footerCatLinks = [
            ['label' => 'Vitrin', 'href' => url('index.php')],
        ];
    }
    $half = (int) ceil(count($footerCatLinks) / 2);
    $footerCatLeft = array_slice($footerCatLinks, 0, $half);
    $footerCatRight = array_slice($footerCatLinks, $half);

    $popular = [];
    if (function_exists('store_read')) {
        $seen = [];
        foreach (store_read('products') as $p) {
            if (($p['status'] ?? '') !== 'active') {
                continue;
            }
            $b = trim((string) ($p['brand'] ?? ''));
            if ($b === '') {
                continue;
            }
            $key = mb_strtolower($b);
            if (isset($seen[$key])) {
                continue;
            }
            $seen[$key] = true;
            $popular[] = [
                'label' => $b,
                'href' => url('index.php?q=' . urlencode($b)),
            ];
            if (count($popular) >= 10) {
                break;
            }
        }
    }
    if (!$popular) {
        foreach (['Organik', 'Doğal', 'Bal', 'Zeytinyağı', 'Baharat', 'Çay', 'Kuruyemiş', 'Reçel'] as $term) {
            $popular[] = [
                'label' => $term,
                'href' => url('index.php?q=' . urlencode($term)),
            ];
        }
    }
    $popHalf = (int) ceil(count($popular) / 2);
    $popLeft = array_slice($popular, 0, $popHalf);
    $popRight = array_slice($popular, $popHalf);

    $footerImage = function_exists('site_footer_image_url') ? site_footer_image_url() : '';
    $waDigits = function_exists('settings_phone_digits') ? settings_phone_digits() : '';
    $waHref = $waDigits !== '' ? ('https://wa.me/' . $waDigits) : url('index.php');
    $appstoreBadge = function_exists('site_app_badge_url') ? site_app_badge_url('ios') : '';
    $playstoreBadge = function_exists('site_app_badge_url') ? site_app_badge_url('play') : '';
    $appstoreUrl = function_exists('site_app_store_url') ? site_app_store_url('ios') : '';
    $playstoreUrl = function_exists('site_app_store_url') ? site_app_store_url('play') : '';

    $resolveFooterHref = static function (string $raw, string $fallback) use ($waDigits): array {
        $raw = trim($raw);
        if ($raw === '' || $raw === '#') {
            return [$fallback, $waDigits !== '' && str_contains($fallback, 'wa.me')];
        }
        if (preg_match('#^(https?:|mailto:|tel:|//)#i', $raw)) {
            return [$raw, (bool) preg_match('#^(https?:|//)#i', $raw)];
        }
        if (str_starts_with($raw, '/')) {
            return [$raw, false];
        }
        return [url(ltrim($raw, '/')), false];
    };
    $contactRaw = function_exists('theme_t') ? theme_t('footer_url_contact') : '';
    $supportRaw = function_exists('theme_t') ? theme_t('footer_url_support') : '';
    [$contactHref, $contactExternal] = $resolveFooterHref($contactRaw, $waHref);
    [$supportHref, $supportExternal] = $resolveFooterHref($supportRaw, $waHref);
?>
<footer class="site-footer<?= $footerImage !== '' ? ' has-nature' : '' ?>">
    <?php if ($footerImage !== ''): ?>
        <div class="footer-nature" aria-hidden="true">
            <img src="<?= e($footerImage) ?>" alt="">
        </div>
    <?php endif; ?>
    <div class="container footer-inner">
        <div class="footer-apps">
            <?php if ($appstoreUrl !== ''): ?>
                <a class="footer-app-badge" href="<?= e($appstoreUrl) ?>" target="_blank" rel="noopener">
                    <img src="<?= e($appstoreBadge) ?>" alt="App Store'dan İndirin">
                </a>
            <?php else: ?>
                <span class="footer-app-badge is-soon" title="Yakında">
                    <img src="<?= e($appstoreBadge) ?>" alt="App Store'dan İndirin">
                </span>
            <?php endif; ?>
            <?php if ($playstoreUrl !== ''): ?>
                <a class="footer-app-badge" href="<?= e($playstoreUrl) ?>" target="_blank" rel="noopener">
                    <img src="<?= e($playstoreBadge) ?>" alt="Google Play'den Alın">
                </a>
            <?php else: ?>
                <span class="footer-app-badge is-soon" title="Yakında">
                    <img src="<?= e($playstoreBadge) ?>" alt="Google Play'den Alın">
                </span>
            <?php endif; ?>
        </div>
        <?php
        $tw = function_exists('theme_t') ? trim(theme_t('social_twitter')) : '';
        $fb = function_exists('theme_t') ? trim(theme_t('social_facebook')) : '';
        $ig = function_exists('theme_t') ? trim(theme_t('social_instagram')) : '';
        $twHref = $tw !== '' ? $tw : '#';
        $fbHref = $fb !== '' ? $fb : '#';
        $igHref = $ig !== '' ? $ig : '#';
        ?>
        <div class="footer-cols">
            <div class="footer-col">
                <h3><?= e(function_exists('theme_t') ? theme_t('footer_cats_title') : 'Kategoriler') ?></h3>
                <div class="footer-col-split">
                    <ul>
                        <?php foreach ($footerCatLeft as $item): ?>
                            <li><a href="<?= e($item['href']) ?>"><?= e($item['label']) ?></a></li>
                        <?php endforeach; ?>
                    </ul>
                    <?php if ($footerCatRight): ?>
                        <ul>
                            <?php foreach ($footerCatRight as $item): ?>
                                <li><a href="<?= e($item['href']) ?>"><?= e($item['label']) ?></a></li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                </div>
            </div>

            <div class="footer-col">
                <h3><?= e(function_exists('theme_t') ? theme_t('footer_popular_title') : 'Popüler Aramalar') ?></h3>
                <div class="footer-col-split">
                    <ul>
                        <?php foreach ($popLeft as $item): ?>
                            <li><a href="<?= e($item['href']) ?>"><?= e($item['label']) ?></a></li>
                        <?php endforeach; ?>
                    </ul>
                    <?php if ($popRight): ?>
                        <ul>
                            <?php foreach ($popRight as $item): ?>
                                <li><a href="<?= e($item['href']) ?>"><?= e($item['label']) ?></a></li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                </div>
            </div>

            <div class="footer-col">
                <h3><?= e(function_exists('theme_t') ? theme_t('footer_about_title') : 'Organik Hakkında') ?></h3>
                <ul class="footer-about">
                    <li><a href="<?= e(url('index.php')) ?>"><?= e(function_exists('theme_t') ? theme_t('footer_link_vitrin') : 'Organik Vitrin') ?></a></li>
                    <li><a href="<?= e($contactHref) ?>"<?= $contactExternal ? ' target="_blank" rel="noopener"' : '' ?>><?= e(function_exists('theme_t') ? theme_t('footer_link_contact') : 'İletişim') ?></a></li>
                    <li><a href="<?= e($supportHref) ?>"<?= $supportExternal ? ' target="_blank" rel="noopener"' : '' ?>><?= e(function_exists('theme_t') ? theme_t('footer_link_support') : 'Destek') ?></a></li>
                    <li><a href="<?= e(url('magaza.php')) ?>"><?= e(function_exists('theme_t') ? theme_t('footer_link_shop') : 'Satıcı Mağazası') ?></a></li>
                    <li><a href="<?= e(url('guvenli-odeme.php')) ?>"><?= e(function_exists('theme_t') ? theme_t('footer_link_secure') : 'Güvenli Ödeme') ?></a></li>
                </ul>
            </div>
        </div>

        <div class="footer-social" aria-label="Sosyal medya">
            <a class="footer-social-btn" href="<?= e($twHref) ?>"<?= $tw !== '' ? ' target="_blank" rel="noopener"' : '' ?> aria-label="Twitter / X">
                <svg viewBox="0 0 24 24" width="18" height="18" aria-hidden="true"><path fill="currentColor" d="M22.46 6c-.77.35-1.6.58-2.46.69a4.22 4.22 0 0 0 1.85-2.33 8.4 8.4 0 0 1-2.67 1.02 4.2 4.2 0 0 0-7.16 3.83A11.92 11.92 0 0 1 3.15 4.9a4.2 4.2 0 0 0 1.3 5.6 4.17 4.17 0 0 1-1.9-.52v.05a4.2 4.2 0 0 0 3.37 4.12 4.23 4.23 0 0 1-1.9.07 4.21 4.21 0 0 0 3.92 2.92A8.44 8.44 0 0 1 2 19.54a11.9 11.9 0 0 0 6.45 1.89c7.74 0 11.97-6.41 11.97-11.97v-.55A8.5 8.5 0 0 0 22.46 6z"/></svg>
            </a>
            <a class="footer-social-btn" href="<?= e($fbHref) ?>"<?= $fb !== '' ? ' target="_blank" rel="noopener"' : '' ?> aria-label="Facebook">
                <svg viewBox="0 0 24 24" width="18" height="18" aria-hidden="true"><path fill="currentColor" d="M14 9h3V6h-3c-1.7 0-3 1.3-3 3v2H8v3h3v7h3v-7h3l1-3h-4V9c0-.6.4-1 1-1z"/></svg>
            </a>
            <a class="footer-social-btn" href="<?= e($igHref) ?>"<?= $ig !== '' ? ' target="_blank" rel="noopener"' : '' ?> aria-label="Instagram">
                <svg viewBox="0 0 24 24" width="18" height="18" aria-hidden="true"><path fill="currentColor" d="M12 7.2A4.8 4.8 0 1 0 16.8 12 4.8 4.8 0 0 0 12 7.2zm0 7.9A3.1 3.1 0 1 1 15.1 12 3.1 3.1 0 0 1 12 15.1zm5.1-8.9a1.12 1.12 0 1 1-1.12-1.12A1.12 1.12 0 0 1 17.1 6.2zM12 4.5c-2.1 0-2.4 0-3.2.05a4.5 4.5 0 0 0-1.6.3 3 3 0 0 0-1.1.7 3 3 0 0 0-.7 1.1 4.5 4.5 0 0 0-.3 1.6C5 9.1 5 9.4 5 11.5s0 2.4.05 3.2a4.5 4.5 0 0 0 .3 1.6 3 3 0 0 0 .7 1.1 3 3 0 0 0 1.1.7 4.5 4.5 0 0 0 1.6.3c.8.05 1.1.05 3.2.05s2.4 0 3.2-.05a4.5 4.5 0 0 0 1.6-.3 3 3 0 0 0 1.1-.7 3 3 0 0 0 .7-1.1 4.5 4.5 0 0 0 .3-1.6c.05-.8.05-1.1.05-3.2s0-2.4-.05-3.2a4.5 4.5 0 0 0-.3-1.6 3 3 0 0 0-.7-1.1 3 3 0 0 0-1.1-.7 4.5 4.5 0 0 0-1.6-.3C14.4 4.5 14.1 4.5 12 4.5zm0 1.5c2.1 0 2.3 0 3.1.05a3 3 0 0 1 1.1.2 1.5 1.5 0 0 1 .9.9 3 3 0 0 1 .2 1.1c.05.8.05 1 .05 3.1s0 2.3-.05 3.1a3 3 0 0 1-.2 1.1 1.5 1.5 0 0 1-.9.9 3 3 0 0 1-1.1.2c-.8.05-1 .05-3.1.05s-2.3 0-3.1-.05a3 3 0 0 1-1.1-.2 1.5 1.5 0 0 1-.9-.9 3 3 0 0 1-.2-1.1C6.5 14.3 6.5 14.1 6.5 12s0-2.3.05-3.1a3 3 0 0 1 .2-1.1 1.5 1.5 0 0 1 .9-.9 3 3 0 0 1 1.1-.2c.8-.05 1-.05 3.1-.05z"/></svg>
            </a>
        </div>
    </div>
</footer>
<?php endif; ?>
<script src="<?= e(url('assets/js/turkiye-il-ilce.js')) ?>"></script>
<script src="<?= e(url('assets/js/app.js')) ?>"></script>
</body>
</html>
