<?php
// bendahara/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 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 donasi
if (!isset($_GET['id']) || empty($_GET['id'])) {
    setFlash('error', 'ID donasi tidak valid', 'danger');
    header('Location: ' . SITE_URL . '/bendahara/donasi/index.php');
    exit;
}

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

// Ambil detail donasi
$db->query("SELECT d.*, 
           k.judul as kampanye_judul, k.deskripsi as kampanye_deskripsi, k.gambar as kampanye_gambar,
           u.nama_lengkap as donatur_nama, u.email as donatur_email, u.no_telp as donatur_telp
           FROM donasi d
           JOIN kampanye k ON d.kampanye_id = k.id
           JOIN users u ON d.user_id = u.id
           WHERE d.id = :id");
$db->bind(':id', $id);
$donasi = $db->single();

if (!$donasi) {
    setFlash('error', 'Donasi tidak ditemukan', 'danger');
    header('Location: ' . SITE_URL . '/bendahara/donasi/index.php');
    exit;
}

// Ambil data admin verifikasi jika sudah diverifikasi
$admin_verifikasi = null;
if ($donasi->admin_verifikasi) {
    $db->query("SELECT nama_lengkap FROM users WHERE id = :id");
    $db->bind(':id', $donasi->admin_verifikasi);
    $admin_verifikasi = $db->single();
}

// Ambil data bendahara verifikasi jika sudah diverifikasi oleh bendahara
$bendahara_verifikasi = null;
if ($donasi->verified_by_bendahara) {
    $db->query("SELECT nama_lengkap FROM users WHERE id = :id");
    $db->bind(':id', $donasi->verified_by_bendahara);
    $bendahara_verifikasi = $db->single();
}

// Ambil log aktivitas donasi
$db->query("SELECT l.*, u.nama_lengkap, u.role 
           FROM logs l 
           JOIN users u ON l.user_id = u.id 
           WHERE l.entity_type = 'donasi' AND l.entity_id = :donasi_id
           ORDER BY l.created_at DESC 
           LIMIT 10");
$db->bind(':donasi_id', $id);
$logs = $db->resultSet();

// 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 active" 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" 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 Donasi</h1>
                <div class="btn-toolbar mb-2 mb-md-0">
                    <a href="<?= SITE_URL ?>/bendahara/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="card mb-4">
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-4 mb-4 mb-md-0">
                            <div class="text-center">
                                <img src="<?= SITE_URL ?>/assets/uploads/kampanye/<?= $donasi->kampanye_gambar ?? 'default.jpg' ?>" class="img-fluid rounded mb-3" alt="<?= $donasi->kampanye_judul ?>">
                                
                                <h5><?= $donasi->kampanye_judul ?></h5>
                                <p class="text-muted"><?= potongTeks($donasi->kampanye_deskripsi, 100) ?></p>
                            </div>
                            
                            <div class="mt-4">
                                <h6>Bukti Transfer</h6>
                                <?php if (!empty($donasi->bukti_transfer)): ?>
                                    <a href="<?= SITE_URL ?>/assets/uploads/bukti_transfer/<?= $donasi->bukti_transfer ?>" target="_blank">
                                        <img src="<?= SITE_URL ?>/assets/uploads/bukti_transfer/<?= $donasi->bukti_transfer ?>" class="img-fluid rounded" alt="Bukti Transfer">
                                    </a>
                                <?php else: ?>
                                    <div class="alert alert-warning">
                                        Bukti transfer belum diupload
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                        
                        <div class="col-md-8">
                            <div class="mb-4">
                                <?php if ($donasi->status == 'menunggu'): ?>
                                    <div class="alert alert-warning">
                                        <h5 class="alert-heading">Menunggu Verifikasi</h5>
                                        <p>Donasi ini menunggu verifikasi dari Anda. Periksa kesesuaian bukti transfer dan data donasi sebelum memverifikasi.</p>
                                        
                                        <div class="mt-3 d-flex">
                                            <button type="button" class="btn btn-success me-2" onclick="verifikasiDonasi(<?= $donasi->id ?>)">
                                                <i class="fas fa-check me-1"></i> Verifikasi Donasi
                                            </button>
                                            <button type="button" class="btn btn-danger" onclick="tolakDonasi(<?= $donasi->id ?>)">
                                                <i class="fas fa-times me-1"></i> Tolak Donasi
                                            </button>
                                        </div>
                                    </div>
                                <?php elseif ($donasi->status == 'diverifikasi'): ?>
                                    <div class="alert alert-success">
                                        <h5 class="alert-heading">Donasi Diverifikasi</h5>
                                        <p>
                                            Donasi ini telah diverifikasi 
                                            <?php if ($bendahara_verifikasi): ?>
                                                oleh <?= $bendahara_verifikasi->nama_lengkap ?> pada <?= date('d/m/Y H:i', strtotime($donasi->verified_at_bendahara)) ?>
                                            <?php endif; ?>
                                        </p>
                                    </div>
                                <?php else: ?>
                                    <div class="alert alert-danger">
                                        <h5 class="alert-heading">Donasi Ditolak</h5>
                                        <p>
                                            Donasi ini telah ditolak 
                                            <?php if ($bendahara_verifikasi): ?>
                                                oleh <?= $bendahara_verifikasi->nama_lengkap ?> pada <?= date('d/m/Y H:i', strtotime($donasi->verified_at_bendahara)) ?>
                                            <?php endif; ?>
                                        </p>
                                    </div>
                                <?php endif; ?>
                            </div>
                            
                            <div class="row mb-4">
                                <div class="col-md-6">
                                    <h5>Informasi Donasi</h5>
                                    <table class="table table-borderless">
                                        <tr>
                                            <td width="40%">Jumlah Donasi</td>
                                            <td width="60%"><strong><?= rupiah($donasi->jumlah) ?></strong></td>
                                        </tr>
                                        <tr>
                                            <td>Metode Pembayaran</td>
                                            <td><?= $donasi->metode_pembayaran ?></td>
                                        </tr>
                                        <tr>
                                            <td>Status</td>
                                            <td><?= getDonasiStatusBadge($donasi->status) ?></td>
                                        </tr>
                                        <tr>
                                            <td>Tanggal Donasi</td>
                                            <td><?= date('d/m/Y H:i', strtotime($donasi->created_at)) ?></td>
                                        </tr>
                                        <tr>
                                            <td>Keterangan</td>
                                            <td><?= $donasi->keterangan ?: '<i>Tidak ada keterangan</i>' ?></td>
                                        </tr>
                                        <tr>
                                            <td>Anonim</td>
                                            <td><?= $donasi->is_anonim ? 'Ya' : 'Tidak' ?></td>
                                        </tr>
                                    </table>
                                </div>
                                
                                <div class="col-md-6">
                                    <h5>Informasi Donatur</h5>
                                    <table class="table table-borderless">
                                        <tr>
                                            <td width="40%">Nama</td>
                                            <td width="60%"><strong><?= $donasi->is_anonim ? '<i>Anonim</i>' : $donasi->donatur_nama ?></strong></td>
                                        </tr>
                                        <?php if (!$donasi->is_anonim): ?>
                                            <tr>
                                                <td>Email</td>
                                                <td><?= $donasi->donatur_email ?></td>
                                            </tr>
                                            <tr>
                                                <td>Telepon</td>
                                                <td><?= $donasi->donatur_telp ?: '<i>Tidak ada</i>' ?></td>
                                            </tr>
                                        <?php endif; ?>
                                    </table>
                                </div>
                            </div>
                            
                            <div class="mb-4">
                                <h5>Riwayat Aktivitas</h5>
                                <?php if (empty($logs)): ?>
                                    <div class="alert alert-info">
                                        Belum ada aktivitas untuk donasi ini.
                                    </div>
                                <?php else: ?>
                                    <div class="table-responsive">
                                        <table class="table table-hover">
                                            <thead>
                                                <tr>
                                                    <th>Tanggal</th>
                                                    <th>Aktivitas</th>
                                                    <th>User</th>
                                                    <th>Deskripsi</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php foreach ($logs as $log): ?>
                                                    <tr>
                                                        <td><?= date('d/m/Y H:i', strtotime($log->created_at)) ?></td>
                                                        <td><?= $log->activity ?></td>
                                                        <td><?= $log->nama_lengkap ?> <small>(<?= getRoleName($log->role) ?>)</small></td>
                                                        <td><?= $log->description ?></td>
                                                    </tr>
                                                <?php endforeach; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </div>
</div>

<!-- Modal Verifikasi -->
<div class="modal fade" id="verifikasiModal" tabindex="-1" aria-labelledby="verifikasiModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-success text-white">
                <h5 class="modal-title" id="verifikasiModalLabel">Verifikasi Donasi</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="<?= SITE_URL ?>/bendahara/donasi/proses.php" method="post">
                <div class="modal-body">
                    <p>Anda yakin ingin memverifikasi donasi ini?</p>
                    <p>Pastikan dana sudah masuk ke rekening yayasan.</p>
                    
                    <div class="mb-3">
                        <label for="keterangan" class="form-label">Keterangan (opsional)</label>
                        <textarea class="form-control" id="keterangan" name="keterangan" rows="3"></textarea>
                    </div>
                    
                    <input type="hidden" name="donasi_id" id="verifikasi_donasi_id" value="<?= $donasi->id ?>">
                    <input type="hidden" name="action" value="verifikasi">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-success">Verifikasi Donasi</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Modal Tolak -->
<div class="modal fade" id="tolakModal" tabindex="-1" aria-labelledby="tolakModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-danger text-white">
                <h5 class="modal-title" id="tolakModalLabel">Tolak Donasi</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="<?= SITE_URL ?>/bendahara/donasi/proses.php" method="post">
                <div class="modal-body">
                    <p>Anda yakin ingin menolak donasi ini?</p>
                    
                    <div class="mb-3">
                        <label for="alasan_tolak" class="form-label">Alasan Penolakan <span class="text-danger">*</span></label>
                        <textarea class="form-control" id="alasan_tolak" name="keterangan" rows="3" required></textarea>
                    </div>
                    
                    <input type="hidden" name="donasi_id" id="tolak_donasi_id" value="<?= $donasi->id ?>">
                    <input type="hidden" name="action" value="tolak">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-danger">Tolak Donasi</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    // Fungsi untuk verifikasi donasi
    function verifikasiDonasi(id) {
        var verifikasiModal = new bootstrap.Modal(document.getElementById('verifikasiModal'));
        verifikasiModal.show();
    }
    
    // Fungsi untuk tolak donasi
    function tolakDonasi(id) {
        var tolakModal = new bootstrap.Modal(document.getElementById('tolakModal'));
        tolakModal.show();
    }
</script>

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