<?php
// ketua/kampanye/detail.php
$pageTitle = 'Detail Kampanye';
require_once '../../includes/config.php';
require_once '../../includes/database.php';
require_once '../../includes/functions.php';
require_once '../../includes/auth.php';

// Mulai session jika belum dimulai
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

// Cek apakah user sudah login dan role-nya ketua
if (!isset($_SESSION['user_id']) || !isset($_SESSION['role']) || $_SESSION['role'] != 'ketua') {
    // Redirect ke halaman login jika belum login atau bukan ketua
    setFlash('login_required', 'Anda harus login sebagai ketua yayasan untuk mengakses halaman ini', 'danger');
    header('Location: ' . SITE_URL . '/auth/login.php');
    exit;
}

// Cek apakah ada ID kampanye
if (!isset($_GET['id']) || empty($_GET['id'])) {
    setFlash('kampanye', 'ID kampanye tidak valid', 'danger');
    header('Location: ' . SITE_URL . '/ketua/kampanye/index.php');
    exit;
}

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

// Ambil detail kampanye
$db->query("SELECT k.*, u.nama_lengkap as created_by_name, 
            (SELECT SUM(d.jumlah) FROM donasi d WHERE d.kampanye_id = k.id AND d.status = 'diverifikasi') as total_terkumpul,
            (SELECT COUNT(d.id) FROM donasi d WHERE d.kampanye_id = k.id AND d.status = 'diverifikasi') as jumlah_donatur
            FROM kampanye k
            JOIN users u ON k.created_by = u.id
            WHERE k.id = :id");
$db->bind(':id', $id);
$kampanye = $db->single();

if (!$kampanye) {
    setFlash('kampanye', 'Kampanye tidak ditemukan', 'danger');
    header('Location: ' . SITE_URL . '/ketua/kampanye/index.php');
    exit;
}

// Proses persetujuan/penolakan kampanye
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    $action = $_POST['action'];
    $keterangan = isset($_POST['keterangan']) ? sanitize($_POST['keterangan']) : '';
    
    if ($action === 'approve') {
    // Approve kampanye
    $db->query("UPDATE kampanye SET status = 'aktif', approved_by = :user_id, approved_at = NOW() WHERE id = :id");
    $db->bind(':user_id', $_SESSION['user_id']);
    $db->bind(':id', $id);
    
    if ($db->execute()) {
        // Log aktivitas
        logActivity('Menyetujui kampanye', 'kampanye', $id, 'Ketua menyetujui kampanye: ' . $kampanye->judul);
        
        setFlash('kampanye', 'Kampanye berhasil disetujui', 'success');
        header('Location: ' . SITE_URL . '/ketua/kampanye/index.php');
        exit;
    } else {
        setFlash('kampanye', 'Gagal menyetujui kampanye', 'danger');
    }
} elseif ($action === 'reject') {
    // Reject kampanye
    $db->query("UPDATE kampanye SET status = 'dibatalkan', approved_by = :user_id, approved_at = NOW() WHERE id = :id");
    $db->bind(':user_id', $_SESSION['user_id']);
    $db->bind(':id', $id);
    
    if ($db->execute()) {
        // Log aktivitas
        logActivity('Menolak kampanye', 'kampanye', $id, 'Ketua menolak kampanye: ' . $kampanye->judul . '. Alasan: ' . $keterangan);
        
        setFlash('kampanye', 'Kampanye ditolak', 'success');
        header('Location: ' . SITE_URL . '/ketua/kampanye/index.php');
        exit;
    } else {
        setFlash('kampanye', 'Gagal menolak kampanye', 'danger');
    }
}
}

// Include header
require_once '../../includes/header.php';
?>

