<?php
session_start();
require_once '../config/database.php';

if (!isset($_SESSION["user_id"]) || $_SESSION["role"] !== "admin") {
    header("Location: ../auth/login.php");
    exit;
}

$stmt = $pdo->query("
    SELECT wt.*, c.full_name, c.customer_code, ps.shop_name
    FROM wallet_transactions wt
    JOIN customers c ON wt.customer_id = c.id
    JOIN partner_shops ps ON wt.partner_shop_id = ps.id
    ORDER BY wt.id DESC
");
$transactions = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html>
<head>
    <title>Wallet Transactions</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f4f6f8;
            margin: 0;
            padding: 30px;
        }

        .container {
            max-width: 1400px;
            margin: auto;
        }

        .card {
            background: white;
            border-radius: 14px;
            padding: 24px;
            box-shadow: 0 4px 14px rgba(0,0,0,0.08);
        }

        h1 {
            margin-top: 0;
            color: #111827;
        }

        .subtitle {
            color: #6b7280;
            margin-bottom: 22px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th {
            background: #111827;
            color: white;
            text-align: left;
            padding: 12px;
        }

        td {
            padding: 12px;
            border-bottom: 1px solid #e5e7eb;
        }

        tr:hover {
            background: #f9fafb;
        }

        .amount {
            color: #2563eb;
            font-weight: bold;
        }

        .type {
            background: #dbeafe;
            color: #1d4ed8;
            padding: 6px 10px;
            border-radius: 999px;
            font-size: 13px;
            font-weight: bold;
            display: inline-block;
        }

        .btn {
            padding: 10px 16px;
            border-radius: 8px;
            text-decoration: none;
            display: inline-block;
        }

        .btn-gray {
            background: #6b7280;
            color: white;
        }

        .footer {
            margin-top: 20px;
        }

        .empty {
            background: #f9fafb;
            padding: 20px;
            border-radius: 10px;
            color: #6b7280;
        }
    </style>
</head>
<body>

<div class="container">
    <div class="card">

        <h1>Wallet Transactions</h1>
        <p class="subtitle">Partner to customer wallet top-up history.</p>

        <?php if (count($transactions) === 0): ?>

            <div class="empty">
                No wallet transactions found.
            </div>

        <?php else: ?>

            <table>
                <tr>
                    <th>Code</th>
                    <th>Customer</th>
                    <th>Customer Code</th>
                    <th>Partner Shop</th>
                    <th>Amount</th>
                    <th>Type</th>
                    <th>Date</th>
                </tr>

                <?php foreach ($transactions as $row): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($row["transaction_code"]); ?></td>
                        <td><?php echo htmlspecialchars($row["full_name"]); ?></td>
                        <td><?php echo htmlspecialchars($row["customer_code"]); ?></td>
                        <td><?php echo htmlspecialchars($row["shop_name"]); ?></td>
                        <td class="amount"><?php echo number_format($row["amount"], 2); ?></td>
                        <td>
                            <span class="type">
                                <?php echo ucwords(str_replace("_", " ", htmlspecialchars($row["transaction_type"]))); ?>
                            </span>
                        </td>
                        <td><?php echo $row["created_at"]; ?></td>
                    </tr>
                <?php endforeach; ?>
            </table>

        <?php endif; ?>

        <div class="footer">
            <a class="btn btn-gray" href="dashboard.php">Back to Dashboard</a>
        </div>

    </div>
</div>

<?php require __DIR__ . '/../config/auto_update.php'; ?>
</body>
</html>