<?php
// admin/transactions.php
session_start();
include '../config.php';
include 'auth_check.php';

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Transactions - SaniBook Admin</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" />
</head>

<body class="bg-slate-100 font-sans text-slate-900">
    <?php include 'navbar.php'; ?>

    <div class="max-w-7xl mx-auto p-6">
        <h2 class="text-2xl font-bold text-slate-800 mb-6">Token Transactions</h2>

        <div class="bg-white rounded-xl shadow-sm overflow-hidden border border-slate-200">
            <div class="overflow-x-auto">
                <table class="w-full text-left border-collapse">
                    <thead class="bg-slate-50 text-slate-500 uppercase text-xs font-bold tracking-wider">
                        <tr>
                            <th class="p-4 border-b">Date</th>
                            <th class="p-4 border-b">User</th>
                            <th class="p-4 border-b">Description</th>
                            <th class="p-4 border-b">Type</th>
                            <th class="p-4 border-b text-right">Amount</th>
                            <th class="p-4 border-b text-right">Ref</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-100">
                        <?php
                        $sql = "SELECT t.*, u.email, u.org_name 
                                FROM token_transactions t 
                                JOIN users u ON t.user_id = u.id 
                                ORDER BY t.created_at DESC LIMIT 100";
                        $result = $conn->query($sql);

                        if ($result && $result->num_rows > 0):
                            while ($row = $result->fetch_assoc()):
                                ?>
                                <tr class="hover:bg-slate-50">
                                    <td class="p-4 text-sm text-slate-500 whitespace-nowrap">
                                        <?= date('M j, Y H:i', strtotime($row['created_at'])) ?>
                                    </td>
                                    <td class="p-4 text-sm font-medium text-slate-800">
                                        <?= htmlspecialchars($row['org_name']) ?>
                                        <div class="text-xs text-slate-400">
                                            <?= htmlspecialchars($row['email']) ?>
                                        </div>
                                    </td>
                                    <td class="p-4 text-sm text-slate-600">
                                        <?= htmlspecialchars($row['description']) ?>
                                    </td>
                                    <td class="p-4">
                                        <span
                                            class="text-xs font-bold uppercase px-2 py-1 rounded-full 
                                    <?= $row['transaction_type'] == 'credit' ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700' ?>">
                                            <?= $row['transaction_type'] ?>
                                        </span>
                                    </td>
                                    <td class="p-4 text-right font-mono text-sm font-bold text-slate-700">
                                        <?= number_format($row['amount']) ?>
                                    </td>
                                    <td class="p-4 text-right text-xs text-slate-400 font-mono">
                                        <?= htmlspecialchars($row['reference_id'] ?? '-') ?>
                                    </td>
                                </tr>
                            <?php endwhile; else: ?>
                            <tr>
                                <td colspan="6" class="p-8 text-center text-slate-400">No transactions found.</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</body>

</html>
