<?php
use App\Models\Settings;
use App\Models\Category;
use App\Models\Brand;

$siteTitle = Settings::get('store_title', 'Çözüm Merkezi');
$siteDesc = Settings::get('store_description');
$navCategories = Category::all(['parent_slug' => null]);
$navBrands = Brand::all();
?>
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($title ?? $siteTitle); ?></title>
    <meta name="description" content="<?php echo htmlspecialchars($siteDesc); ?>">
    
    <!-- Premium Google Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700;800&family=Plus+Jakarta+Sans:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    
    <!-- FontAwesome icons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    
    <!-- Client Custom stylesheet -->
    <link rel="stylesheet" href="<?php echo $baseUrl; ?>/assets/css/style.css?v=<?php echo time(); ?>">
</head>
<body>

<!-- Header / Navigation -->
<header class="main-header">
    <div class="header-mainbar">
        <div class="container mainbar-content">
            <a href="<?php echo $baseUrl; ?>/" class="site-logo <?php echo (in_array(Settings::get('logo_type', 'text'), ['image', 'both']) && !empty(Settings::get('logo_image', ''))) ? 'has-image' : ''; ?>">
                <?php 
                $lType = Settings::get('logo_type', 'text');
                $lImage = Settings::get('logo_image', '');
                $lTextMain = Settings::get('logo_text_main', 'Çözüm');
                $lTextHigh = Settings::get('logo_text_highlight', 'Merkezi');
                $lSlogan = Settings::get('logo_slogan', 'Yazıcı Teknik Servis Çözümleri');
                
                if ($lType === 'image' && !empty($lImage)): ?>
                    <img src="<?php echo htmlspecialchars($lImage); ?>" alt="Logo" style="max-height: 40px;">
                <?php elseif ($lType === 'both' && !empty($lImage)): ?>
                    <img src="<?php echo htmlspecialchars($lImage); ?>" alt="Logo" style="max-height: 35px; margin-right: 10px;">
                    <div class="logo-text">
                        <h1><?php echo htmlspecialchars($lTextMain); ?> <span><?php echo htmlspecialchars($lTextHigh); ?></span></h1>
                        <?php if(!empty($lSlogan)): ?><p><?php echo htmlspecialchars($lSlogan); ?></p><?php endif; ?>
                    </div>
                <?php else: ?>
                    <i class="fa fa-wrench logo-icon"></i>
                    <div class="logo-text">
                        <h1><?php echo htmlspecialchars($lTextMain); ?> <span><?php echo htmlspecialchars($lTextHigh); ?></span></h1>
                        <?php if(!empty($lSlogan)): ?><p><?php echo htmlspecialchars($lSlogan); ?></p><?php endif; ?>
                    </div>
                <?php endif; ?>
            </a>
            
            <form action="<?php echo $baseUrl; ?>/search" method="GET" class="header-search-form position-relative">
                <input type="text" name="q" placeholder="Hata kodu, model veya yazılım ara..." required autocomplete="off" value="<?php echo htmlspecialchars($_GET['q'] ?? ''); ?>">
                <button type="submit" aria-label="Ara"><i class="fa fa-search"></i></button>
            </form>
            
            <div class="header-user-actions" style="display:flex; align-items:center; margin-left:auto;">
                <?php 
                $session = new \App\Core\Session();
                $customer = $session->get('customer_user');
                $admin = $session->get('admin_user');
                $displayUser = $customer ? $customer : $admin;
                
                if ($displayUser): 
                ?>
                    <div style="position: relative; display: inline-block;" class="user-dropdown-wrapper">
                        <a href="javascript:void(0)" title="Profilim" style="display:flex; align-items:center; justify-content:center; background:var(--bg-light); border:1px solid var(--border); color:var(--text-2); height:44px; border-radius: 20px; padding: 0 12px; gap: 8px; width: auto; text-decoration:none; transition:all 0.2s;">
                            <?php if(!empty($displayUser['avatar'])): ?>
                                <img src="<?php echo htmlspecialchars($displayUser['avatar']); ?>" alt="Avatar" referrerpolicy="no-referrer" style="width: 24px; height: 24px; border-radius: 50%;">
                            <?php else: ?>
                                <i class="fa fa-user-circle-o" style="font-size:20px;"></i>
                            <?php endif; ?>
                            <span style="font-size: 13px; font-weight: 600;"><?php echo htmlspecialchars(explode(' ', $displayUser['name'] ?? $displayUser['username'])[0]); ?></span>
                        </a>
                        <div class="user-dropdown-menu" style="display: none; position: absolute; top: 100%; right: 0; background: var(--bg-white); border: 1px solid var(--border); box-shadow: var(--shadow-md); border-radius: var(--r-md); min-width: 150px; z-index: 100; overflow: hidden; margin-top: 5px;">
                            <?php $everestBaseUrl = rtrim(str_replace('/cozummerkezi', '', $baseUrl), '/'); ?>
                            <a href="<?php echo $everestBaseUrl; ?>/profile" style="display: block; padding: 10px 15px; color: var(--text-1); text-decoration: none; font-size: 14px; font-weight: 600; white-space: nowrap;"><i class="fa fa-user" style="margin-right: 8px;"></i> Profilim</a>
                            <?php if(isset($displayUser['role']) && $displayUser['role'] === 'admin'): ?>
                            <a href="<?php echo $baseUrl; ?>/admin" style="display: block; padding: 10px 15px; color: var(--text-1); text-decoration: none; font-size: 14px; font-weight: 600; white-space: nowrap;"><i class="fa fa-lock" style="margin-right: 8px;"></i> CM Admin Paneli</a>
                            <a href="<?php echo $everestBaseUrl; ?>/admin" style="display: block; padding: 10px 15px; color: var(--text-1); text-decoration: none; font-size: 14px; font-weight: 600; white-space: nowrap;"><i class="fa fa-lock" style="margin-right: 8px;"></i> Everest Admin Paneli</a>
                            <?php endif; ?>
                            <a href="<?php echo $everestBaseUrl; ?>/logout" style="display: block; padding: 10px 15px; color: #ef4444; text-decoration: none; font-size: 14px; font-weight: 600; white-space: nowrap;"><i class="fa fa-sign-out" style="margin-right: 8px;"></i> Çıkış Yap</a>
                        </div>
                    </div>
                    <script>
                        document.querySelector('.user-dropdown-wrapper').addEventListener('click', function(e) {
                            const menu = this.querySelector('.user-dropdown-menu');
                            menu.style.display = menu.style.display === 'none' ? 'block' : 'none';
                            e.stopPropagation();
                        });
                        document.addEventListener('click', function(e) {
                            if (!e.target.closest('.user-dropdown-wrapper')) {
                                const menu = document.querySelector('.user-dropdown-menu');
                                if (menu) menu.style.display = 'none';
                            }
                        });
                    </script>
                <?php else: ?>
                    <?php 
                    $everestBaseUrl = rtrim(str_replace('/cozummerkezi/public', '', $baseUrl), '/');
                    $everestBaseUrl = rtrim(str_replace('/cozummerkezi', '', $everestBaseUrl), '/');
                    $currentUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
                    $loginUrl = $everestBaseUrl . '/auth/google?return_url=' . urlencode($currentUrl);
                    ?>
                    <a href="<?php echo $loginUrl; ?>" title="Giriş Yap" style="display:flex; align-items:center; justify-content:center; background:var(--bg-light); border:1px solid var(--border); color:var(--text-2); height:44px; border-radius: 20px; padding: 0 16px; width: auto; font-size: 13px; font-weight: 600; gap: 6px; text-decoration:none; transition:all 0.2s;" onmouseover="this.style.borderColor='var(--primary)'; this.style.color='var(--primary)';" onmouseout="this.style.borderColor='var(--border)'; this.style.color='var(--text-2)';">
                        <img src="https://upload.wikimedia.org/wikipedia/commons/c/c1/Google_%22G%22_logo.svg" alt="Google" style="width:16px; height:16px;"> Giriş Yap
                    </a>
                <?php endif; ?>
            </div>
        </div>
    </div>
    
    <nav class="header-navigation" id="mainNavigation">
        <div class="container nav-content">
            <ul class="nav-links">
                <?php $everestRoot = rtrim(str_replace('/cozummerkezi', '', $baseUrl), '/'); if(empty($everestRoot)) $everestRoot = '/'; ?>
                <li><a href="<?php echo $everestRoot; ?>"><i class="fa fa-home"></i> Ana Sayfa</a></li>
                <li><a href="<?php echo $baseUrl; ?>/">Çözüm Merkezi</a></li>
                <li><a href="https://services.everestburo.com/" target="_blank">Servis</a></li>
                <li class="has-dropdown">
                    <a href="javascript:void(0)">Reset Yazılımları <i class="fa fa-angle-down"></i></a>
                    <ul class="dropdown-menu">
                        <?php foreach ($navBrands as $b): ?>
                        <li><a href="<?php echo $baseUrl; ?>/<?php echo $b->slug; ?>"><?php echo htmlspecialchars($b->name); ?> Reset</a></li>
                        <?php endforeach; ?>
                    </ul>
                </li>
                <li class="has-dropdown">
                    <a href="javascript:void(0)">Hata Çözümleri <i class="fa fa-angle-down"></i></a>
                    <ul class="dropdown-menu">
                        <?php foreach ($navCategories as $cat): ?>
                        <li><a href="<?php echo $baseUrl; ?>/<?php echo $cat->slug; ?>"><?php echo htmlspecialchars($cat->name); ?></a></li>
                        <?php endforeach; ?>
                    </ul>
                </li>
                <li><a href="<?php echo $baseUrl; ?>/compatibilities">Parça Uyumlulukları</a></li>
                <li><a href="<?php echo $baseUrl; ?>/contact">İletişim</a></li>
            </ul>
        </div>
    </nav>
</header>
<main class="page-container container">
