<?php
// user/donasi/detail.php
$pageTitle = 'Detail Donasi';
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 donatur
if (!isset($_SESSION['user_id']) || !isset($_SESSION['role']) || $_SESSION['role'] != 'donatur') {
    // Redirect ke halaman login jika belum login atau bukan donatur
    setFlash('login_required', 'Anda harus login sebagai donatur untuk mengakses halaman ini', 'danger');
    header('Location: ' . SITE_URL . '/auth/login.php');
    exit;
}

$user_id = $_SESSION['user_id'];
$db = dbConnect();

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

$id = $_GET['id'];

// Ambil data donasi
$db->query("SELECT d.*, k.judul as kampanye_judul, k.id as kampanye_id
           FROM donasi d 
           JOIN kampanye k ON d.kampanye_id = k.id 
           WHERE d.id = :id AND d.user_id = :user_id");
$db->bind(':id', $id);
$db->bind(':user_id', $user_id);
$donasi = $db->single();

if (!$donasi) {
    setFlash('donasi', 'Donasi tidak ditemukan atau bukan milik Anda', 'danger');
    header('Location: ' . SITE_URL . '/user/donasi/index.php');
    exit;
}

// Ambil data kampanye
$db->query("SELECT k.*, 
           (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 
           WHERE k.id = :id");
$db->bind(':id', $donasi->kampanye_id);
$kampanye = $db->single();

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

<!-- Sidebar & Content -->
<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 ?>/user/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 ?>/user/donasi/index.php">
                            <i class="fas fa-donate me-2"></i>
                            Donasi Saya
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/user/donasi/buat.php">
                            <i class="fas fa-hand-holding-heart me-2"></i>
                            Buat Donasi
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/user/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 Donasi</h1>
                <div class="btn-toolbar mb-2 mb-md-0">
                    <a href="<?= SITE_URL ?>/user/donasi/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="row">
                <!-- Detail Donasi -->
                <div class="col-md-8 mb-4">
                    <div class="card">
                        <div class="card-header bg-light">
                            <h5 class="mb-0">Detail Donasi</h5>
                        </div>
                        <div class="card-body">
                            <div class="row mb-4">
                                <div class="col-md-12">
                                    <?php if ($donasi->status == 'menunggu'): ?>
                                        <div class="alert alert-warning">
                                            <h5 class="alert-heading"><i class="fas fa-clock me-2"></i> Menunggu Verifikasi</h5>
                                            <p class="mb-0">Donasi Anda sedang menunggu verifikasi dari admin. Proses verifikasi biasanya memakan waktu 1x24 jam.</p>
                                            <?php if (empty($donasi->bukti_transfer)): ?>
                                                <hr>
                                                <p class="mb-0"><a href="<?= SITE_URL ?>/user/donasi/upload-bukti.php?id=<?= $donasi->id ?>" class="alert-link">Upload bukti transfer</a> untuk mempercepat proses verifikasi.</p>
                                            <?php endif; ?>
                                        </div>
                                    <?php elseif ($donasi->status == 'diverifikasi'): ?>
                                        <div class="alert alert-success">
                                            <h5 class="alert-heading"><i class="fas fa-check-circle me-2"></i> Donasi Terverifikasi</h5>
                                            <p class="mb-0">Donasi Anda telah diverifikasi. Terima kasih atas kontribusi Anda!</p>
                                            <?php if ($donasi->admin_verifikasi): ?>
                                                <?php
                                                $db->query("SELECT nama_lengkap FROM users WHERE id = :id");
                                                $db->bind(':id', $donasi->admin_verifikasi);
                                                $admin = $db->single();
                                                ?>
                                                <p class="mb-0">Diverifikasi oleh: <?= $admin->nama_lengkap ?? 'Admin' ?></p>
                                            <?php endif; ?>
                                        </div>
                                    <?php else: ?>
                                        <div class="alert alert-danger">
                                            <h5 class="alert-heading"><i class="fas fa-times-circle me-2"></i> Donasi Ditolak</h5>
                                            <p class="mb-0">Maaf, donasi Anda ditolak. Silakan hubungi admin untuk informasi lebih lanjut.</p>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6">
                                    <h5 class="mb-3">Informasi Donasi</h5>
                                    <table class="table table-striped">
                                        <tr>
                                            <td width="40%">Kampanye</td>
                                            <td width="60%"><strong><?= $donasi->kampanye_judul ?></strong></td>
                                        </tr>
                                        <tr>
                                            <td>Jumlah Donasi</td>
                                            <td><strong><?= rupiah($donasi->jumlah) ?></strong></td>
                                        </tr>
                                        <tr>
                                            <td>Metode Pembayaran</td>
                                            <td><strong><?= ucfirst($donasi->metode_pembayaran) ?></strong></td>
                                        </tr>
                                        <tr>
                                            <td>Status</td>
                                            <td>
                                                <?php if ($donasi->status == 'menunggu'): ?>
                                                    <span class="badge bg-warning text-dark">Menunggu</span>
                                                <?php elseif ($donasi->status == 'diverifikasi'): ?>
                                                    <span class="badge bg-success">Diverifikasi</span>
                                                <?php else: ?>
                                                    <span class="badge bg-danger">Ditolak</span>
                                                <?php endif; ?>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>Tanggal Donasi</td>
                                            <td><strong><?= date('d/m/Y H:i', strtotime($donasi->created_at)) ?></strong></td>
                                        </tr>
                                        <tr>
                                            <td>Donasi Sebagai</td>
                                            <td><strong><?= $donasi->is_anonim ? 'Anonim' : $_SESSION['nama_lengkap'] ?></strong></td>
                                        </tr>
                                        <?php if ($donasi->is_otomatis): ?>
                                        <tr>
                                            <td>Donasi Otomatis</td>
                                            <td><strong>Ya (<?= ucfirst($donasi->frekuensi_otomatis) ?>)</strong></td>
                                        </tr>
                                        <tr>
                                            <td>Tanggal Otomatis</td>
                                            <td><strong><?= date('d/m/Y', strtotime($donasi->tanggal_otomatis)) ?></strong></td>
                                        </tr>
                                        <?php endif; ?>
                                        <?php if (!empty($donasi->keterangan)): ?>
                                        <tr>
                                            <td>Keterangan</td>
                                            <td><strong><?= $donasi->keterangan ?></strong></td>
                                        </tr>
                                        <?php endif; ?>
                                    </table>
                                </div>
                                
                                <div class="col-md-6">
                                    <h5 class="mb-3">Bukti Transfer</h5>
                                    <?php if (!empty($donasi->bukti_transfer)): ?>
                                        <div class="text-center">
                                            <img src="<?= SITE_URL ?>/assets/uploads/bukti_transfer/<?= $donasi->bukti_transfer ?>" alt="Bukti Transfer" class="img-fluid rounded" style="max-height: 300px;">
                                        </div>
                                        
                                        <?php if ($donasi->status == 'menunggu'): ?>
                                            <div class="d-grid gap-2 mt-3">
                                                <a href="<?= SITE_URL ?>/user/donasi/upload-bukti.php?id=<?= $donasi->id ?>" class="btn btn-warning btn-sm">
                                                    <i class="fas fa-edit me-1"></i> Ganti Bukti Transfer
                                                </a>
                                            </div>
                                        <?php endif; ?>
                                    <?php else: ?>
                                        <div class="alert alert-info">
                                            <p class="mb-0">Anda belum mengupload bukti transfer.</p>
                                        </div>
                                        
                                        <div class="d-grid gap-2">
                                            <a href="<?= SITE_URL ?>/user/donasi/upload-bukti.php?id=<?= $donasi->id ?>" class="btn btn-primary">
                                                <i class="fas fa-upload me-1"></i> Upload Bukti Transfer
                                            </a>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- Informasi Kampanye -->
                <div class="col-md-4 mb-4">
                    <div class="card">
                        <div class="card-header bg-light">
                            <h5 class="mb-0">Informasi Kampanye</h5>
                        </div>
                        <div class="card-body">
                            <h5><?= $kampanye->judul ?></h5>
                            
                            <?php 
                            $total_terkumpul = $kampanye->total_terkumpul ?? 0;
                            $persentase = hitungPersentase($total_terkumpul, $kampanye->target_donasi);
                            ?>
                            
                            <div class="progress my-3" 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="d-flex justify-content-between mb-3">
                                <span>Terkumpul: <?= rupiah($total_terkumpul) ?></span>
                                <span>Target: <?= rupiah($kampanye->target_donasi) ?></span>
                            </div>
                            
                            <div class="mb-3">
                                <p><?= potongTeks($kampanye->deskripsi, 200) ?></p>
                            </div>
                            
                            <div class="d-flex justify-content-between">
                                <span><i class="far fa-calendar-alt me-1"></i> Berakhir: <?= date('d/m/Y', strtotime($kampanye->tanggal_selesai)) ?></span>
                                <span><i class="fas fa-users me-1"></i> <?= $kampanye->jumlah_donatur ?? 0 ?> donatur</span>
                            </div>
                            
                            <div class="d-grid gap-2 mt-3">
                                <a href="<?= SITE_URL ?>/pages/kampanye-detail.php?id=<?= $kampanye->id ?>" class="btn btn-outline-primary btn-sm" target="_blank">
                                    <i class="fas fa-external-link-alt me-1"></i> Lihat Kampanye
                                </a>
                                
                                <?php if ($kampanye->status == 'aktif'): ?>
                                    <a href="<?= SITE_URL ?>/user/donasi/buat.php?kampanye_id=<?= $kampanye->id ?>" class="btn btn-success btn-sm">
                                        <i class="fas fa-plus me-1"></i> Donasi Lagi
                                    </a>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Riwayat Status -->
                    <div class="card mt-4">
                        <div class="card-header bg-light">
                            <h5 class="mb-0">Riwayat Status</h5>
                        </div>
                        <div class="card-body p-0">
                            <ul class="list-group list-group-flush">
                                <li class="list-group-item">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <div>
                                            <i class="fas fa-plus-circle text-success me-2"></i>
                                            <span>Donasi Dibuat</span>
                                        </div>
                                        <small class="text-muted"><?= date('d/m/Y H:i', strtotime($donasi->created_at)) ?></small>
                                    </div>
                                </li>
                                
                                <?php if (!empty($donasi->bukti_transfer)): ?>
                                    <li class="list-group-item">
                                        <div class="d-flex justify-content-between align-items-center">
                                            <div>
                                                <i class="fas fa-upload text-primary me-2"></i>
                                                <span>Bukti Transfer Diupload</span>
                                            </div>
                                            <small class="text-muted"><?= date('d/m/Y H:i', strtotime($donasi->updated_at)) ?></small>
                                        </div>
                                    </li>
                                <?php endif; ?>
                                
                                <?php if ($donasi->status == 'diverifikasi'): ?>
                                    <li class="list-group-item">
                                        <div class="d-flex justify-content-between align-items-center">
                                            <div>
                                                <i class="fas fa-check-circle text-success me-2"></i>
                                                <span>Donasi Diverifikasi</span>
                                            </div>
                                            <small class="text-muted"><?= date('d/m/Y H:i', strtotime($donasi->updated_at)) ?></small>
                                        </div>
                                    </li>
                                <?php elseif ($donasi->status == 'ditolak'): ?>
                                    <li class="list-group-item">
                                        <div class="d-flex justify-content-between align-items-center">
                                            <div>
                                                <i class="fas fa-times-circle text-danger me-2"></i>
                                                <span>Donasi Ditolak</span>
                                            </div>
                                            <small class="text-muted"><?= date('d/m/Y H:i', strtotime($donasi->updated_at)) ?></small>
                                        </div>
                                    </li>
                                <?php endif; ?>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </div>
</div>

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