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

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

$stmt = $pdo->prepare("SELECT * FROM partner_shops WHERE user_id = ? LIMIT 1");
$stmt->execute([$_SESSION["user_id"]]);
$shop = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$shop) {
    die("No partner shop connected.");
}

$stmt = $pdo->prepare("
    SELECT wt.*, c.customer_code, c.full_name
    FROM wallet_transactions wt
    JOIN customers c ON wt.customer_id = c.id
    WHERE wt.partner_shop_id = ?
    ORDER BY wt.id DESC
");
$stmt->execute([$shop["id"]]);
$transactions = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

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

        .container {
            max-width: 1200px;
            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;
        }

        .info-box {
            background: #eff6ff;
            color: #1e3a8a;
            padding: 14px;
            border-radius: 10px;
            margin-bottom: 20px;
        }

        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: 18px;
            border-radius: 10px;
            color: #6b7280;
        }
    </style>
</head>
<body>

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

        <h1>Top Up History</h1>
        <p class="subtitle">Customer wallet top-ups made by your partner shop.</p>

        <div class="info-box">
            <strong>Partner Shop:</strong>
            <?php echo htmlspecialchars($shop["shop_name"]); ?>
        </div>

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

            <div class="empty">
                No top-up transactions found.
            </div>

        <?php else: ?>

            <table>
                <tr>
                    <th>Code</th>
                    <th>Customer</th>
                    <th>Customer Code</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 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>

</body>
</html>