<?php
// Halaman berita-detail.php (Detail Berita Kegiatan)
require_once '../includes/header.php';

// Cek ID berita dari parameter URL
if (!isset($_GET['id']) || empty($_GET['id'])) {
    setFlash('error', 'ID berita tidak valid', 'danger');
    redirect(SITE_URL . '/pages/berita.php');
    exit;
}

$id = sanitize($_GET['id']);
$db = dbConnect();

// Ambil data berita
$db->query("SELECT b.*, u.nama_lengkap as penulis 
            FROM berita b 
            JOIN users u ON b.created_by = u.id 
            WHERE b.id = :id");
$db->bind(':id', $id);
$berita = $db->single();

// Jika berita tidak ditemukan
if (!$berita) {
    setFlash('error', 'Berita tidak ditemukan', 'danger');
    redirect(SITE_URL . '/pages/berita.php');
    exit;
}

// Set page title
$pageTitle = $berita->judul;
$activePage = 'berita';

// Ambil berita terkait (3 berita terbaru selain berita ini)
$db->query("SELECT b.*, u.nama_lengkap as penulis 
            FROM berita b 
            JOIN users u ON b.created_by = u.id 
            WHERE b.id != :id
            ORDER BY b.created_at DESC 
            LIMIT 3");
$db->bind(':id', $id);
$berita_terkait = $db->resultSet();
?>

<!-- Breadcrumb -->
<section class="py-3 bg-light">
    <div class="container">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb mb-0">
                <li class="breadcrumb-item"><a href="<?= SITE_URL ?>">Beranda</a></li>
                <li class="breadcrumb-item"><a href="<?= SITE_URL ?>/pages/berita.php">Berita Kegiatan</a></li>
                <li class="breadcrumb-item active" aria-current="page"><?= $berita->judul ?></li>
            </ol>
        </nav>
    </div>
</section>

<!-- Berita Detail Section -->
<section class="py-5">
    <div class="container">
        <div class="row">
            <!-- Main Content -->
            <div class="col-lg-8 mb-4 mb-lg-0">
                <div class="card">
                    <div class="card-body">
                        <h1 class="fw-bold mb-3"><?= $berita->judul ?></h1>
                        
                        <div class="d-flex align-items-center text-muted mb-4">
                            <div class="me-3">
                                <i class="far fa-calendar-alt me-1"></i> <?= tanggal_indo(date('Y-m-d', strtotime($berita->created_at)), true) ?>
                            </div>
                            <div>
                                <i class="far fa-user me-1"></i> <?= $berita->penulis ?>
                            </div>
                        </div>
                        
                        <!-- Featured Image -->
                        <div class="mb-4">
                            <img src="<?= SITE_URL ?>/assets/uploads/berita/<?= $berita->gambar ?>" alt="<?= $berita->judul ?>" class="img-fluid rounded">
                        </div>
                        
                        <!-- Berita Content -->
                        <div class="berita-content">
                            <?= nl2br($berita->konten) ?>
                        </div>
                        
                        <!-- Share Buttons -->
                        <div class="mt-4 pt-3 border-top">
                            <h5>Bagikan:</h5>
                            <div class="d-flex gap-2">
                                <a href="https://www.facebook.com/sharer/sharer.php?u=<?= urlencode(SITE_URL . $_SERVER['REQUEST_URI']) ?>" target="_blank" class="btn btn-sm btn-primary">
                                    <i class="fab fa-facebook-f"></i> Facebook
                                </a>
                                <a href="https://twitter.com/intent/tweet?text=<?= urlencode($berita->judul) ?>&url=<?= urlencode(SITE_URL . $_SERVER['REQUEST_URI']) ?>" target="_blank" class="btn btn-sm btn-info text-white">
                                    <i class="fab fa-twitter"></i> Twitter
                                </a>
                                <a href="https://api.whatsapp.com/send?text=<?= urlencode($berita->judul . ' - ' . SITE_URL . $_SERVER['REQUEST_URI']) ?>" target="_blank" class="btn btn-sm btn-success">
                                    <i class="fab fa-whatsapp"></i> WhatsApp
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Sidebar -->
            <div class="col-lg-4">
                <!-- Berita Terkait -->
                <div class="card mb-4">
                    <div class="card-header bg-white">
                        <h5 class="mb-0">Berita Terkait</h5>
                    </div>
                    <div class="card-body">
                        <?php if (empty($berita_terkait)): ?>
                            <p class="text-muted">Tidak ada berita terkait.</p>
                        <?php else: ?>
                            <?php foreach($berita_terkait as $terkait): ?>
                                <div class="d-flex mb-3">
                                    <div class="flex-shrink-0">
                                        <img src="<?= SITE_URL ?>/assets/uploads/berita/<?= $terkait->gambar ?>" alt="<?= $terkait->judul ?>" class="img-fluid rounded" style="width: 80px; height: 60px; object-fit: cover;">
                                    </div>
                                    <div class="flex-grow-1 ms-3">
                                        <a href="<?= SITE_URL ?>/pages/berita-detail.php?id=<?= $terkait->id ?>" class="fw-bold text-decoration-none text-dark"><?= $terkait->judul ?></a>
                                        <p class="small text-muted mb-0"><?= tanggal_indo(date('Y-m-d', strtotime($terkait->created_at))) ?></p>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                </div>
                
                <!-- Kampanye Aktif -->
                <?php
                // Ambil data kampanye aktif
                $db->query("SELECT k.*, 
                           (SELECT SUM(d.jumlah) FROM donasi d WHERE d.kampanye_id = k.id AND d.status = 'diverifikasi') as total_donasi
                           FROM kampanye k 
                           WHERE k.status = 'aktif' AND k.tanggal_selesai >= CURDATE()
                           ORDER BY k.created_at DESC 
                           LIMIT 3");
                $kampanye_aktif = $db->resultSet();
                ?>
                
                <div class="card">
                    <div class="card-header bg-white">
                        <h5 class="mb-0">Kampanye Aktif</h5>
                    </div>
                    <div class="card-body">
                        <?php if (empty($kampanye_aktif)): ?>
                            <p class="text-muted">Tidak ada kampanye aktif saat ini.</p>
                        <?php else: ?>
                            <?php foreach($kampanye_aktif as $kampanye): ?>
                                <?php 
                                $total_donasi = $kampanye->total_donasi ?? 0;
                                $persentase = hitungPersentase($total_donasi, $kampanye->target_donasi);
                                ?>
                                <div class="d-flex mb-3">
                                    <div class="flex-shrink-0">
                                        <img src="<?= SITE_URL ?>/assets/uploads/kampanye/<?= $kampanye->gambar ?>" alt="<?= $kampanye->judul ?>" class="img-fluid rounded" style="width: 80px; height: 60px; object-fit: cover;">
                                    </div>
                                    <div class="flex-grow-1 ms-3">
                                        <a href="<?= SITE_URL ?>/pages/kampanye-detail.php?id=<?= $kampanye->id ?>" class="fw-bold text-decoration-none text-dark"><?= $kampanye->judul ?></a>
                                        <div class="progress mt-1 mb-1" style="height: 5px;">
                                            <div class="progress-bar" role="progressbar" style="width: <?= $persentase ?>%;" aria-valuenow="<?= $persentase ?>" aria-valuemin="0" aria-valuemax="100"></div>
                                        </div>
                                        <div class="d-flex justify-content-between">
                                            <small><?= rupiah($total_donasi) ?></small>
                                            <small><?= $persentase ?>%</small>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                            <div class="text-center mt-3">
                                <a href="<?= SITE_URL ?>/pages/kampanye.php" class="btn btn-sm btn-outline-success">Lihat Semua Kampanye</a>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

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