<?php
session_start();
// Check if the user is logged in (you need to implement this logic)
$is_logged_in = isset($_SESSION['rm_id']); // Example: Check if user_id is set in the session
?>



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Shopping Cart</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- FontAwesome for icons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        .cart-table {
            margin-top: 50px;
        }
        .cart-table th, .cart-table td {
            vertical-align: middle;
        }
        .cart-table img {
            width: 80px;
            height: 80px;
            object-fit: cover;
            border-radius: 5px;
        }
        .total-amount {
            font-size: 1.5rem;
            font-weight: bold;
            color: #333;
        }
        .btn-remove {
            color: #ff0000;
            cursor: pointer;
        }
        .btn-remove:hover {
            color: #cc0000;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="my-5">Shopping Cart</h1>
        <?php if (isset($_SESSION['cart']) && !empty($_SESSION['cart'])): ?>
            <div class="table-responsive">
                <table class="table table-bordered cart-table">
                    <thead>
                        <tr>
                            <th>Image</th>
                            <th>Product</th>
                            <th>Price</th>
                            <th>Quantity</th>
                            <th>Total</th>
                            <th>Remove</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $total_amount = 0;
                        foreach ($_SESSION['cart'] as $item_id => $item):
                            $item_name = $item['item_name'];
                            $sales_price = $item['sales_price'];
                            $quantity = $item['quantity'];
                            $total = $sales_price * $quantity;
                            $total_amount += $total;
                        ?>
                            <tr>
                                <td>
                                    <img src="<?php echo htmlspecialchars($item['image_path'] ?? 'default-image.jpg'); ?>" alt="<?php echo htmlspecialchars($item_name); ?>">
                                </td>
                                <td><?php echo htmlspecialchars($item_name); ?></td>
                                <td>Rs. <?php echo number_format($sales_price, 2); ?></td>
                                <td><?php echo $quantity; ?></td>
                                <td>Rs. <?php echo number_format($total, 2); ?></td>
                                <td>
                                    <a href="remove_from_cart.php?id=<?php echo $item_id; ?>" class="btn-remove">
                                        <i class="fas fa-trash-alt"></i>
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <div class="d-flex justify-content-between align-items-center mt-4">
                <div class="total-amount">
                    Total: Rs. <?php echo number_format($total_amount, 2); ?>
                </div>


                
                <div>
                    <a href="clear_cart.php" class="btn btn-danger">
                        <i class="fas fa-trash"></i> Clear Cart
                    </a>
                    <a href="select_customer.php" class="btn btn-success">
    <i class="fas fa-shopping-cart"></i> Checkout
</a>
                </div>
            </div>
        <?php else: ?>
            <div class="alert alert-info">Your cart is empty.</div>
        <?php endif; ?>
    </div>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>