<?php
// user/dashboard.php
$pageTitle = 'Dashboard Donatur';
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;
}

// Ambil data untuk dashboard
$db = dbConnect();
$user_id = $_SESSION['user_id'];

// Total donasi yang telah dilakukan
$db->query("SELECT SUM(jumlah) as total_donasi FROM donasi WHERE user_id = :user_id AND status = 'diverifikasi'");
$db->bind(':user_id', $user_id);
$total_donasi = $db->single();
$total_donasi_value = $total_donasi->total_donasi ?? 0;

// Jumlah donasi yang telah dilakukan
$db->query("SELECT COUNT(*) as count FROM donasi WHERE user_id = :user_id AND status = 'diverifikasi'");
$db->bind(':user_id', $user_id);
$jumlah_donasi = $db->single();
$jumlah_donasi_value = $jumlah_donasi->count ?? 0;

// Jumlah kampanye yang telah didonasikan
$db->query("SELECT COUNT(DISTINCT kampanye_id) as count FROM donasi WHERE user_id = :user_id AND status = 'diverifikasi'");
$db->bind(':user_id', $user_id);
$jumlah_kampanye = $db->single();
$jumlah_kampanye_value = $jumlah_kampanye->count ?? 0;

// Donasi terbaru
$db->query("SELECT d.*, k.judul as kampanye_judul 
           FROM donasi d 
           JOIN kampanye k ON d.kampanye_id = k.id 
           WHERE d.user_id = :user_id 
           ORDER BY d.created_at DESC 
           LIMIT 5");
$db->bind(':user_id', $user_id);
$donasi_recent = $db->resultSet();

// 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_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.status = 'aktif'
            ORDER BY k.created_at DESC 
            LIMIT 3");
$kampanye_aktif = $db->resultSet();

// Ambil notifikasi untuk user
$db->query("SELECT * FROM notifications WHERE user_id = :user_id AND is_read = 0 ORDER BY created_at DESC LIMIT 5");
$db->bind(':user_id', $user_id);
$notifikasi = $db->resultSet();

// 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 active" 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" 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">Dashboard Donatur</h1>
                <div class="btn-toolbar mb-2 mb-md-0">
                    <a href="<?= SITE_URL ?>/user/donasi/buat.php" class="btn btn-sm btn-success">
                        <i class="fas fa-plus me-1"></i> Donasi Baru
                    </a>
                </div>
            </div>

            <?php flash(); ?>

            <!-- Pesan Selamat Datang -->
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <h4 class="alert-heading">Selamat Datang, <?= $_SESSION['nama_lengkap'] ?>!</h4>
                <p>Terima kasih telah berpartisipasi dalam kegiatan donasi. Dengan berdonasi, Anda telah membantu mereka yang membutuhkan.</p>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>

            <!-- Notifikasi -->
            <?php if (!empty($notifikasi)): ?>
                <div class="card mb-4">
                    <div class="card-header bg-light">
                        <h5 class="mb-0"><i class="fas fa-bell me-2"></i> Notifikasi</h5>
                    </div>
                    <div class="card-body p-0">
                        <div class="list-group list-group-flush">
                            <?php foreach ($notifikasi as $notif): ?>
                                <a href="<?= $notif->link ?? SITE_URL . '/user/notifikasi/detail.php?id=' . $notif->id ?>" class="list-group-item list-group-item-action">
                                    <div class="d-flex w-100 justify-content-between">
                                        <h6 class="mb-1"><?= $notif->title ?></h6>
                                        <small><?= date('d/m/Y H:i', strtotime($notif->created_at)) ?></small>
                                    </div>
                                    <p class="mb-1"><?= $notif->message ?></p>
                                </a>
                            <?php endforeach; ?>
                        </div>
                    </div>
                    <?php if (count($notifikasi) > 0): ?>
                        <div class="card-footer text-center">
                            <a href="<?= SITE_URL ?>/user/notifikasi/index.php" class="text-decoration-none">Lihat Semua Notifikasi</a>
                        </div>
                    <?php endif; ?>
                </div>
            <?php endif; ?>

            <!-- Statistik Donasi -->
            <div class="row dashboard-stats">
                <div class="col-md-4 mb-4">
                    <div class="card h-100">
                        <div class="card-body">
                            <div class="d-flex align-items-center">
                                <div class="card-icon rounded-circle d-flex align-items-center justify-content-center me-3">
                                    <i class="fas fa-money-bill-wave"></i>
                                </div>
                                <div>
                                    <h5 class="card-title">Total Donasi</h5>
                                    <h3 class="mb-0"><?= rupiah($total_donasi_value) ?></h3>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 mb-4">
                    <div class="card h-100">
                        <div class="card-body">
                            <div class="d-flex align-items-center">
                                <div class="card-icon rounded-circle d-flex align-items-center justify-content-center me-3">
                                    <i class="fas fa-gift"></i>
                                </div>
                                <div>
                                    <h5 class="card-title">Jumlah Donasi</h5>
                                    <h3 class="mb-0"><?= $jumlah_donasi_value ?> kali</h3>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 mb-4">
                    <div class="card h-100">
                        <div class="card-body">
                            <div class="d-flex align-items-center">
                                <div class="card-icon rounded-circle d-flex align-items-center justify-content-center me-3">
                                    <i class="fas fa-project-diagram"></i>
                                </div>
                                <div>
                                    <h5 class="card-title">Kampanye Dibantu</h5>
                                    <h3 class="mb-0"><?= $jumlah_kampanye_value ?> kampanye</h3>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Donasi Terbaru & Kampanye Aktif -->
            <div class="row">
                <!-- Donasi Terbaru -->
                <div class="col-md-6 mb-4">
                    <div class="card">
                        <div class="card-header bg-light">
                            <h5 class="mb-0"><i class="fas fa-history me-2"></i> Donasi Terbaru Saya</h5>
                        </div>
                        <div class="card-body p-0">
                            <?php if (empty($donasi_recent)): ?>
                                <p class="text-center py-3">Belum ada riwayat donasi</p>
                            <?php else: ?>
                                <div class="table-responsive">
                                    <table class="table table-hover mb-0">
                                        <thead>
                                            <tr>
                                                <th>Kampanye</th>
                                                <th>Jumlah</th>
                                                <th>Status</th>
                                                <th>Tanggal</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php foreach ($donasi_recent as $donasi): ?>
                                                <tr>
                                                    <td><?= potongTeks($donasi->kampanye_judul, 30) ?></td>
                                                    <td><?= rupiah($donasi->jumlah) ?></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>
                                                    <td>
                                                        <small><?= date('d/m/Y', strtotime($donasi->created_at)) ?></small>
                                                    </td>
                                                </tr>
                                            <?php endforeach; ?>
                                        </tbody>
                                    </table>
                                </div>
                                <div class="card-footer text-center">
                                    <a href="<?= SITE_URL ?>/user/donasi/index.php" class="btn btn-sm btn-outline-primary">Lihat Semua Donasi</a>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>

                <!-- Kampanye Aktif -->
                <div class="col-md-6 mb-4">
                    <div class="card">
                        <div class="card-header bg-light">
                            <h5 class="mb-0"><i class="fas fa-hand-holding-heart me-2"></i> Kampanye Aktif</h5>
                        </div>
                        <div class="card-body">
                            <?php if (empty($kampanye_aktif)): ?>
                                <p class="text-center py-3">Tidak ada kampanye aktif saat ini</p>
                            <?php else: ?>
                                <div class="row">
                                    <?php foreach ($kampanye_aktif as $kampanye): ?>
                                        <?php 
                                        $total_terkumpul = $kampanye->total_terkumpul ?? 0;
                                        $persentase = hitungPersentase($total_terkumpul, $kampanye->target_donasi);
                                        ?>
                                        <div class="col-md-12 mb-3">
                                            <div class="card border-0 shadow-sm">
                                                <div class="row g-0">
                                                    <div class="col-md-4">
                                                        <img src="<?= SITE_URL ?>/assets/uploads/kampanye/<?= $kampanye->gambar ?? 'default.jpg' ?>" class="img-fluid rounded-start" alt="<?= $kampanye->judul ?>" style="height: 100%; object-fit: cover;">
                                                    </div>
                                                    <div class="col-md-8">
                                                        <div class="card-body">
                                                            <h5 class="card-title"><?= potongTeks($kampanye->judul, 40) ?></h5>
                                                            <div class="progress mt-2" style="height: 6px;">
                                                                <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 mt-2 mb-2">
                                                                <small><?= rupiah($total_terkumpul) ?> dari <?= rupiah($kampanye->target_donasi) ?></small>
                                                                <small><?= $persentase ?>%</small>
                                                            </div>
                                                            <div class="d-grid">
                                                                <a href="<?= SITE_URL ?>/user/donasi/buat.php?kampanye_id=<?= $kampanye->id ?>" class="btn btn-sm btn-success">Donasi Sekarang</a>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    <?php endforeach; ?>
                                </div>
                                <div class="text-center mt-3">
                                    <a href="<?= SITE_URL ?>/pages/kampanye.php" class="btn btn-sm btn-outline-primary">Lihat Semua Kampanye</a>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </div>
</div>

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