<?php
// File: pages/kampanye.php
// Halaman untuk menampilkan semua kampanye (galang dana)
$pageTitle = 'Galang Dana';
$activePage = 'kampanye';
require_once '../includes/header.php';

// Pagination
$limit = 9; // Jumlah kampanye per halaman
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $limit;

// Filter kampanye
$status_filter = isset($_GET['status']) ? $_GET['status'] : 'aktif';
$search = isset($_GET['search']) ? sanitize($_GET['search']) : '';

// Query dasar
$base_query = "SELECT k.*, 
               (SELECT SUM(d.jumlah) FROM donasi d WHERE d.kampanye_id = k.id AND d.status = 'diverifikasi') as total_donasi,
               (SELECT COUNT(d.id) FROM donasi d WHERE d.kampanye_id = k.id AND d.status = 'diverifikasi') as jumlah_donatur
               FROM kampanye k 
               WHERE k.status = :status";

// Tambahkan filter pencarian jika ada
if (!empty($search)) {
    $base_query .= " AND (k.judul LIKE :search OR k.deskripsi LIKE :search)";
}

// Hitung total kampanye (untuk pagination)
$db = dbConnect();
$db->query($base_query);
$db->bind(':status', $status_filter);
if (!empty($search)) {
    $db->bind(':search', "%$search%");
}
$total_kampanye = count($db->resultSet());
$total_pages = ceil($total_kampanye / $limit);

// Query untuk data kampanye dengan limit
$query = $base_query . " ORDER BY k.created_at DESC LIMIT :start, :limit";
$db->query($query);
$db->bind(':status', $status_filter);
if (!empty($search)) {
    $db->bind(':search', "%$search%");
}
$db->bind(':start', $start, PDO::PARAM_INT);
$db->bind(':limit', $limit, PDO::PARAM_INT);
$kampanye_list = $db->resultSet();
?>

<!-- Hero Section -->
<section class="page-header bg-success text-white">
    <div class="container py-5 text-center">
        <h1 class="fw-bold">Galang Dana</h1>
        <p class="lead">Pilih kampanye yang ingin Anda dukung dan mulai berbagi kebaikan</p>
    </div>
</section>

<!-- Kampanye Section -->
<section class="py-5">
    <div class="container">
        <!-- Filter dan Pencarian -->
        <div class="row mb-4">
            <div class="col-md-6 mb-3 mb-md-0">
                <form action="" method="get" class="d-flex">
                    <select name="status" class="form-select me-2">
                        <option value="aktif" <?= $status_filter == 'aktif' ? 'selected' : '' ?>>Kampanye Aktif</option>
                        <option value="selesai" <?= $status_filter == 'selesai' ? 'selected' : '' ?>>Kampanye Selesai</option>
                    </select>
                    <button type="submit" class="btn btn-success">Filter</button>
                </form>
            </div>
            <div class="col-md-6">
                <form action="" method="get" class="d-flex">
                    <input type="hidden" name="status" value="<?= $status_filter ?>">
                    <input type="text" name="search" class="form-control me-2" placeholder="Cari kampanye..." value="<?= $search ?>">
                    <button type="submit" class="btn btn-success">Cari</button>
                </form>
            </div>
        </div>
        
        <!-- Kampanye Cards -->
        <div class="row">
            <?php if (empty($kampanye_list)): ?>
                <div class="col-12 text-center py-5">
                    <h4>Tidak ada kampanye yang ditemukan</h4>
                    <p>Silakan coba filter atau pencarian lain</p>
                </div>
            <?php else: ?>
                <?php foreach ($kampanye_list as $kampanye): ?>
                    <?php 
                    $total_donasi = $kampanye->total_donasi ?? 0;
                    $persentase = hitungPersentase($total_donasi, $kampanye->target_donasi);
                    $jumlah_donatur = $kampanye->jumlah_donatur ?? 0;
                    ?>
                    <div class="col-md-6 col-lg-4 mb-4">
                        <div class="card campaign-card h-100">
                            <img src="<?= SITE_URL ?>/assets/uploads/kampanye/<?= $kampanye->gambar ?>" class="card-img-top" alt="<?= $kampanye->judul ?>">
                            <div class="card-body">
                                <h5 class="card-title"><?= $kampanye->judul ?></h5>
                                <p class="card-text text-muted small"><?= potongTeks($kampanye->deskripsi, 100) ?></p>
                                
                                <div class="progress">
                                    <div class="progress-bar" role="progressbar" style="width: <?= $persentase ?>%;" aria-valuenow="<?= $persentase ?>" aria-valuemin="0" aria-valuemax="100"></div>
                                </div>
                                
                                <div class="campaign-stats d-flex justify-content-between mb-3">
                                    <div>
                                        <strong><?= rupiah($total_donasi) ?></strong>
                                        <div class="small">dari <?= rupiah($kampanye->target_donasi) ?></div>
                                    </div>
                                    <div>
                                        <strong><?= $persentase ?>%</strong>
                                        <div class="small">Tercapai</div>
                                    </div>
                                    <div>
                                        <strong><?= $jumlah_donatur ?></strong>
                                        <div class="small">Donatur</div>
                                    </div>
                                </div>
                                
                                <a href="<?= SITE_URL ?>/pages/kampanye-detail.php?id=<?= $kampanye->id ?>" class="btn btn-success w-100">Donasi Sekarang</a>
                            </div>
                            <div class="card-footer bg-white text-center">
                                <small class="text-muted">
                                    <i class="far fa-clock me-1"></i> 
                                    <?php if ($kampanye->status == 'aktif'): ?>
                                        Berakhir pada: 
                                        <span class="countdown" data-end="<?= $kampanye->tanggal_selesai ?>">
                                            <?= tanggal_indo($kampanye->tanggal_selesai) ?>
                                        </span>
                                    <?php else: ?>
                                        Kampanye telah selesai
                                    <?php endif; ?>
                                </small>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
        
        <!-- Pagination -->
        <?php if ($total_pages > 1): ?>
            <div class="row mt-4">
                <div class="col-12">
                    <nav aria-label="Page navigation">
                        <ul class="pagination justify-content-center">
                            <li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">
                                <a class="page-link" href="?page=<?= $page-1 ?>&status=<?= $status_filter ?>&search=<?= $search ?>" aria-label="Previous">
                                    <span aria-hidden="true">&laquo;</span>
                                </a>
                            </li>
                            
                            <?php for($i = 1; $i <= $total_pages; $i++): ?>
                                <li class="page-item <?= ($page == $i) ? 'active' : '' ?>">
                                    <a class="page-link" href="?page=<?= $i ?>&status=<?= $status_filter ?>&search=<?= $search ?>"><?= $i ?></a>
                                </li>
                            <?php endfor; ?>
                            
                            <li class="page-item <?= ($page >= $total_pages) ? 'disabled' : '' ?>">
                                <a class="page-link" href="?page=<?= $page+1 ?>&status=<?= $status_filter ?>&search=<?= $search ?>" aria-label="Next">
                                    <span aria-hidden="true">&raquo;</span>
                                </a>
                            </li>
                        </ul>
                    </nav>
                </div>
            </div>
        <?php endif; ?>
    </div>
</section>

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