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

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

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

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

// Ambil riwayat log untuk donasi ini
$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");
$db->bind(':donasi_id', $id);
$logs = $db->resultSet();

// Flag donasi sebagai diperiksa dalam log jika belum pernah diperiksa oleh pengawas
$db->query("SELECT COUNT(*) as count FROM logs 
           WHERE entity_type = 'donasi' AND entity_id = :donasi_id 
           AND activity = 'Audit donasi' AND user_id = :user_id");
$db->bind(':donasi_id', $id);
$db->bind(':user_id', $_SESSION['user_id']);
$already_audited = $db->single()->count > 0;

if (!$already_audited) {
    // Log aktivitas
    logActivity('Audit donasi', 'donasi', $id, 'Pengawas memeriksa detail transaksi donasi ID ' . $id);
}

// 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 ?>/pengawas/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 ?>/pengawas/audit/index.php">
                            <i class="fas fa-search-dollar me-2"></i>
                            Audit Transaksi
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/pengawas/laporan/index.php">
                            <i class="fas fa-file-alt me-2"></i>
                            Laporan Keuangan
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/pengawas/logs/index.php">
                            <i class="fas fa-history me-2"></i>
                            Log Aktivitas
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/pengawas/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 Audit Transaksi</h1>
                <div class="btn-toolbar mb-2 mb-md-0">
                    <a href="<?= SITE_URL ?>/pengawas/audit/index.php" class="btn btn-sm btn-outline-secondary">
                        <i class="fas fa-arrow-left me-1"></i> Kembali
                    </a>
                </div>
            </div>

            <?php flash(); ?>

            <!-- Detail Transaksi -->
            <div class="card mb-4">
                <div class="card-header bg-white">
                    <h5 class="card-title mb-0">Informasi Transaksi</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label fw-bold">ID Transaksi</label>
                                <p>#<?= $donasi->id ?></p>
                            </div>
                            <div class="mb-3">
                                <label class="form-label fw-bold">Tanggal Donasi</label>
                                <p><?= date('d F Y H:i', strtotime($donasi->created_at)) ?></p>
                            </div>
                            <div class="mb-3">
                                <label class="form-label fw-bold">Kampanye</label>
                                <p><?= $donasi->kampanye_judul ?></p>
                            </div>
                            <div class="mb-3">
                                <label class="form-label fw-bold">Jumlah Donasi</label>
                                <p class="text-primary fw-bold"><?= rupiah($donasi->jumlah) ?></p>
                            </div>
                            <div class="mb-3">
                                <label class="form-label fw-bold">Status</label>
                                <p>
                                    <?php if ($donasi->status == 'menunggu'): ?>
                                        <span class="badge bg-warning text-dark">Menunggu Verifikasi</span>
                                    <?php elseif ($donasi->status == 'diverifikasi'): ?>
                                        <span class="badge bg-success">Diverifikasi</span>
                                    <?php else: ?>
                                        <span class="badge bg-danger">Ditolak</span>
                                    <?php endif; ?>
                                </p>
                            </div>
                            <div class="mb-3">
                                <label class="form-label fw-bold">Metode Pembayaran</label>
                                <p><?= $donasi->metode_pembayaran ?></p>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label fw-bold">Donatur</label>
                                <p><?= $donasi->is_anonim ? 'Anonim' : $donasi->donatur_nama ?></p>
                            </div>
                            <?php if (!$donasi->is_anonim): ?>
                                <div class="mb-3">
                                    <label class="form-label fw-bold">Email Donatur</label>
                                    <p><?= $donasi->donatur_email ?></p>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label fw-bold">Telepon Donatur</label>
                                    <p><?= $donasi->donatur_telp ?? '-' ?></p>
                                </div>
                            <?php endif; ?>
                            <div class="mb-3">
                                <label class="form-label fw-bold">Keterangan</label>
                                <p><?= $donasi->keterangan ?? '-' ?></p>
                            </div>
                            <div class="mb-3">
                                <label class="form-label fw-bold">Diverifikasi oleh Admin</label>
                                <p><?= $donasi->admin_nama ?? 'Belum diverifikasi' ?></p>
                                <?php if ($donasi->admin_verifikasi): ?>
                                    <p class="text-muted small">Pada: <?= date('d F Y H:i', strtotime($donasi->updated_at)) ?></p>
                                <?php endif; ?>
                            </div>
                            <div class="mb-3">
                                <label class="form-label fw-bold">Diverifikasi oleh Bendahara</label>
                                <p><?= $donasi->bendahara_nama ?? 'Belum diverifikasi' ?></p>
                                <?php if ($donasi->verified_by_bendahara): ?>
                                    <p class="text-muted small">Pada: <?= date('d F Y H:i', strtotime($donasi->verified_at_bendahara)) ?></p>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Bukti Transfer -->
            <div class="card mb-4">
                <div class="card-header bg-white">
                    <h5 class="card-title mb-0">Bukti Transfer</h5>
                </div>
                <div class="card-body text-center">
                    <?php if ($donasi->bukti_transfer): ?>
                        <img src="<?= SITE_URL ?>/assets/uploads/bukti_transfer/<?= $donasi->bukti_transfer ?>" alt="Bukti Transfer" class="img-fluid" style="max-height: 400px;">
                    <?php else: ?>
                        <div class="alert alert-warning">
                            <i class="fas fa-exclamation-triangle me-2"></i> Bukti transfer belum diupload
                        </div>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Riwayat Aktivitas Donasi -->
            <div class="card">
                <div class="card-header bg-white">
                    <h5 class="card-title mb-0">Riwayat Aktivitas</h5>
                </div>
                <div class="card-body">
                    <?php if (empty($logs)): ?>
                        <p class="text-center py-3">Belum ada riwayat aktivitas untuk donasi ini</p>
                    <?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 ?>
                                                <span class="badge bg-secondary"><?= getRoleName($log->role) ?></span>
                                            </td>
                                            <td><?= $log->description ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Tombol Tindakan -->
            <div class="mt-4 text-center">
                <a href="<?= SITE_URL ?>/pengawas/audit/index.php" class="btn btn-outline-secondary me-2">
                    <i class="fas fa-arrow-left me-1"></i> Kembali ke Daftar
                </a>
                <a href="<?= SITE_URL ?>/pengawas/audit/export.php?id=<?= $id ?>&type=pdf" class="btn btn-danger">
                    <i class="fas fa-file-pdf me-1"></i> Export PDF
                </a>
            </div>
        </main>
    </div>
</div>

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