<?php
// bendahara/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 bendahara
if (!isset($_SESSION['user_id']) || !isset($_SESSION['role']) || $_SESSION['role'] != 'bendahara') {
    // Redirect ke halaman login jika belum login atau bukan bendahara
    setFlash('login_required', 'Anda harus login sebagai bendahara 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('error', 'ID kampanye tidak valid', 'danger');
    header('Location: ' . SITE_URL . '/bendahara/kampanye/index.php');
    exit;
}

$id = (int)$_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_donasi,
            (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('error', 'Kampanye tidak ditemukan', 'danger');
    header('Location: ' . SITE_URL . '/bendahara/kampanye/index.php');
    exit;
}

// Ambil data ketua yang menyetujui kampanye
$ketua_approver = null;
if ($kampanye->approved_by) {
    $db->query("SELECT nama_lengkap FROM users WHERE id = :id");
    $db->bind(':id', $kampanye->approved_by);
    $ketua_approver = $db->single();
}

// Ambil data donasi terkait kampanye
$db->query("SELECT d.*, u.nama_lengkap as donatur_nama 
           FROM donasi d 
           JOIN users u ON d.user_id = u.id 
           WHERE d.kampanye_id = :kampanye_id 
           ORDER BY d.created_at DESC
           LIMIT 10");
$db->bind(':kampanye_id', $id);
$donasi_list = $db->resultSet();

// Ambil total donasi dan statistik
$db->query("SELECT SUM(jumlah) as total, COUNT(id) as jumlah, status 
           FROM donasi 
           WHERE kampanye_id = :kampanye_id 
           GROUP BY status");
$db->bind(':kampanye_id', $id);
$donasi_stats = $db->resultSet();

$total_donasi = 0;
$total_donasi_diverifikasi = 0;
$total_donasi_menunggu = 0;
$total_donasi_ditolak = 0;
$jumlah_donasi = 0;
$jumlah_donasi_diverifikasi = 0;
$jumlah_donasi_menunggu = 0;
$jumlah_donasi_ditolak = 0;

foreach ($donasi_stats as $stat) {
    if ($stat->status == 'diverifikasi') {
        $total_donasi_diverifikasi = $stat->total;
        $jumlah_donasi_diverifikasi = $stat->jumlah;
    } elseif ($stat->status == 'menunggu') {
        $total_donasi_menunggu = $stat->total;
        $jumlah_donasi_menunggu = $stat->jumlah;
    } elseif ($stat->status == 'ditolak') {
        $total_donasi_ditolak = $stat->total;
        $jumlah_donasi_ditolak = $stat->jumlah;
    }
    $total_donasi += $stat->total;
    $jumlah_donasi += $stat->jumlah;
}

// Hitung persentase pencapaian
$persentase = hitungPersentase($total_donasi_diverifikasi, $kampanye->target_donasi);

// 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 ?>/bendahara/dashboard.php">
                            <i class="fas fa-tachometer-alt me-2"></i>
                            Dashboard
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/bendahara/donasi/index.php">
                            <i class="fas fa-donate me-2"></i>
                            Verifikasi Donasi
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/bendahara/laporan/index.php">
                            <i class="fas fa-chart-bar me-2"></i>
                            Laporan Keuangan
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="<?= SITE_URL ?>/bendahara/kampanye/index.php">
                            <i class="fas fa-hand-holding-heart me-2"></i>
                            Data Kampanye
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/bendahara/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 ?>/bendahara/kampanye/index.php" class="btn btn-sm btn-outline-secondary me-2">
                        <i class="fas fa-arrow-left me-1"></i> Kembali
                    </a>
                    <a href="<?= SITE_URL ?>/bendahara/donasi/index.php?kampanye_id=<?= $id ?>" class="btn btn-sm btn-primary">
                        <i class="fas fa-donate me-1"></i> Lihat Donasi
                    </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 mb-3">
                            
                            <div class="card mb-3">
                                <div class="card-body">
                                    <h5 class="card-title">Informasi Kampanye</h5>
                                    <table class="table table-borderless mb-0">
                                        <tr>
                                            <td width="40%">Status</td>
                                            <td width="60%"><?= getKampanyeStatusBadge($kampanye->status) ?></td>
                                        </tr>
                                        <tr>
                                            <td>Tanggal Mulai</td>
                                            <td><?= date('d/m/Y', strtotime($kampanye->tanggal_mulai)) ?></td>
                                        </tr>
                                        <tr>
                                            <td>Tanggal Selesai</td>
                                            <td><?= date('d/m/Y', strtotime($kampanye->tanggal_selesai)) ?></td>
                                        </tr>
                                        <tr>
                                            <td>Dibuat Oleh</td>
                                            <td><?= $kampanye->created_by_name ?></td>
                                        </tr>
                                        <tr>
                                            <td>Tanggal Dibuat</td>
                                            <td><?= date('d/m/Y H:i', strtotime($kampanye->created_at)) ?></td>
                                        </tr>
                                        <?php if ($kampanye->status != 'menunggu_persetujuan' && $ketua_approver): ?>
                                            <tr>
                                                <td>Disetujui Oleh</td>
                                                <td><?= $ketua_approver->nama_lengkap ?></td>
                                            </tr>
                                            <tr>
                                                <td>Tanggal Disetujui</td>
                                                <td><?= date('d/m/Y H:i', strtotime($kampanye->approved_at)) ?></td>
                                            </tr>
                                        <?php endif; ?>
                                    </table>
                                </div>
                            </div>
                            
                            <div class="card">
                                <div class="card-body">
                                    <h5 class="card-title">Target & Pencapaian</h5>
                                    <div class="d-flex justify-content-between mb-1">
                                        <span>Target Donasi</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($total_donasi_diverifikasi) ?></span>
                                    </div>
                                    
                                    <div class="d-flex justify-content-between mb-1">
                                        <span>Jumlah Donatur</span>
                                        <span class="fw-bold"><?= $jumlah_donasi_diverifikasi ?> orang</span>
                                    </div>
                                    
                                    <div class="progress mt-2" style="height: 15px;">
                                        <div class="progress-bar bg-success" role="progressbar" style="width: <?= $persentase ?>%;" aria-valuenow="<?= $persentase ?>" aria-valuemin="0" aria-valuemax="100">
                                            <?= $persentase ?>%
                                        </div>
                                    </div>
                                    
                                    <div class="d-flex justify-content-between mt-3">
                                        <span>Menunggu Verifikasi</span>
                                        <span class="fw-bold"><?= rupiah($total_donasi_menunggu) ?></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-md-8">
                            <h3 class="mb-3"><?= $kampanye->judul ?></h3>
                            
                            <div class="mb-4">
                                <?= $kampanye->deskripsi ?>
                            </div>
                            
                            <h5 class="mt-4 mb-3">Donasi Terbaru</h5>
                            <?php if (empty($donasi_list)): ?>
                                <div class="alert alert-info">
                                    Belum ada donasi untuk kampanye ini.
                                </div>
                            <?php else: ?>
                                <div class="table-responsive">
                                    <table class="table table-hover">
                                        <thead>
                                            <tr>
                                                <th>Tanggal</th>
                                                <th>Donatur</th>
                                                <th>Jumlah</th>
                                                <th>Status</th>
                                                <th>Aksi</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php foreach ($donasi_list as $donasi): ?>
                                                <tr>
                                                    <td><?= date('d/m/Y H:i', strtotime($donasi->created_at)) ?></td>
                                                    <td><?= $donasi->is_anonim ? 'Anonim' : $donasi->donatur_nama ?></td>
                                                    <td><?= rupiah($donasi->jumlah) ?></td>
                                                    <td><?= getDonasiStatusBadge($donasi->status) ?></td>
                                                    <td>
                                                        <a href="<?= SITE_URL ?>/bendahara/donasi/detail.php?id=<?= $donasi->id ?>" class="btn btn-sm btn-info">
                                                            <i class="fas fa-eye"></i> Detail
                                                        </a>
                                                    </td>
                                                </tr>
                                            <?php endforeach; ?>
                                        </tbody>
                                    </table>
                                </div>
                                
                                <div class="text-center mt-3">
                                    <a href="<?= SITE_URL ?>/bendahara/donasi/index.php?kampanye_id=<?= $id ?>" class="btn btn-primary">
                                        <i class="fas fa-list me-1"></i> Lihat Semua Donasi
                                    </a>
                                </div>
                            <?php endif; ?>
                            
                            <h5 class="mt-4 mb-3">Statistik Donasi</h5>
                            <div class="row">
                                <div class="col-md-4 mb-3">
                                    <div class="card text-center h-100">
                                        <div class="card-body">
                                            <h6 class="card-title text-success">Donasi Diverifikasi</h6>
                                            <h4 class="mb-2"><?= rupiah($total_donasi_diverifikasi) ?></h4>
                                            <p class="card-text"><?= $jumlah_donasi_diverifikasi ?> transaksi</p>
                                        </div>
                                    </div>
                                </div>
                                
                                <div class="col-md-4 mb-3">
                                    <div class="card text-center h-100">
                                        <div class="card-body">
                                            <h6 class="card-title text-warning">Menunggu Verifikasi</h6>
                                            <h4 class="mb-2"><?= rupiah($total_donasi_menunggu) ?></h4>
                                            <p class="card-text"><?= $jumlah_donasi_menunggu ?> transaksi</p>
                                        </div>
                                    </div>
                                </div>
                                
                                <div class="col-md-4 mb-3">
                                    <div class="card text-center h-100">
                                        <div class="card-body">
                                            <h6 class="card-title text-danger">Donasi Ditolak</h6>
                                            <h4 class="mb-2"><?= rupiah($total_donasi_ditolak) ?></h4>
                                            <p class="card-text"><?= $jumlah_donasi_ditolak ?> transaksi</p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </div>
</div>

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