<?php
if (!isset($page_title)) {
    $page_title = 'Ana Sayfa';
}

// Menü öğelerini al
$menuItems = getMenuItems();

// Ana sayfa kontrolü (index.php)
$is_homepage = (basename($_SERVER['SCRIPT_NAME']) === 'index.php' && !isset($meta_description) && !isset($meta_title));

// Ana sayfa için SEO ayarlarını yükle
$homepage_seo = [];
if ($is_homepage) {
    try {
        $stmt = $pdo->query("SELECT setting_key, setting_value FROM settings WHERE setting_key IN ('homepage_meta_title', 'homepage_meta_description', 'homepage_og_title', 'homepage_og_description', 'homepage_og_image')");
        while ($row = $stmt->fetch()) {
            $homepage_seo[$row['setting_key']] = $row['setting_value'];
        }
    } catch (PDOException $e) {
        // Settings tablosu yoksa veya hata varsa devam et
    }
}

// Title belirleme
$final_title = $page_title . ' - ' . SITE_NAME;
if ($is_homepage && !empty($homepage_seo['homepage_meta_title'])) {
    $final_title = $homepage_seo['homepage_meta_title'];
} elseif (isset($meta_title) && !empty($meta_title)) {
    $final_title = $meta_title;
}

// Description belirleme
$final_description = '';
if ($is_homepage && !empty($homepage_seo['homepage_meta_description'])) {
    $final_description = $homepage_seo['homepage_meta_description'];
} elseif (isset($meta_description) && !empty($meta_description)) {
    $final_description = $meta_description;
}
?>
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= e($final_title) ?></title>
    
    <!-- SEO Meta Tags -->
    <?php if (!empty($final_description)): ?>
        <meta name="description" content="<?= e($final_description) ?>">
    <?php endif; ?>
    <?php if (isset($meta_keywords) && !empty($meta_keywords)): ?>
        <meta name="keywords" content="<?= e($meta_keywords) ?>">
    <?php endif; ?>
    
    <!-- Open Graph Tags (Ana sayfa için) -->
    <?php if ($is_homepage && (!empty($homepage_seo['homepage_og_title']) || !empty($homepage_seo['homepage_og_description']) || !empty($homepage_seo['homepage_og_image']))): ?>
        <meta property="og:type" content="website">
        <meta property="og:url" content="<?= SITE_URL ?>">
        <?php if (!empty($homepage_seo['homepage_og_title'])): ?>
            <meta property="og:title" content="<?= e($homepage_seo['homepage_og_title']) ?>">
        <?php else: ?>
            <meta property="og:title" content="<?= e($final_title) ?>">
        <?php endif; ?>
        <?php if (!empty($homepage_seo['homepage_og_description'])): ?>
            <meta property="og:description" content="<?= e($homepage_seo['homepage_og_description']) ?>">
        <?php elseif (!empty($final_description)): ?>
            <meta property="og:description" content="<?= e($final_description) ?>">
        <?php endif; ?>
        <?php if (!empty($homepage_seo['homepage_og_image'])): ?>
            <meta property="og:image" content="<?= e(getImageUrl($homepage_seo['homepage_og_image'])) ?>">
        <?php endif; ?>
        <meta property="og:site_name" content="<?= e(SITE_NAME) ?>">
    <?php endif; ?>
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <!-- Custom CSS -->
    <link rel="stylesheet" href="<?= ASSETS_URL ?>/css/style.css">
</head>
<body>
    <!-- Navigation -->
    <header>
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
            <div class="container">
                <a class="navbar-brand" href="<?= SITE_URL ?>">
                    <i class="fas fa-skiing me-2"></i><?= SITE_NAME ?>
                </a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="navbarNav">
                    <ul class="navbar-nav me-auto">
                        <li class="nav-item">
                            <a class="nav-link" href="<?= SITE_URL ?>">Ana Sayfa</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?= SITE_URL ?>/companies.php">Firmalar</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?= SITE_URL ?>/blog.php">Blog</a>
                        </li>
                        <?php if (!empty($menuItems)): ?>
                            <?php foreach ($menuItems as $item): ?>
                                <?php if (empty($item['children'])): ?>
                                    <li class="nav-item">
                                        <a class="nav-link" href="<?= e($item['url']) ?>" target="<?= e($item['target']) ?>">
                                            <?php if ($item['icon']): ?>
                                                <i class="<?= e($item['icon']) ?> me-1"></i>
                                            <?php endif; ?>
                                            <?= e($item['title']) ?>
                                        </a>
                                    </li>
                                <?php else: ?>
                                    <li class="nav-item dropdown">
                                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown<?= $item['id'] ?>" role="button" data-bs-toggle="dropdown">
                                            <?php if ($item['icon']): ?>
                                                <i class="<?= e($item['icon']) ?> me-1"></i>
                                            <?php endif; ?>
                                            <?= e($item['title']) ?>
                                        </a>
                                        <ul class="dropdown-menu">
                                            <?php foreach ($item['children'] as $child): ?>
                                                <li>
                                                    <a class="dropdown-item" href="<?= e($child['url']) ?>" target="<?= e($child['target']) ?>">
                                                        <?php if ($child['icon']): ?>
                                                            <i class="<?= e($child['icon']) ?> me-1"></i>
                                                        <?php endif; ?>
                                                        <?= e($child['title']) ?>
                                                    </a>
                                                </li>
                                            <?php endforeach; ?>
                                        </ul>
                                    </li>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </ul>
                    <ul class="navbar-nav">
                        <li class="nav-item">
                            <a class="nav-link btn btn-primary text-white ms-2" href="<?= SITE_URL ?>/company-register.php">
                                <i class="fas fa-user-plus me-1"></i>Firma Üye Ol
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </header>

    <!-- Flash Messages -->
    <?php if (isset($_SESSION['flash_message'])): ?>
        <div class="container mt-3">
            <div class="alert alert-<?= $_SESSION['flash_message_type'] ?? 'info' ?> alert-dismissible fade show" role="alert">
                <?= e($_SESSION['flash_message']) ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        </div>
        <?php
        unset($_SESSION['flash_message']);
        unset($_SESSION['flash_message_type']);
        ?>
    <?php endif; ?>
