<?php
$pageTitle = 'Transactions';
require_once 'includes/header.php';

$transactions = $db->query("
    SELECT t.*, u.name as user_name, b.booking_id as booking_code, p.title as package_title
    FROM transactions t 
    JOIN users u ON t.user_id = u.id 
    JOIN bookings b ON t.booking_id = b.id 
    JOIN packages p ON b.package_id = p.id 
    ORDER BY t.created_at DESC
")->fetchAll();

$totalRevenue = $db->query("SELECT COALESCE(SUM(amount), 0) as total FROM transactions WHERE status = 'success'")->fetch()['total'];
?>

<div class="page-header">
    <h1><i class="fas fa-credit-card text-primary"></i> Transactions</h1>
    <div>
        <span style="background:rgba(16,185,129,0.1);color:#065F46;padding:8px 16px;border-radius:8px;font-weight:600;">
            Total Revenue: <?= formatCurrency($totalRevenue) ?>
        </span>
    </div>
</div>

<div class="admin-card">
    <div class="card-body" style="padding:0;">
        <div class="table-responsive">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>Transaction ID</th>
                        <th>Booking</th>
                        <th>Customer</th>
                        <th>Package</th>
                        <th>Amount</th>
                        <th>Method</th>
                        <th>Status</th>
                        <th>Date</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($transactions as $t): ?>
                        <tr>
                            <td><strong><?= $t['transaction_id'] ?></strong></td>
                            <td><?= $t['booking_code'] ?></td>
                            <td><?= sanitize($t['user_name']) ?></td>
                            <td><?= sanitize(truncateText($t['package_title'], 25)) ?></td>
                            <td><strong><?= formatCurrency($t['amount']) ?></strong></td>
                            <td><span style="text-transform:capitalize;"><?= $t['payment_method'] ?></span></td>
                            <td><span class="status-badge status-<?= $t['status'] ?>"><?= ucfirst($t['status']) ?></span></td>
                            <td><?= formatDate($t['created_at'], 'd M Y, h:i A') ?></td>
                            <td>
                                <div class="table-actions">
                                    <button class="action-btn print" title="Print" onclick="window.print()"><i class="fas fa-print"></i></button>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (empty($transactions)): ?>
                        <tr><td colspan="9" style="text-align:center;color:var(--gray);padding:30px;">No transactions found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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