@extends('layouts.member')

@section('title', $product->name)
@section('page-title', $product->name)

@section('breadcrumb')
<nav aria-label="breadcrumb">
    <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="{{ route('member.products') }}">Produits</a></li>
        <li class="breadcrumb-item"><a href="{{ route('member.products') }}?category={{ $product->category_id }}">{{ $product->category->name }}</a></li>
        <li class="breadcrumb-item active" aria-current="page">{{ $product->name }}</li>
    </ol>
</nav>
@endsection

@section('content')
@php
    $inStock = ($product->stock ?? 0) > 0;
    $isLow = ($product->stock ?? 0) > 0 && ($product->stock ?? 0) <= 5;
@endphp

<div class="row g-4">
    <div class="col-lg-6">
        <div class="card modern shadow-sm border-0 rounded-3 h-100">
            <div class="card-body d-flex align-items-center justify-content-center p-3">
                @if($product->image)
                    <img src="{{ asset('storage/' . $product->image) }}" class="img-fluid rounded" alt="{{ $product->name }}" style="max-height: 460px; object-fit: contain;">
                @else
                    <div class="d-flex align-items-center justify-content-center bg-light rounded w-100" style="height: 460px;">
                        <i class="bi bi-image text-muted" style="font-size: 4rem;"></i>
                    </div>
                @endif
            </div>
        </div>
    </div>

    <div class="col-lg-6">
        <div class="card modern shadow-sm border-0 rounded-3">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-start mb-2">
                    <div>
                        <h4 class="mb-1">{{ $product->name }}</h4>
                        <a href="{{ route('member.products') }}?category={{ $product->category_id }}" class="text-muted text-decoration-none small"><i class="bi bi-tag me-1"></i>{{ $product->category->name }}</a>
                    </div>
                    <div class="d-flex flex-wrap gap-1 justify-content-end">
                        @if($product->discount_price)
                            <span class="badge bg-danger">-{{ $product->discount_percent }}%</span>
                        @endif
                        @if($product->is_featured)
                            <span class="badge bg-warning text-dark">Populaire</span>
                        @endif
                        @if($isLow)
                            <span class="badge bg-warning text-dark">Stock faible</span>
                        @endif
                        @unless($inStock)
                            <span class="badge bg-secondary">Rupture</span>
                        @endunless
                    </div>
                </div>

                <p class="text-muted">{{ $product->description }}</p>

                <div class="mb-3 d-flex align-items-center gap-3">
                    @if($product->discount_price)
                        <div>
                            <h3 class="text-primary mb-0">{{ number_format($product->discount_price, 0, ',', ' ') }} FCFA</h3>
                            <small class="text-muted text-decoration-line-through">{{ number_format($product->price, 0, ',', ' ') }} FCFA</small>
                        </div>
                        <small class="text-success">Économie {{ number_format($product->price - $product->discount_price, 0, ',', ' ') }} FCFA</small>
                    @else
                        <h3 class="text-primary mb-0">{{ number_format($product->price, 0, ',', ' ') }} FCFA</h3>
                    @endif
                </div>

                <div class="mb-4">
                    @if($inStock)
                        <span class="badge bg-success rounded-pill"><i class="bi bi-check-circle me-1"></i> En stock ({{ $product->stock }})</span>
                    @else
                        <span class="badge bg-danger rounded-pill"><i class="bi bi-x-circle me-1"></i> Rupture de stock</span>
                    @endif
                </div>

                @auth
                    @if(auth()->user()->role === 'member')
                        @php
                            $memberDiscountRate = floatval(\App\Models\Setting::get('member_discount_percentage', 5));
                        @endphp
                        @if($memberDiscountRate > 0)
                            <div class="alert alert-info d-flex align-items-start gap-3">
                                <i class="bi bi-star fs-4"></i>
                                <div>
                                    <strong>Avantage membre :</strong> {{ $memberDiscountRate }}% de réduction automatique.
                                    @if($product->discount_price)
                                        <br><small class="text-muted">Cumulable avec la promotion en cours.</small>
                                    @endif
                                </div>
                            </div>
                        @endif
                    @endif
                @endauth

                <div class="d-grid gap-2">
                    @if($inStock)
                        <form action="{{ route('cart.add', $product) }}" method="POST" id="cartForm">
                            @csrf
                            <input type="hidden" name="quantity" value="1">
                            <input type="hidden" name="redirect_to_checkout" id="redirectCheckout" value="0">
                            
                            <div class="d-grid gap-2">
                                <button type="submit" name="action" value="checkout" class="btn btn-primary" id="checkoutNowBtn">
                                    <i class="bi bi-lightning me-1"></i> Commander maintenant
                                </button>
                                <button type="submit" name="action" value="add_to_cart" class="btn btn-outline-primary" id="addToCartBtn">
                                    <i class="bi bi-cart-plus me-1"></i> Ajouter au panier
                                </button>
                            </div>
                        </form>
                    @else
                        <button class="btn btn-secondary" disabled><i class="bi bi-cart-x me-1"></i> Produit indisponible</button>
                        <button class="btn btn-outline-primary"><i class="bi bi-bell me-1"></i> Me prévenir quand disponible</button>
                    @endif
                </div>

                <div class="mt-3 text-center text-muted small"><i class="bi bi-truck me-1"></i>Livraison gratuite à partir de 50 000 FCFA</div>
            </div>
        </div>
    </div>
