@extends('layouts.app')

@section('title', 'My Wishlist - ' . \App\Models\Setting::get('site_name', 'KiyoKart'))

@section('content')
<div style="background: #F9FAFB;">
<div class="container cart-page-container">
    <h1 class="cart-page-title">
        <i class="bi bi-heart-fill"></i>
        My Wishlist
    </h1>

    @if($wishlistItems->count())
        <div class="row">
            <div class="col-12">
                <div class="cart-items-card">
                    @foreach($wishlistItems as $wishlistItem)
                        <div class="cart-item-row">
                            <div>
                                @if($wishlistItem->product->image_path || $wishlistItem->product->image)
                                    <x-product-image :product="$wishlistItem->product" context="listing" class="cart-item-image" alt="{{ $wishlistItem->product->name }}" />
                                @else
                                    <div class="cart-item-image-placeholder">
                                        <i class="bi bi-image"></i>
                                    </div>
                                @endif
                            </div>
                            <div class="cart-item-details">
                                <a href="{{ $wishlistItem->product->parent_id ? route('product.show', ['slug' => $wishlistItem->product->parent->slug]) . '?variant=' . $wishlistItem->product->id : route('product.show', $wishlistItem->product->slug) }}" class="cart-item-name" title="{{ $wishlistItem->product->name }}">
                                    {{ $wishlistItem->product->name }}
                                </a>
                                @if($wishlistItem->product->product_type === 'variant' && $wishlistItem->product->variants && $wishlistItem->product->variants->isNotEmpty())
                                    @php
                                        $activeVariants = $wishlistItem->product->variants->where('is_active', true);
                                        $variantPrices = $activeVariants->pluck('price')->filter();
                                        $minPrice = $variantPrices->min();
                                        $maxPrice = $variantPrices->max();
                                        $variantMrps = $activeVariants->pluck('mrp')->filter();
                                        $minMrp = $variantMrps->min();
                                        $maxMrp = $variantMrps->max();
                                    @endphp
                                    @if($minPrice && $maxPrice && $maxPrice === $minPrice)
                                        <div class="cart-item-price wishlist-price">₹{{ number_format($minPrice, 0)}}
                                            @if($minMrp && $minMrp > $minPrice)
                                                <span class="text-muted text-decoration-line-through ms-2">₹{{ number_format($minMrp, 0) }}</span>
                                            @endif
                                        </div>
                                    @else
                                        <div class="cart-item-price wishlist-price">₹{{ number_format($minPrice, 0) }} - ₹{{ number_format($maxPrice, 0) }}</div>
                                    @endif
                                @else
                                <div class="cart-item-price wishlist-price">
                                    ₹{{ number_format($wishlistItem->product->price, 2) }}
                                    @if($wishlistItem->product->mrp && $wishlistItem->product->mrp > $wishlistItem->product->price)
                                        <span class="text-muted text-decoration-line-through ms-2">
                                            ₹{{ number_format($wishlistItem->product->mrp, 2) }}
                                        </span>
                                    @endif
                                </div>
                                @endif
                                @if($wishlistItem->product->current_stock > 0 || ($wishlistItem->product->product_type === 'variant' && !$wishlistItem->product->is_out_of_stock))
                                    <span class="stock-badge in-stock">In Stock</span>
                                @else
                                    @if($wishlistItem->product->is_prebooking ?? false)
                                        <span class="stock-badge pre-booking-available">Pre-Booking Available</span>
                                    @else
                                        @if($wishlistItem->product->is_out_of_stock)
                                            <span class="stock-badge out-of-stock">Out of Stock</span>
                                        @endif
                                    @endif
                                @endif
                            </div>
                            <div class="cart-item-actions">
                                @if($wishlistItem->product->product_type !== 'variant')
                                <form action="{{ route('cart.store') }}" method="POST" class=" me-2">
                                    @csrf
                                    <input type="hidden" name="product_id" value="{{ $wishlistItem->product->id }}">
                                    <input type="hidden" name="qty" value="1">
                                    <button type="submit" class="btn btn-primary view-add-btn" 
                                            {{ $wishlistItem->product->current_stock <= 0 && !($wishlistItem->product->is_prebooking ?? false) ? 'disabled' : '' }}>
                                        <i class="bi bi-bag"></i> Add to Cart
                                    </button>
                                </form>
                                @else
                                    <a href="{{ $wishlistItem->product->parent_id ? route('product.show', ['slug' => $wishlistItem->product->parent->slug]) . '?variant=' . $wishlistItem->product->id : route('product.show', $wishlistItem->product->slug) }}" class="btn btn-primary">
                                        <i class="bi bi-eye"></i> View Product
                                    </a>
                                @endif
                                <form action="{{ route('wishlist.destroy', $wishlistItem->product->id) }}" method="POST" >
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="btn-cart-delete" onclick="return confirm('Are you sure you want to remove this item from wishlist?')">
                                        <i class="bi bi-x"></i> 
                                    </button>
                                </form>
                            </div>
                        </div>
                    @endforeach
                </div>

                <!-- Pagination -->
                @if($wishlistItems->hasPages())
                    <div class="mt-4">
                        {{ $wishlistItems->links('vendor.pagination.bootstrap-5') }}
                    </div>
                @endif
            </div>
        </div>
    @else
        <div class="empty-cart-container">
            <div class="empty-cart-icon">
                <i class="bi bi-heart"></i>
            </div>
            <h3>Your Wishlist is Empty</h3>
            <p>Start adding products you love to your wishlist!</p>
            <a href="{{ route('home') }}" class="btn-continue-shopping">
                <i class="bi bi-arrow-left"></i> Continue Shopping
            </a>
        </div>
    @endif
</div>
</div>
@endsection

@push('scripts')
<script>
    function trimText() {
    let elements = document.querySelectorAll('.cart-item-name');
    let width = window.innerWidth;

    let limit = 45; // default desktop
    if (width < 576) limit = 15;        // mobile
    else if (width < 992) limit = 25;   // tablet

    elements.forEach(el => {
        let fullText = el.getAttribute('title') || el.textContent.trim();
        if (fullText.length > limit) {
            el.textContent = fullText.substring(0, limit) + '...';
        } else {
            el.textContent = fullText;
        }
    });
}

window.onload = trimText;
window.onresize = trimText;
</script>
@endpush
