<?php
session_start();

$order_id = isset($_GET['order_id']) ? (int)$_GET['order_id'] : 0;

if ($order_id <= 0) {
    header('Location: index.php');
    exit;
}

require __DIR__ . '/db_config.php';

// Get order details
$order = null;
$orderItems = [];

$stmt = $conn->prepare("SELECT * FROM orders WHERE id = ?");
$stmt->bind_param('i', $order_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result && $result->num_rows > 0) {
    $order = $result->fetch_assoc();
    
    // Get order items
    $itemsStmt = $conn->prepare("SELECT * FROM order_items WHERE order_id = ?");
    $itemsStmt->bind_param('i', $order_id);
    $itemsStmt->execute();
    $itemsRes = $itemsStmt->get_result();
    while ($item = $itemsRes->fetch_assoc()) {
        $orderItems[] = $item;
    }
    $itemsStmt->close();
}
$stmt->close();
$conn->close();

if (!$order) {
    header('Location: index.php');
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Order Confirmation</title>
    <link rel="stylesheet" href="styles.css">
    <style>
        .confirmation-container {
            max-width: 600px;
            margin: 40px auto;
            background: #fff;
            padding: 40px;
            border-radius: 8px;
            box-shadow: 0 2px 16px rgba(0,0,0,0.1);
            text-align: center;
        }
        
        .success-icon {
            font-size: 64px;
            margin-bottom: 16px;
        }
        
        .order-number {
            background: #e3f2fd;
            color: #e3a2e0ff;
            padding: 16px;
            border-radius: 4px;
            margin: 24px 0;
            font-size: 18px;
            font-weight: 600;
        }
        
        .order-details {
            text-align: left;
            background: #f5f5f5;
            padding: 20px;
            border-radius: 4px;
            margin: 24px 0;
        }
        
        .detail-row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 12px;
            font-size: 14px;
        }
        
        .detail-row strong {
            color: #333;
        }
        
        .detail-row span {
            color: #666;
        }
        
        .items-list {
            background: #f5f5f5;
            padding: 20px;
            border-radius: 4px;
            margin: 24px 0;
            text-align: left;
        }
        
        .item-row {
            display: flex;
            justify-content: space-between;
            padding: 8px 0;
            border-bottom: 1px solid #eee;
            font-size: 14px;
        }
        
        .item-row:last-child {
            border-bottom: none;
        }
        
        .item-total {
            font-weight: 600;
            color: #e19dcbff;
        }
        
        .order-total {
            font-size: 20px;
            font-weight: 600;
            color: #333;
            margin-top: 16px;
            padding-top: 16px;
            border-top: 2px solid #ddd;
        }
        
        .btn-group {
            display: flex;
            gap: 12px;
            margin-top: 32px;
            justify-content: center;
        }
        
        .btn-group a {
            display: inline-block;
            padding: 12px 24px;
            border-radius: 4px;
            text-decoration: none;
            font-weight: 500;
            transition: background 0.2s;
        }
        
        .btn-home {
            background: #667eea;
            color: #fff;
        }
        
        .btn-home:hover {
            background: #5568d3;
        }
        
        .btn-shop {
            background: #f0f0f0;
            color: #333;
        }
        
        .btn-shop:hover {
            background: #e0e0e0;
        }
    </style>
</head>
<body>
<?php include __DIR__ . '/header.php'; ?>

<main class="container">
    <div class="confirmation-container">
        <div class="success-icon"></div>
        <h1>Order Confirmed!</h1>
        <p style="color: #666; margin-bottom: 24px;">Thank you for your purchase. Your order has been placed successfully.</p>
        
        <div class="order-number">Order #<?php echo $order['id']; ?></div>
        
        <div class="order-details">
            <div class="detail-row">
                <strong>Customer:</strong>
                <span><?php echo htmlspecialchars($order['user_name']); ?></span>
            </div>
            <div class="detail-row">
                <strong>Email:</strong>
                <span><?php echo htmlspecialchars($order['user_email']); ?></span>
            </div>
            <div class="detail-row">
                <strong>Order Date:</strong>
                <span><?php echo date('M d, Y H:i', strtotime($order['order_date'])); ?></span>
            </div>
            <div class="detail-row">
                <strong>Payment Method:</strong>
                <span><?php echo ucfirst(str_replace('_', ' ', $order['payment_method'])); ?></span>
            </div>
            <div class="detail-row">
                <strong>Shipping Address:</strong>
                <span><?php echo htmlspecialchars($order['shipping_address']); ?></span>
            </div>
        </div>
        
        <div class="items-list">
            <h3 style="margin-bottom: 16px; margin-top: 0;">Order Items</h3>
            <?php foreach ($orderItems as $item): ?>
            <div class="item-row">
                <div>
                    <strong><?php echo htmlspecialchars($item['product_title']); ?></strong><br>
                    <span style="font-size: 12px; color: #999;">Qty: <?php echo $item['quantity']; ?> × $<?php echo number_format($item['product_price'], 2); ?></span>
                </div>
                <span class="item-total">$<?php echo number_format($item['subtotal'], 2); ?></span>
            </div>
            <?php endforeach; ?>
            
            <div class="order-total">
                Total: $<?php echo number_format($order['total_price'], 2); ?>
            </div>
        </div>
        
        <p style="color: #666; font-size: 14px; margin: 24px 0;">
            An order confirmation email has been sent to <strong><?php echo htmlspecialchars($order['user_email']); ?></strong>
        </p>
        
        <div class="btn-group">
            <a href="index.php" class="btn-home">Continue Shopping</a>
            <a href="dashboard.php" class="btn-shop">View Account</a>
        </div>
    </div>
</main>

<?php include __DIR__ . '/footer.php'; ?>
</body>
</html>
