<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION['cart'])) {
    $_SESSION['cart'] = [];
}

if (isset($_POST['add_to_cart'])) {
    $item = [
        'id' => $_POST['product_id'],
        'name' => $_POST['product_name'],
        'price' => $_POST['product_price'],
        'image' => $_POST['product_image'],
        'qty' => 1
    ];

    $_SESSION['cart'][] = $item;
    header("Location: cart.php");
    exit();
}

if (isset($_GET['remove'])) {
    $index = $_GET['remove'];
    if(isset($_SESSION['cart'][$index])) {
        unset($_SESSION['cart'][$index]);
        $_SESSION['cart'] = array_values($_SESSION['cart']);
    }
    header("Location: cart.php");
    exit();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cart - ShopNest</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>
        .btn-orange {
            background: linear-gradient(45deg, #ff6b35, #ff8e53);
            border: none;
            color: white;
            padding: 8px 20px;
            border-radius: 20px;
            font-weight: 600;
        }
        .btn-orange:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(255, 107, 53, 0.3);
            color: white;
        }
        .cart-summary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 15px;
            padding: 25px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <?php include "includes/header.php"; ?>

    <div class="container mt-4">
        <h2 class="mb-4">
            <i class="fas fa-shopping-cart me-2"></i>Your Rental Cart
        </h2>

        <?php if (empty($_SESSION['cart'])): ?>
            <div class="text-center py-5">
                <i class="fas fa-shopping-cart fa-4x mb-3" style="color: #ddd;"></i>
                <h4>Your cart is empty</h4>
                <p class="text-muted">Add some amazing outfits to your cart</p>
                <a href="products.php" class="btn btn-orange">
                    <i class="fas fa-tshirt me-2"></i>Browse Collection
                </a>
            </div>
        <?php else: ?>
            <div class="row">
                <!-- Cart Items -->
                <div class="col-lg-8">
                    <div class="table-responsive">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>Product</th>
                                    <th>Price/Day</th>
                                    <th>Quantity</th>
                                    <th>Total</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php 
                                $subtotal = 0;
                                foreach ($_SESSION['cart'] as $index => $item): 
                                    $item_total = $item['price'] * $item['qty'];
                                    $subtotal += $item_total;
                                ?>
                                <tr>
                                    <td>
                                        <div class="d-flex align-items-center">
                                                <?php
                                                $cart_img = $item['image'] ?? $item['images'] ?? '';
                                                $cart_path = 'assets/images/products/' . $cart_img;
                                                $cart_src = (!empty($cart_img) && file_exists($cart_path)) ? $cart_path : '';
                                                ?>
                                                <img src="<?php echo $cart_src; ?>" 
                                                    class="me-3 rounded" 
                                                    style="width: 80px; height: 80px; object-fit: cover;">
                                            <div>
                                                <h6 class="mb-0"><?php echo htmlspecialchars($item['name']); ?></h6>
                                                <?php if(isset($item['size'])): ?>
                                                    <small class="text-muted d-block mt-1">
                                                        <strong>Size:</strong> <?php echo htmlspecialchars($item['size']); ?>
                                                    </small>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                    </td>
                                    <td>₹<?php echo number_format($item['price'], 2); ?></td>
                                    <td><?php echo $item['qty']; ?></td>
                                    <td>₹<?php echo number_format($item_total, 2); ?></td>
                                    <td>
                                        <a href="cart.php?remove=<?php echo $index; ?>" 
                                           class="btn btn-danger btn-sm"
                                           onclick="return confirm('Remove this item from cart?')">
                                            <i class="fas fa-trash"></i>
                                        </a>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                    
                    <div class="d-flex justify-content-between mt-3">
                        <a href="products.php" class="btn btn-outline-primary">
                            <i class="fas fa-arrow-left me-2"></i>Continue Shopping
                        </a>
                    </div>
                </div>
                
                <!-- Order Summary -->
                <div class="col-lg-4">
                    <div class="cart-summary">
                        <h4 class="mb-4">
                            <i class="fas fa-receipt me-2"></i>Rental Summary
                        </h4>
                        
                        <div class="mb-3">
                            <div class="d-flex justify-content-between mb-2">
                                <span>Rental Subtotal:</span>
                                <span>₹<?php echo number_format($subtotal, 2); ?></span>
                            </div>
                            <div class="d-flex justify-content-between mb-2">
                                <span>Delivery Charge:</span>
                                <span>₹99.00</span>
                            </div>
                            <div class="d-flex justify-content-between mb-2">
                                <span>Tax (18% GST):</span>
                                <span>₹<?php echo number_format($subtotal * 0.18, 2); ?></span>
                            </div>
                            <hr style="border-color: rgba(255,255,255,0.3);">
                            <div class="d-flex justify-content-between fw-bold">
                                <span>Total Payable:</span>
                                <span>₹<?php echo number_format($subtotal + 99 + ($subtotal * 0.18), 2); ?></span>
                            </div>
                        </div>
                        
                        <a href="checkout.php" class="btn btn-light w-100 mt-3">
                            <i class="fas fa-credit-card me-2"></i>Proceed to Checkout
                        </a>
                    </div>
                    
                    <!-- Rental Policies -->
                    <div class="card mt-3">
                        <div class="card-body">
                            <h6><i class="fas fa-shield-alt me-2"></i>Rental Policies</h6>
                            <ul class="list-unstyled small mb-0">
                                <li class="mb-1">
                                    <i class="fas fa-check text-success me-2"></i>
                                    Free pickup after rental
                                </li>
                                <li class="mb-1">
                                    <i class="fas fa-check text-success me-2"></i>
                                    Professional cleaning included
                                </li>
                                <li class="mb-1">
                                    <i class="fas fa-check text-success me-2"></i>
                                    Easy cancellation
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        <?php endif; ?>
    </div>

    <?php include "includes/footer.php"; ?>