<?php
require_once 'config/config.php';

$page_title = 'Ana Sayfa';

// Slider'ları al
$stmt = $pdo->query("SELECT * FROM sliders WHERE status = 'active' ORDER BY sort_order ASC");
$sliders = $stmt->fetchAll();

// Blog yazıları (son 6)
$stmt = $pdo->query("
    SELECT * FROM blog_posts 
    WHERE status = 'published' 
    ORDER BY created_at DESC 
    LIMIT 6
");
$blogPosts = $stmt->fetchAll();

// Kategoriler
$stmt = $pdo->query("
    SELECT cc.*, 
           (SELECT COUNT(*) FROM company_category_relations WHERE category_id = cc.id) as company_count
    FROM company_categories cc
    WHERE cc.status = 'active'
    ORDER BY cc.name
");
$categories = $stmt->fetchAll();

// En çok incelenen hizmetler (son 10)
$stmt = $pdo->query("
    SELECT s.*, c.company_name, c.membership_type
    FROM services s
    INNER JOIN companies c ON s.company_id = c.id
    WHERE s.status = 'active' AND c.status = 'active'
    ORDER BY s.views DESC, s.created_at DESC
    LIMIT 10
");
$popularServices = $stmt->fetchAll();

include 'includes/header.php';
?>

<!-- Banner/Slider -->
<?php if (!empty($sliders)): ?>
    <section class="hero-slider mb-5">
        <div id="mainSlider" class="carousel slide" data-bs-ride="carousel">
            <div class="carousel-indicators">
                <?php foreach ($sliders as $index => $slider): ?>
                    <button type="button" data-bs-target="#mainSlider" data-bs-slide-to="<?= $index ?>" <?= $index === 0 ? 'class="active"' : '' ?>></button>
                <?php endforeach; ?>
            </div>
            <div class="carousel-inner">
                <?php foreach ($sliders as $index => $slider): ?>
                    <div class="carousel-item <?= $index === 0 ? 'active' : '' ?>">
                        <img src="<?= e(getImageUrl($slider['image'])) ?>" class="d-block w-100" alt="<?= e($slider['title']) ?>" style="height: 600px; object-fit: cover;">
                        <?php if ($slider['title'] || $slider['description']): ?>
                            <div class="carousel-caption d-none d-md-block">
                                <?php if ($slider['title']): ?>
                                    <h2><?= e($slider['title']) ?></h2>
                                <?php endif; ?>
                                <?php if ($slider['description']): ?>
                                    <p><?= e($slider['description']) ?></p>
                                <?php endif; ?>
                                <?php if ($slider['link']): ?>
                                    <a href="<?= e($slider['link']) ?>" class="btn btn-primary btn-lg">Detaylar</a>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>
            </div>
            <button class="carousel-control-prev" type="button" data-bs-target="#mainSlider" data-bs-slide="prev">
                <span class="carousel-control-prev-icon"></span>
            </button>
            <button class="carousel-control-next" type="button" data-bs-target="#mainSlider" data-bs-slide="next">
                <span class="carousel-control-next-icon"></span>
            </button>
        </div>
    </section>
<?php endif; ?>

<div class="container">
    <!-- Blog Yazıları -->
    <?php if (!empty($blogPosts)): ?>
        <section class="mb-5">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2 class="h3">Son Blog Yazıları</h2>
                <a href="<?= SITE_URL ?>/blog.php" class="btn btn-outline-primary">Tümünü Gör</a>
            </div>
            <div class="row">
                <?php foreach ($blogPosts as $post): ?>
                    <div class="col-md-4 mb-4">
                        <div class="card h-100">
                            <?php if ($post['image']): ?>
                                <img src="<?= e(getImageUrl($post['image'])) ?>" class="card-img-top" alt="<?= e($post['title']) ?>" style="height: 200px; object-fit: cover;">
                            <?php endif; ?>
                            <div class="card-body">
                                <h5 class="card-title"><?= e($post['title']) ?></h5>
                                <p class="card-text"><?= e($post['excerpt'] ?: excerpt(strip_tags($post['content']), 100)) ?></p>
                                <a href="<?= SITE_URL ?>/blog/<?= e($post['slug']) ?>" class="btn btn-primary">Devamını Oku</a>
                            </div>
                            <div class="card-footer text-muted small">
                                <?= formatDate($post['created_at'], 'd.m.Y') ?>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </section>
    <?php endif; ?>
    
    <!-- Kategoriler -->
    <?php if (!empty($categories)): ?>
        <section class="mb-5">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2 class="h3">Kayak Merkezleri</h2>
            </div>
            <div class="row">
                <?php foreach ($categories as $category): ?>
                    <div class="col-lg-2 col-md-3 col-sm-4 col-6 mb-3">
                        <div class="category-card-modern">
                            <?php if ($category['icon']): ?>
                                <div class="category-image-wrapper">
                                    <img src="<?= e(getImageUrl($category['icon'])) ?>" alt="<?= e($category['name']) ?>" class="category-image">
                                </div>
                            <?php else: ?>
                                <div class="category-image-wrapper">
                                    <div class="category-icon-placeholder">
                                        <i class="fas fa-mountain"></i>
                                    </div>
                                </div>
                            <?php endif; ?>
                            <div class="category-info">
                                <div class="category-name"><?= e($category['name']) ?></div>
                                <div class="category-footer">
                                    <span class="category-count"><?= $category['company_count'] ?> Firma</span>
                                    <a href="<?= SITE_URL ?>/<?= e($category['slug']) ?>" class="btn btn-sm btn-primary category-btn">Firmaları Gör</a>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </section>
    <?php endif; ?>
    
    <!-- En Çok İncelenen Hizmetler -->
    <?php if (!empty($popularServices)): ?>
        <section class="mb-5">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2 class="h3">En Çok İncelenen Hizmetler</h2>
            </div>
            <div class="row">
                <?php foreach ($popularServices as $service): ?>
                    <div class="col-md-4 col-sm-6 mb-4">
                        <div class="card h-100">
                            <?php if ($service['image']): ?>
                                <img src="<?= e(getImageUrl($service['image'])) ?>" class="card-img-top" alt="<?= e($service['title']) ?>" style="height: 200px; object-fit: cover;">
                            <?php endif; ?>
                            <div class="card-body">
                                <h5 class="card-title"><?= e($service['title']) ?></h5>
                                <p class="card-text"><?= excerpt($service['description'], 80) ?></p>
                                <div class="d-flex justify-content-between align-items-center">
                                    <?php if ($service['price']): ?>
                                        <span class="h5 text-primary mb-0"><?= number_format($service['price'], 2) ?> ₺</span>
                                    <?php endif; ?>
                                    <small class="text-muted">
                                        <i class="fas fa-eye me-1"></i><?= $service['views'] ?>
                                    </small>
                                </div>
                            </div>
                            <div class="card-footer">
                                <small class="text-muted"><?= e($service['company_name']) ?></small>
                                <a href="<?= SITE_URL ?>/hizmet/<?= $service['id'] ?>" class="btn btn-sm btn-primary float-end">Detaylar</a>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </section>
    <?php endif; ?>
</div>

<?php include 'includes/footer.php'; ?>