</div>

@if(count($relatedProducts) > 0)
<div class="mt-5 pt-4 border-top">
    <h5 class="mb-3">Produits similaires</h5>
    <div class="row g-3">
        @foreach($relatedProducts as $related)
        <div class="col-xl-3 col-lg-4 col-md-6">
            <div class="card h-100 product-card shadow-sm border-0 rounded-3">
                @if($related->image)
                    <img src="{{ asset('storage/' . $related->image) }}" class="card-img-top" alt="{{ $related->name }}" style="height: 200px; object-fit: cover;">
                @else
                    <div class="d-flex align-items-center justify-content-center bg-light" style="height: 200px;">
                        <i class="bi bi-image text-muted"></i>
                    </div>
                @endif
                <div class="card-body">
                    <h6 class="fw-600 mb-1">{{ Str::limit($related->name, 50) }}</h6>
                    <p class="mb-2"><strong class="text-primary">{{ number_format($related->price, 0, ',', ' ') }} FCFA</strong></p>
                    <a href="{{ route('member.products.show', $related) }}" class="btn btn-sm btn-outline-primary w-100">Voir détails</a>
                </div>
            </div>
        </div>
        @endforeach
    </div>
</div>
@endif

@push('scripts')
<script>
document.addEventListener('DOMContentLoaded', function() {
    const form = document.getElementById('cartForm');
    const checkoutNowBtn = document.getElementById('checkoutNowBtn');
    const addToCartBtn = document.getElementById('addToCartBtn');
    const redirectCheckout = document.getElementById('redirectCheckout');
    
    if (checkoutNowBtn) {
        checkoutNowBtn.addEventListener('click', function(e) {
            e.preventDefault();
            // Définir redirect_to_checkout pour le bouton Commander maintenant
            redirectCheckout.value = '1';
            // Soumettre le formulaire
            if (form) form.submit();
        });
    }
    
    if (addToCartBtn) {
        addToCartBtn.addEventListener('click', function(e) {
            e.preventDefault();
            // S'assurer que redirect_to_checkout est 0 pour le bouton Ajouter au panier
            redirectCheckout.value = '0';
            // Soumettre le formulaire
            if (form) form.submit();
        });
    }
});
</script>
@endpush

<style>
.product-card { transition: transform 0.2s ease-in-out; }
.product-card:hover { transform: translateY(-3px); }
</style>
@endsection