<?php 
session_start(); 
include 'config.php';

if(!isset($_SESSION['mc_username'])) { 
    header("Location: login.php"); 
    exit; 
}

$cart = json_decode($_POST['cart'] ?? '[]', true);
if(empty($cart)) { 
    header("Location: index.php"); 
    exit; 
}

$total = 0;
foreach($cart as $item) $total += $item['price'] * $item['qty'];
?>
<!DOCTYPE html>
<html>
<head>
    <title>Dummy Checkout • Reckon Store</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        body{background:#0a0a1a;color:#0f0;font-family:courier;text-align:center;padding:4rem}
        .box{background:#111;border:3px solid #0f0;padding:3rem;border-radius:20px;max-width:600px;margin:2rem auto;box-shadow:0 0 50px #0f03}
        .btn{background:#0f0;color:#000;padding:1.5rem 4rem;font-size:2rem;font-weight:bold;border:none;border-radius:20px;cursor:pointer;margin:2rem}
        table{margin:2rem auto;width:90%;border-collapse:collapse;color:#0f0}
        th,td{border:1px solid #0f0;padding:1rem;text-align:left}
        th{background:#000;color:#0f0}
    </style>
</head>
<body>
<div class="box">
    <h1>CHECKOUT</h1>
    <p>Logged in as: <strong><?=htmlspecialchars($_SESSION['mc_username'])?></strong></p>
    
    <table>
        <tr><th>Item</th><th>Qty</th><th>Price</th><th>Total</th></tr>
        <?php foreach($cart as $item): 
            $itemTotal = $item['price'] * $item['qty'];
        ?>
        <tr>
            <td><?=htmlspecialchars($item['name'])?></td>
            <td><?=$item['qty']?></td>
            <td>$<?=$item['price']?></td>
            <td>$<?=number_format($itemTotal,2)?></td>
        </tr>
        <?php endforeach; ?>
        <tr><th colspan="3">GRAND TOTAL</th><th>$<?=number_format($total,2)?></th></tr>
    </table>

    <!-- FIXED FORM — NOW SENDS CART DATA CORRECTLY -->
    <form method="POST" action="dummy-success.php">
        <input type="hidden" name="cart" value='<?=json_encode($cart)?>'>
        <button type="submit" class="btn">
            PAY $<?=number_format($total,2)?> NOW
        </button>
    </form>

    <br><br>
    <small>Dummy checkout — click the button → instant success!</small>
</div>
</body>
</html>