{% extends "base.html" %}

{% block title %}Shopping Cart - Golden Uniforms{% endblock %}

{% block content %}
<div class="container py-4">
    <div class="cart-header text-center mb-4">
        <h2 class="cart-title">
            <i class="fas fa-shopping-cart me-2 text-orange"></i>
            Shopping Cart
        </h2>
        {% if cart_items %}
        <p class="text-muted">{{ cart_items|length }} item{{ 's' if cart_items|length != 1 else '' }} in your cart</p>
        {% endif %}
    </div>
    
    {% if cart_items %}
    <div class="row g-4">
        <div class="col-lg-8">
            <div class="cart-items-section">
                {% for item in cart_items %}
                <div class="cart-item-card">
                    <div class="row align-items-center">
                        <div class="col-md-3">
                            {% if item.product.image_url %}
                                <img src="{{ item.product.image_url }}" alt="{{ item.product.name }}" class="img-fluid rounded" style="max-height: 100px; object-fit: contain;">
                            {% else %}
                                <div class="bg-light d-flex align-items-center justify-content-center rounded" style="height: 100px;">
                                    <i class="fas fa-image fa-2x text-muted"></i>
                                </div>
                            {% endif %}
                        </div>
                        <div class="col-md-4">
                            <h6 class="mb-1">{{ item.product.name }}</h6>
                            <p class="text-muted small mb-1">{{ item.product.category_ref.name }}</p>
                            {% if item.variant %}
                            <p class="small mb-0">
                                <span class="badge" style="background-color: {{ item.variant.color_code or '#ddd' }};">{{ item.variant.color }}</span>
                            </p>
                            {% endif %}
                        </div>
                        <div class="col-md-2">
                            <p class="mb-0 fw-bold">KES {{ "%.2f"|format(item.product.price) }}</p>
                        </div>
                        <div class="col-md-2">
                            <div class="input-group input-group-sm">
                                <form method="POST" action="{{ url_for('update_cart', item_id=item.id) }}" class="d-flex">
                                    <input type="number" name="quantity" value="{{ item.quantity }}" min="1" max="{{ item.product.stock_quantity }}" class="form-control" style="width: 60px;">
                                    <button type="submit" class="btn btn-outline-secondary">Update</button>
                                </form>
                            </div>
                        </div>
                        <div class="col-md-1">
                            <a href="{{ url_for('remove_from_cart', item_id=item.id) }}" class="btn btn-sm btn-danger">
                                <i class="fas fa-trash"></i>
                            </a>
                        </div>
                    </div>
                    <hr>
                </div>
                {% endfor %}
            </div>
        </div>
        
        <div class="col-lg-4">
            <div class="card">
                <div class="card-header bg-orange text-white">
                    <h5 class="mb-0">Order Summary</h5>
                </div>
                <div class="card-body">
                    <div class="d-flex justify-content-between mb-3">
                        <strong>Total:</strong>
                        <strong class="text-orange">KES {{ "%.2f"|format(total) }}</strong>
                    </div>
                    <a href="{{ url_for('checkout') }}" class="btn btn-orange w-100">
                        Proceed to Checkout
                    </a>
                </div>
            </div>
            
            <div class="mt-3">
                <a href="{{ url_for('products') }}" class="btn btn-outline-secondary w-100">
                    Continue Shopping
                </a>
            </div>
        </div>
    </div>
    {% else %}
    <div class="text-center py-5">
        <i class="fas fa-shopping-cart fa-4x mb-3 text-muted"></i>
        <h4>Your cart is empty</h4>
        <p class="text-muted">Add some products to get started!</p>
        <a href="{{ url_for('products') }}" class="btn btn-orange">Start Shopping</a>
    </div>
    {% endif %}
</div>
{% endblock %}