<div class="container-fluid">
    <div class="row">
        <!-- Sidebar -->
        <nav id="sidebar" class="col-md-3 col-lg-2 d-md-block bg-light sidebar collapse">
            <div class="position-sticky pt-3">
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/ketua/dashboard.php">
                            <i class="fas fa-tachometer-alt me-2"></i>
                            Dashboard
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="<?= SITE_URL ?>/ketua/kampanye/index.php">
                            <i class="fas fa-hand-holding-heart me-2"></i>
                            Persetujuan Kampanye
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/ketua/laporan/index.php">
                            <i class="fas fa-chart-line me-2"></i>
                            Laporan Keuangan
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/ketua/kegiatan/index.php">
                            <i class="fas fa-newspaper me-2"></i>
                            Kegiatan Yayasan
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/ketua/staff/index.php">
                            <i class="fas fa-users-cog me-2"></i>
                            Manajemen Staff
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/ketua/profil/index.php">
                            <i class="fas fa-user-circle me-2"></i>
                            Profil Saya
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/auth/logout.php">
                            <i class="fas fa-sign-out-alt me-2"></i>
                            Logout
                        </a>
                    </li>
                </ul>
                
                <h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
                    <span>Pintasan</span>
                </h6>
                <ul class="nav flex-column mb-2">
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>" target="_blank">
                            <i class="fas fa-external-link-alt me-2"></i>
                            Lihat Website
                        </a>
                    </li>
                </ul>
            </div>
        </nav>

        <!-- Content Area -->
        <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
            <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                <h1 class="h2">Detail Kampanye</h1>
                <div class="btn-toolbar mb-2 mb-md-0">
                    <a href="<?= SITE_URL ?>/ketua/kampanye/index.php" class="btn btn-sm btn-outline-secondary">
                        <i class="fas fa-arrow-left me-1"></i> Kembali
                    </a>
                </div>
            </div>

            <?php flash(); ?>

            <div class="card mb-4">
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-4 mb-4 mb-md-0">
                            <img src="<?= SITE_URL ?>/assets/uploads/kampanye/<?= $kampanye->gambar ?? 'default.jpg' ?>" alt="<?= $kampanye->judul ?>" class="img-fluid rounded">
                            
                            <div class="mt-3">
                                <div class="d-flex justify-content-between mb-1">
                                    <span>Target</span>
                                    <span class="fw-bold"><?= rupiah($kampanye->target_donasi) ?></span>
                                </div>
                                <div class="d-flex justify-content-between mb-1">
                                    <span>Terkumpul</span>
                                    <span class="fw-bold"><?= rupiah($kampanye->total_terkumpul ?? 0) ?></span>
                                </div>
                                <div class="d-flex justify-content-between mb-1">
                                    <span>Donatur</span>
                                    <span class="fw-bold"><?= $kampanye->jumlah_donatur ?? 0 ?> orang</span>
                                </div>
                                
                                <?php 
                                $persentase = hitungPersentase($kampanye->total_terkumpul ?? 0, $kampanye->target_donasi);
                                ?>
                                <div class="progress mt-2" style="height: 10px;">
                                    <div class="progress-bar bg-success" role="progressbar" style="width: <?= $persentase ?>%;" aria-valuenow="<?= $persentase ?>" aria-valuemin="0" aria-valuemax="100"></div>
                                </div>
                                <div class="text-center mt-1">
                                    <span class="text-muted"><?= $persentase ?>% tercapai</span>
                                </div>
                            </div>
                            
                            <div class="mt-3">
                                <div class="d-flex justify-content-between mb-1">
                                    <span>Status</span>
                                    <span><?= getKampanyeStatusBadge($kampanye->status) ?></span>
                                </div>
                                <div class="d-flex justify-content-between mb-1">
                                    <span>Tanggal Mulai</span>
                                    <span><?= date('d/m/Y', strtotime($kampanye->tanggal_mulai)) ?></span>
                                </div>
                                <div class="d-flex justify-content-between mb-1">
                                    <span>
									<div class="d-flex justify-content-between mb-1">
                                    <span>Tanggal Selesai</span>
                                    <span><?= date('d/m/Y', strtotime($kampanye->tanggal_selesai)) ?></span>
                                </div>
                                <div class="d-flex justify-content-between mb-1">
                                    <span>Dibuat Oleh</span>
                                    <span><?= $kampanye->created_by_name ?></span>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-md-8">
                            <h3 class="mb-3"><?= $kampanye->judul ?></h3>
                            
                            <div class="bg-light p-3 rounded mb-3">
                                <h5>Deskripsi Kampanye</h5>
                                <p><?= nl2br($kampanye->deskripsi) ?></p>
                            </div>
                            
                            <?php if ($kampanye->status === 'menunggu_persetujuan'): ?>
                            <div class="card border-warning mb-3">
                                <div class="card-header bg-warning text-white">
                                    <i class="fas fa-exclamation-circle me-2"></i> Menunggu Persetujuan
                                </div>
                                <div class="card-body">
                                    <form method="post">
                                        <div class="mb-3">
                                            <div class="btn-group w-100" role="group">
                                                <button type="submit" name="action" value="approve" class="btn btn-success">
                                                    <i class="fas fa-check me-2"></i> Setujui Kampanye
                                                </button>
                                                <button type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#rejectModal">
                                                    <i class="fas fa-times me-2"></i> Tolak Kampanye
                                                </button>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </div>
</div>

<?php if ($kampanye->status === 'menunggu_persetujuan'): ?>
<!-- Modal Penolakan -->
<div class="modal fade" id="rejectModal" tabindex="-1" aria-labelledby="rejectModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-danger text-white">
                <h5 class="modal-title" id="rejectModalLabel">Tolak Kampanye</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form method="post">
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="keterangan" class="form-label">Alasan Penolakan</label>
                        <textarea class="form-control" id="keterangan" name="keterangan" rows="4" placeholder="Berikan alasan mengapa kampanye ditolak" required></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" name="action" value="reject" class="btn btn-danger">Tolak</button>
                </div>
            </form>
        </div>
    </div>
</div>
<?php endif; ?>

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