<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
include "config/db.php";

if (!isset($_GET['order_id']) || empty($_GET['order_id'])) {
    header("Location: index.php");
    exit();
}

$order_id = mysqli_real_escape_string($conn, $_GET['order_id']);
$order_query = mysqli_query($conn, 
    "SELECT o.*, u.name as user_name, u.email, u.phone 
     FROM orders o 
     JOIN users u ON o.user_id = u.id 
     WHERE o.id = '$order_id'"
);

if (mysqli_num_rows($order_query) == 0) {
    header("Location: index.php");
    exit();
}

$order = mysqli_fetch_assoc($order_query);

// Get order items
$items_query = mysqli_query($conn, 
    "SELECT * FROM order_items WHERE order_id = '$order_id'"
);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Order Confirmed - ShopNest Rentals</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        .success-icon {
            font-size: 5rem;
            color: #28a745;
        }
        .order-card {
            border-left: 5px solid #28a745;
            background: white;
            border-radius: 10px;
            padding: 30px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.05);
        }
        .timeline {
            position: relative;
            padding-left: 30px;
            margin-top: 30px;
        }
        .timeline::before {
            content: '';
            position: absolute;
            left: 10px;
            top: 0;
            bottom: 0;
            width: 2px;
            background: #28a745;
        }
        .timeline-step {
            position: relative;
            margin-bottom: 30px;
        }
        .timeline-step::before {
            content: '';
            position: absolute;
            left: -30px;
            top: 5px;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background: #28a745;
            border: 3px solid white;
            box-shadow: 0 0 0 3px #28a745;
        }
    </style>
</head>
<body>
    <?php include "includes/header.php"; ?>

    <div class="container mt-4">
        <div class="text-center py-5">
            <div class="success-icon mb-4">
                <i class="fas fa-check-circle"></i>
            </div>
            
            <h1 class="mb-3" style="color: #28a745;">Rental Confirmed!</h1>
            <p class="lead mb-4">Your designer outfit rental has been successfully booked.</p>
            
            <div class="row justify-content-center">
                <div class="col-md-8">
                    <div class="order-card mb-4">
                        <div class="text-center mb-4">
                            <h3>Order #<?php echo str_pad($order['id'], 6, '0', STR_PAD_LEFT); ?></h3>
                            <p class="text-muted">Placed on <?php echo date('F j, Y', strtotime($order['created_at'])); ?></p>
                        </div>
                        
                        <div class="row mb-4">
                            <div class="col-md-6">
                                <h6><i class="fas fa-user me-2"></i>Customer</h6>
                                <p class="mb-1"><?php echo htmlspecialchars($order['user_name']); ?></p>
                                <p class="mb-1"><?php echo htmlspecialchars($order['email']); ?></p>
                                <p class="mb-0"><?php echo htmlspecialchars($order['phone']); ?></p>
                            </div>
                            <div class="col-md-6">
                                <h6><i class="fas fa-map-marker-alt me-2"></i>Delivery Address</h6>
                                <p class="mb-0 text-break"><?php echo nl2br(htmlspecialchars($order['shipping_address'])); ?></p>
                            </div>
                        </div>

                        <!-- Payment Info -->
                        <div class="mb-4 p-3 rounded" style="background:#f8f9fa;">
                            <h6><i class="fas fa-credit-card me-2"></i>Payment Info</h6>
                            <?php
                                $pm = isset($order['payment_method']) ? $order['payment_method'] : 'razorpay';
                                $ps = isset($order['payment_status']) ? $order['payment_status'] : 'Pending';
                                
                                $badges = [
                                    'Paid'  => 'bg-success',
                                    'COD'   => 'bg-warning text-dark',
                                    'Failed'=> 'bg-danger'
                                ];
                                $badge_class = isset($badges[$ps]) ? $badges[$ps] : 'bg-secondary';
                                
                                $pm_labels = ['razorpay'=>'Razorpay', 'upi'=>'UPI', 'cod'=>'Cash on Delivery'];
                                $pm_label  = isset($pm_labels[$pm]) ? $pm_labels[$pm] : ucfirst($pm);
                            ?>
                            <div class="d-flex align-items-center gap-2 flex-wrap">
                                <span class="badge <?php echo $badge_class; ?> fs-6">
                                    <?php echo $ps === 'Paid' ? '<i class="fas fa-check-circle me-1"></i>' : ''; ?>
                                    <?php echo htmlspecialchars($ps); ?>
                                </span>
                                <span class="text-muted"><?php echo htmlspecialchars($pm_label); ?></span>
                            </div>
                            <?php if (!empty($order['payment_id'])): ?>
                            <small class="text-muted d-block mt-1">
                                Transaction ID: <code><?php echo htmlspecialchars($order['payment_id']); ?></code>
                            </small>
                            <?php endif; ?>
                        </div>
                        
                        <h6><i class="fas fa-tshirt me-2"></i>Items Rented</h6>
                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>Item</th>
                                        <th>Price/Day</th>
                                        <th>Qty</th>
                                        <th>Total</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php while($item = mysqli_fetch_assoc($items_query)): ?>
                                    <tr>
                                        <td><?php echo htmlspecialchars($item['product_name']); ?></td>
                                        <td>₹<?php echo number_format($item['price'], 2); ?></td>
                                        <td><?php echo $item['quantity']; ?></td>
                                        <td>₹<?php echo number_format($item['price'] * $item['quantity'], 2); ?></td>
                                    </tr>
                                    <?php endwhile; ?>
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <td colspan="3" class="text-end"><strong>Total:</strong></td>
                                        <td><strong>₹<?php echo number_format($order['total_price'], 2); ?></strong></td>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Next Steps -->
            <div class="row justify-content-center">
                <div class="col-md-8">
                    <h3 class="mb-4">What's Next?</h3>
                    <div class="timeline">
                        <div class="timeline-step">
                            <h5><i class="fas fa-check-circle me-2 text-success"></i>Order Confirmed</h5>
                            <p class="text-muted">We've received your rental order</p>
                        </div>
                        <div class="timeline-step">
                            <h5><i class="fas fa-truck me-2 text-warning"></i>Delivery Preparation</h5>
                            <p class="text-muted">Your outfit will be prepared for delivery</p>
                        </div>
                        <div class="timeline-step">
                            <h5><i class="fas fa-calendar-day me-2 text-secondary"></i>Outfit Delivery</h5>
                            <p class="text-muted">We'll deliver your outfit on the selected date</p>
                        </div>
                        <div class="timeline-step">
                            <h5><i class="fas fa-box me-2 text-secondary"></i>Return & Pickup</h5>
                            <p class="text-muted">We'll pickup the outfit after your rental period</p>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="mt-5">
                <div class="row g-3 justify-content-center">
                    <div class="col-auto">
                        <a href="user/my-rentals.php" class="btn btn-primary">
                            <i class="fas fa-box me-2"></i>View My Rentals
                        </a>
                    </div>
                    <div class="col-auto">
                        <a href="products.php" class="btn btn-orange">
                            <i class="fas fa-tshirt me-2"></i>Rent More Outfits
                        </a>
                    </div>
                    <div class="col-auto">
                        <button onclick="window.print()" class="btn btn-outline-secondary">
                            <i class="fas fa-print me-2"></i>Print Receipt
                        </button>
                    </div>
                </div>
                
                <div class="mt-4">
                    <p class="text-muted">
                        <i class="fas fa-envelope me-2"></i>
                        A confirmation email has been sent to <?php echo htmlspecialchars($order['email']); ?>
                    </p>
                </div>
            </div>
        </div>
    </div>

    <?php include "includes/footer.php"; ?>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>