<?php
require_once 'config.php';

if (!isLoggedIn()) {
    header('Location: login.php');
    exit;
}

$user_id = $_SESSION['user_id'];
$message = '';

// Handle actions (remove item)
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['cart_id'])) {
    $cart_id = intval($_GET['cart_id']);
    $stmt = $pdo->prepare("DELETE FROM cart WHERE id = ? AND user_id = ?");
    $stmt->execute([$cart_id, $user_id]);
    $message = "Item removed from cart.";
}

// Fetch all cart items
$stmt = $pdo->prepare("SELECT c.id as cart_id, c.quantity, p.* FROM cart c JOIN products p ON c.product_id = p.id WHERE c.user_id = ?");
$stmt->execute([$user_id]);
$cart_items = $stmt->fetchAll();

$subtotal = 0;
foreach ($cart_items as $item) {
    $subtotal += $item['price'] * $item['quantity'];
}

// Discount via points: Admin/User points setting. Let's load user's actual points.
$stmt_user = $pdo->prepare("SELECT points FROM users WHERE id = ?");
$stmt_user->execute([$user_id]);
$userData = $stmt_user->fetch();
$user_points = $userData['points'] ?? 0;
$_SESSION['user_points'] = $user_points; // Refresh session points
?>
<?php include 'header.php'; ?>

<div class="container">
    <h2>Shopping Cart 🛒</h2>
    
    <?php if (!empty($message)): ?>
        <div class="alert alert-success" style="margin-top: 15px;"><?php echo $message; ?></div>
    <?php endif; ?>

    <div class="cart-layout" style="margin-top: 30px;">
        <div>
            <?php if (empty($cart_items)): ?>
                <div style="text-align: center; padding: 40px; background: var(--light); border-radius: var(--radius); border: 1px solid var(--gray-border);">
                    <span style="font-size: 40px;">🛍️</span>
                    <h3 style="margin-top: 15px;">Your cart is empty</h3>
                    <p style="color: var(--text-muted); margin-bottom: 20px;">Let's find some gorgeous outfits for you.</p>
                    <a href="index.php" class="btn btn-primary">Go Shopping</a>
                </div>
            <?php else: ?>
                <div class="cart-items">
                    <?php foreach ($cart_items as $item): ?>
                        <div class="cart-item">
                            <img src="<?php echo htmlspecialchars($item['image_url']); ?>" alt="<?php echo htmlspecialchars($item['name']); ?>" class="cart-item-img">
                            <div class="cart-item-details">
                                <h4><?php echo htmlspecialchars($item['name']); ?></h4>
                                <p style="color: var(--primary-hover); font-weight: 600;">฿<?php echo number_format($item['price'], 2); ?></p>
                                <p style="font-size: 13px;">Qty: <?php echo $item['quantity']; ?> | Subtotal: ฿<?php echo number_format($item['price'] * $item['quantity'], 2); ?></p>
                            </div>
                            <div>
                                <a href="cart.php?action=delete&cart_id=<?php echo $item['cart_id']; ?>" style="color: var(--danger); font-weight: 600; font-size: 14px;">Remove</a>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>

        <div>
            <?php if (!empty($cart_items)): ?>
                <div class="cart-summary">
                    <h3 style="margin-bottom: 20px; font-weight: 700; border-bottom: 1px solid var(--gray-border); padding-bottom: 10px;">Order Summary</h3>
                    
                    <div class="summary-row">
                        <span>Subtotal</span>
                        <strong>฿<?php echo number_format($subtotal, 2); ?></strong>
                    </div>
                    
                    <div class="summary-row">
                        <span>Shipping Fee</span>
                        <strong style="color: var(--success);">FREE</strong>
                    </div>

                    <div class="points-reward">
                        <span>✨ Your loyalty points: <strong><?php echo $user_points; ?> pts</strong></span>
                    </div>

                    <div class="summary-row total">
                        <span>Estimated Total</span>
                        <strong style="color: var(--primary-hover);">฿<?php echo number_format($subtotal, 2); ?></strong>
                    </div>

                    <a href="checkout.php" class="btn btn-primary" style="width: 100%; display: block; margin-top: 20px;">Proceed to Checkout</a>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
