<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Panier - 
        @if($user->role === 'seller' && $user->shop_name)
            {{ $user->shop_name }}
        @else
            Boutique de {{ $user->name }}
        @endif
    </title>
    <link rel="icon" type="image/svg+xml" href="{{ asset('favicon.svg') }}">
    <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}">

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    <link href="https://unpkg.com/aos@2.3.1/dist/aos.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Cormorant+Garamond:wght@400;600;700&family=Montserrat:wght@300;400;500;600;700&display=swap" rel="stylesheet">

    <style>
        :root {
            --primary: #10b981;
            --primary-dark: #059669;
            --accent: #10b981;
            --gradient-2: linear-gradient(135deg, #10b981 0%, #059669 100%);
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Montserrat', sans-serif;
            color: #1e293b;
            background: #f8fafc;
        }

        h1, h2, h3, h4, h5, h6 {
            font-family: 'Cormorant Garamond', serif;
        }

        /* TOP BAR */
        .top-bar {
            background: linear-gradient(135deg, #0f172a, #1e293b);
            color: white;
            padding: 10px 0;
            font-size: 0.85rem;
        }

        .top-bar a {
            color: white;
            text-decoration: none;
        }

        /* NAVIGATION */
        .main-nav {
            background: white;
            box-shadow: 0 2px 20px rgba(0,0,0,0.1);
            padding: 20px 0;
            position: sticky;
            top: 0;
            z-index: 1000;
        }

        .brand-logo {
            display: flex;
            align-items: center;
            gap: 12px;
            font-size: 1.5rem;
            font-weight: 700;
            color: #1e293b;
            text-decoration: none;
            font-family: 'Cormorant Garamond', serif;
        }

        .brand-logo i {
            font-size: 2rem;
            color: var(--accent);
        }

        .nav-links {
            list-style: none;
            display: flex;
            gap: 40px;
            margin: 0;
        }

        .nav-links a {
            color: #475569;
            text-decoration: none;
            font-weight: 500;
            transition: color 0.3s;
        }

        .nav-links a:hover {
            color: var(--accent);
        }

        .cart-btn {
            display: flex;
            align-items: center;
            gap: 8px;
            background: var(--gradient-2);
            color: white;
            padding: 10px 20px;
            border-radius: 50px;
            text-decoration: none;
            font-weight: 600;
            position: relative;
        }

        .cart-badge {
            position: absolute;
            top: -5px;
            right: -5px;
            background: #ef4444;
            color: white;
            width: 22px;
            height: 22px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 0.75rem;
        }

        /* CART SECTION */
        .cart-section {
            padding: 80px 0;
            min-height: 60vh;
        }

        .page-title {
            font-size: 3rem;
            font-weight: 700;
            margin-bottom: 10px;
            color: #0f172a;
        }

        .page-subtitle {
            color: #64748b;
            margin-bottom: 50px;
        }

        .cart-item {
            background: white;
            border-radius: 20px;
            padding: 25px;
            margin-bottom: 20px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.08);
            transition: all 0.3s;
        }

        .cart-item:hover {
            box-shadow: 0 10px 30px rgba(0,0,0,0.12);
        }

        .cart-item-image {
            width: 120px;
            height: 120px;
            border-radius: 15px;
            overflow: hidden;
            background: #f1f5f9;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .cart-item-image img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .cart-item-info h3 {
            font-size: 1.3rem;
            font-weight: 600;
            margin-bottom: 8px;
            color: #0f172a;
        }

        .cart-item-price {
            font-size: 1.5rem;
            font-weight: 700;
            color: var(--accent);
        }

        .quantity-controls {
            display: flex;
            align-items: center;
            gap: 10px;
            background: #f1f5f9;
            padding: 5px 10px;
            border-radius: 50px;
            width: fit-content;
        }

        .quantity-controls button {
            width: 30px;
            height: 30px;
            border: none;
            background: white;
            color: var(--accent);
            border-radius: 50%;
            cursor: pointer;
            transition: all 0.3s;
        }

        .quantity-controls button:hover {
            background: var(--accent);
            color: white;
        }

        .quantity-controls input {
            width: 50px;
            text-align: center;
            border: none;
            background: transparent;
            font-weight: 600;
        }

        .remove-btn {
            background: rgba(239, 68, 68, 0.1);
            color: #dc2626;
            border: none;
            padding: 10px 20px;
            border-radius: 50px;
            cursor: pointer;
            transition: all 0.3s;
            font-weight: 600;
        }

        .remove-btn:hover {
            background: #dc2626;
            color: white;
        }

        .cart-summary {
            background: white;
            border-radius: 20px;
            padding: 35px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.1);
            position: sticky;
            top: 100px;
        }

        .cart-summary h3 {
            font-size: 2rem;
            margin-bottom: 30px;
            color: #0f172a;
        }

        .summary-row {
            display: flex;
            justify-content: space-between;
            padding: 15px 0;
            border-bottom: 1px solid #f1f5f9;
        }

        .summary-row.total {
            border-bottom: none;
            border-top: 2px solid #e2e8f0;
            margin-top: 10px;
            padding-top: 20px;
        }

        .summary-row.total .label {
            font-size: 1.3rem;
            font-weight: 700;
            color: #0f172a;
        }

        .summary-row.total .value {
            font-size: 1.8rem;
            font-weight: 700;
            color: var(--accent);
        }

        .checkout-btn {
            width: 100%;
            padding: 18px;
            background: var(--gradient-2);
            color: white;
            border: none;
            border-radius: 50px;
            font-size: 1.1rem;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.3s;
            margin-top: 20px;
            text-decoration: none;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }

        .checkout-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 10px 30px rgba(16, 185, 129, 0.4);
            color: white;
        }

        .continue-shopping {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            color: var(--accent);
            text-decoration: none;
            font-weight: 600;
            margin-top: 30px;
        }

        .continue-shopping:hover {
            text-decoration: underline;
        }

        .empty-cart {
            text-align: center;
            padding: 80px 20px;
        }

        .empty-cart i {
            font-size: 6rem;
            color: #cbd5e1;
            margin-bottom: 30px;
        }

        .empty-cart h2 {
            font-size: 2.5rem;
            margin-bottom: 15px;
            color: #0f172a;
        }

        .empty-cart p {
            color: #64748b;
            font-size: 1.1rem;
            margin-bottom: 30px;
        }

        .empty-cart a {
            display: inline-flex;
            align-items: center;
            gap: 10px;
            background: var(--gradient-2);
            color: white;
            padding: 15px 40px;
            border-radius: 50px;
            text-decoration: none;
            font-weight: 600;
            transition: all 0.3s;
        }

        .empty-cart a:hover {
            transform: translateY(-3px);
            box-shadow: 0 10px 30px rgba(16, 185, 129, 0.4);
        }

        /* FOOTER */
        .footer {
            background: linear-gradient(135deg, #0f172a, #1e293b);
            color: #cbd5e1;
            padding: 60px 0 30px;
            margin-top: 80px;
        }

        .footer h5 {
            color: white;
            margin-bottom: 20px;
        }

        .footer a {
            color: #94a3b8;
            text-decoration: none;
            transition: color 0.3s;
        }

        .footer a:hover {
            color: var(--accent);
        }

        .footer-bottom {
            text-align: center;
            margin-top: 50px;
            padding-top: 30px;
            border-top: 1px solid rgba(255,255,255,0.1);
            color: #64748b;
        }
    </style>
</head>
<body>

<!-- TOP BAR -->
<div class="top-bar">
    <div class="container">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <i class="fas fa-phone me-2"></i>
                <a href="tel:{{ $user->phone }}">{{ $user->phone ?? '+225 XX XX XX XX XX' }}</a>
                <span class="mx-3">|</span>
                <i class="fas fa-envelope me-2"></i>
                <a href="mailto:{{ $user->email }}">{{ $user->email ?? 'contact@boutique.com' }}</a>
            </div>
            <div>
                <i class="fas fa-shipping-fast me-2"></i>
                Livraison Gratuite dès 50 000 XOF
            </div>
        </div>
    </div>
</div>

<!-- NAVIGATION -->
<nav class="main-nav">
    <div class="container">
        <div class="d-flex justify-content-between align-items-center">
            <a href="{{ route('user.shop', $user->username ?? $user->member_code ?? $user->id) }}" class="brand-logo">
                <i class="fas fa-gem"></i>
                <span>
                    @if($user->role === 'seller' && $user->shop_name)
                        {{ $user->shop_name }}
                    @else
                        Boutique de {{ $user->name }}
                    @endif
                </span>
            </a>

            <ul class="nav-links d-none d-lg-flex">
                <li><a href="{{ route('user.shop', $user->username ?? $user->member_code ?? $user->id) }}">Accueil</a></li>
                <li><a href="{{ route('user.shop.collection', $user->username ?? $user->member_code ?? $user->id) }}">Collection</a></li>
                <li><a href="{{ route('user.shop.about', $user->username ?? $user->member_code ?? $user->id) }}">À Propos</a></li>
                <li><a href="{{ route('user.shop.contact', $user->username ?? $user->member_code ?? $user->id) }}">Contact</a></li>
            </ul>

            <a href="{{ route('user.shop.cart', $user->username ?? $user->member_code ?? $user->id) }}" class="cart-btn">
                <i class="fas fa-shopping-bag"></i>
                Panier
                <span class="cart-badge">{{ count($cartItems) }}</span>
            </a>
        </div>
    </div>
</nav>

<!-- CART SECTION -->
<section class="cart-section">
    <div class="container">
        <h1 class="page-title" data-aos="fade-up">Mon Panier</h1>
        <p class="page-subtitle" data-aos="fade-up" data-aos-delay="100">
            {{ count($cartItems) }} article(s) dans votre panier
        </p>

        @if(count($cartItems) > 0)
            <div class="row g-4">
                <!-- Cart Items -->
                <div class="col-lg-8">
                    @foreach($cartItems as $item)
                        <div class="cart-item" data-aos="fade-up" data-aos-delay="{{ $loop->index * 100 }}">
                            <div class="d-flex gap-4 align-items-center flex-wrap">
                                <!-- Image -->
                                <div class="cart-item-image">
                                    @if($item['product']->image)
                                        <img src="{{ asset('storage/' . $item['product']->image) }}" alt="{{ $item['product']->name }}">
                                    @else
                                        <i class="fas fa-gem" style="font-size: 3rem; color: rgba(0,0,0,0.1);"></i>
                                    @endif
                                </div>

                                <!-- Info -->
                                <div class="cart-item-info flex-grow-1">
                                    <h3>{{ $item['product']->name }}</h3>
                                    <p class="text-muted mb-2">{{ $item['product']->category->name ?? 'Bijoux' }}</p>
                                    <div class="cart-item-price">
                                        {{ number_format($item['product']->price, 0, ',', ' ') }} XOF
                                    </div>
                                </div>

                                <!-- Quantity -->
                                <div class="d-flex flex-column align-items-center gap-3">
                                    <div class="quantity-controls">
                                        <button onclick="updateQuantity({{ $item['product']->id }}, {{ $item['quantity'] - 1 }})">
                                            <i class="fas fa-minus"></i>
                                        </button>
                                        <input type="number" value="{{ $item['quantity'] }}" readonly>
                                        <button onclick="updateQuantity({{ $item['product']->id }}, {{ $item['quantity'] + 1 }})">
                                            <i class="fas fa-plus"></i>
                                        </button>
                                    </div>
                                    <button class="remove-btn" onclick="removeFromCart({{ $item['product']->id }})">
                                        <i class="fas fa-trash"></i> Retirer
                                    </button>
                                </div>

                                <!-- Subtotal -->
                                <div class="text-end">
                                    <div class="text-muted small mb-1">Sous-total</div>
                                    <div class="fs-5 fw-bold" style="color: var(--accent);">
                                        {{ number_format($item['subtotal'], 0, ',', ' ') }} XOF
                                    </div>
                                </div>
                            </div>
                        </div>
                    @endforeach

                    <a href="{{ route('user.shop.collection', $user->username ?? $user->member_code ?? $user->id) }}" class="continue-shopping">
                        <i class="fas fa-arrow-left"></i>
                        Continuer vos achats
                    </a>
                </div>

                <!-- Summary -->
                <div class="col-lg-4">
                    <div class="cart-summary" data-aos="fade-left">
                        <h3>Récapitulatif</h3>

                        <div class="summary-row">
                            <span class="label">Sous-total</span>
                            <span class="value">{{ number_format($subtotal, 0, ',', ' ') }} XOF</span>
                        </div>

                        <div class="summary-row">
                            <span class="label">Livraison</span>
                            <span class="value text-success">Gratuite</span>
                        </div>

                        <div class="summary-row total">
                            <span class="label">Total</span>
                            <span class="value">{{ number_format($total, 0, ',', ' ') }} XOF</span>
                        </div>

                        <a href="{{ route('user.shop.checkout', $user->username ?? $user->member_code ?? $user->id) }}" class="checkout-btn">
                            <i class="fas fa-lock"></i>
                            <span>Procéder au Paiement</span>
                        </a>

                        <div class="mt-4 p-3 bg-light rounded">
                            <p class="mb-0 small text-muted">
                                <i class="fas fa-shield-alt text-success me-2"></i>
                                Paiement 100% sécurisé
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        @else
            <div class="empty-cart" data-aos="fade-up">
                <i class="fas fa-shopping-bag"></i>
                <h2>Votre panier est vide</h2>
                <p>Découvrez notre collection et ajoutez des produits à votre panier</p>
                <a href="{{ route('user.shop.collection', $user->username ?? $user->member_code ?? $user->id) }}">
                    <i class="fas fa-gem"></i>
                    <span>Découvrir la Collection</span>
                </a>
            </div>
        @endif
    </div>
</section>

<!-- FOOTER -->
<footer class="footer">
    <div class="container">
        <div class="row g-4">
            <div class="col-lg-4">
                <h5>
                    @if($user->role === 'seller' && $user->shop_name)
                        {{ $user->shop_name }}
                    @else
                        Boutique de {{ $user->name }}
                    @endif
                </h5>
                <p style="opacity: 0.8;">Des produits d'exception pour des moments inoubliables.</p>
            </div>

            <div class="col-lg-4">
                <h5>Navigation</h5>
                <div class="d-flex flex-column gap-2">
                    <a href="{{ route('user.shop', $user->username ?? $user->member_code ?? $user->id) }}">Accueil</a>
                    <a href="{{ route('user.shop.collection', $user->username ?? $user->member_code ?? $user->id) }}">Collection</a>
                    <a href="{{ route('user.shop.about', $user->username ?? $user->member_code ?? $user->id) }}">À Propos</a>
                    <a href="{{ route('user.shop.contact', $user->username ?? $user->member_code ?? $user->id) }}">Contact</a>
                </div>
            </div>

            <div class="col-lg-4">
                <h5>Contact</h5>
                <div class="d-flex flex-column gap-2">
                    <span><i class="fas fa-map-marker-alt me-2"></i>{{ $user->city ?? 'Ville' }}, {{ $user->country ?? 'Pays' }}</span>
                    <a href="tel:{{ $user->phone }}"><i class="fas fa-phone me-2"></i>{{ $user->phone ?? 'Téléphone' }}</a>
                    <a href="mailto:{{ $user->email }}"><i class="fas fa-envelope me-2"></i>{{ $user->email ?? 'Email' }}</a>
                </div>
            </div>
        </div>

        <div class="footer-bottom">
            <span>&copy; {{ date('Y') }} 
                @if($user->role === 'seller' && $user->shop_name)
                    {{ $user->shop_name }}
                @else
                    Boutique de {{ $user->name }}
                @endif
                . Tous droits réservés.
            </span>
        </div>
    </div>
</footer>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://unpkg.com/aos@2.3.1/dist/aos.js"></script>
<script>
    AOS.init({
        duration: 800,
        easing: 'ease-in-out',
        once: true
    });

    const username = '{{ $user->username ?? $user->member_code ?? $user->id }}';

    async function updateQuantity(productId, newQuantity) {
        if (newQuantity < 1) {
            return removeFromCart(productId);
        }

        try {
            const response = await fetch(`/u/${username}/update-cart`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRF-TOKEN': '{{ csrf_token() }}'
                },
                body: JSON.stringify({ product_id: productId, quantity: newQuantity })
            });

            const data = await response.json();

            if (data.success) {
                window.location.reload();
            } else {
                alert(data.message || 'Erreur lors de la mise à jour');
            }
        } catch (error) {
            console.error('Erreur:', error);
            alert('Erreur lors de la mise à jour de la quantité');
        }
    }

    async function removeFromCart(productId) {
        if (!confirm('Voulez-vous vraiment retirer cet article du panier ?')) {
            return;
        }

        try {
            const response = await fetch(`/u/${username}/remove-from-cart`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRF-TOKEN': '{{ csrf_token() }}'
                },
                body: JSON.stringify({ product_id: productId })
            });

            const data = await response.json();

            if (data.success) {
                window.location.reload();
            } else {
                alert(data.message || 'Erreur lors de la suppression');
            }
        } catch (error) {
            console.error('Erreur:', error);
            alert('Erreur lors de la suppression de l\'article');
        }
    }
</script>

</body>
</html>
