@extends('layouts.app')

@section('title', __('products.items.title'))

@push('styles')
<link href="{{ asset('css/products-items.css') }}" rel="stylesheet">
@endpush

@section('content')
<div class="container-fluid products-items-page">
    <div class="d-flex align-items-center justify-content-between mb-4 header-bar">
        <div class="d-flex align-items-center gap-3">
            <a href="{{ route('products.hub') }}" class="btn btn-outline-dark rounded-circle back-btn">
                <i class="bi bi-arrow-{{ app()->getLocale() == 'ar' ? 'right' : 'left' }}"></i>
            </a>
            <h2 class="h3 fw-bold text-primary mb-0">{{ __('products.items.title') }}</h2>
        </div>
        <button class="btn btn-primary add-btn" data-bs-toggle="modal" data-bs-target="#addProductModal">
            <i class="bi bi-plus-lg"></i>
            <span class="d-none d-md-inline ms-2">{{ __('products.items.add_button') }}</span>
        </button>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    @endif

    <div class="products-list">
        @forelse($products as $product)
            <div class="product-smart-bar card mb-3">
                <div class="card-body d-flex align-items-center justify-content-between p-3">
                    <div class="d-flex align-items-center gap-3 info-section">
                        @if($product->images->isNotEmpty())
                            <img src="{{ Storage::url($product->images->first()->path) }}" alt="{{ $product->name }}" class="product-thumb rounded">
                        @else
                            <div class="product-thumb-placeholder rounded d-flex align-items-center justify-content-center bg-light text-muted">
                                <i class="bi bi-box-seam"></i>
                            </div>
                        @endif
                        
                        <div>
                            <h5 class="mb-0 fw-bold text-dark">{{ $product->name }}</h5>
                            <div class="d-flex gap-2 mt-1">
                                <span class="badge bg-success price-badge">{{ number_format($product->sale_price) }}</span>
                            </div>
                        </div>
                    </div>

                    <div class="actions-section d-flex align-items-center gap-2">
                        @if($product->has_color || $product->has_size || $product->has_measurement)
                            <button class="btn btn-sm btn-outline-info view-variants-btn" type="button" data-bs-toggle="collapse" data-bs-target="#variants-{{ $product->id }}" aria-expanded="false">
                                <i class="bi bi-diagram-3"></i>
                                <span class="d-none d-md-inline ms-1">{{ __('products.items.view_variants') }}</span>
                            </button>
                        @endif
                        
                        <button class="btn btn-sm btn-outline-warning edit-btn" data-bs-toggle="modal" data-bs-target="#editProductModal-{{ $product->id }}">
                            <i class="bi bi-pencil"></i>
                        </button>
                        
                        <form action="{{ route('products.items.destroy', $product->id) }}" method="POST" class="d-inline delete-form">
                            @csrf
                            @method('DELETE')
                            <button type="button" class="btn btn-sm btn-outline-danger delete-btn" onclick="confirmDelete(this)">
                                <i class="bi bi-trash"></i>
                            </button>
                        </form>
                    </div>
                </div>

                {{-- Variants Collapse --}}
                <div class="collapse variants-collapse" id="variants-{{ $product->id }}">
                    <div class="card-footer bg-light border-top-0 p-3">
                        <div class="row g-3">
                            @if($product->has_color)
                                <div class="col-md-4">
                                    <h6 class="fw-bold mb-2">{{ __('products.items.form.has_color') }}</h6>
                                    <div class="d-flex flex-wrap gap-2">
                                        @foreach($product->colors as $color)
                                            <span class="badge bg-white text-dark border d-flex align-items-center gap-2 p-2">
                                                <span class="color-dot" style="background-color: {{ $color->code }}; width: 12px; height: 12px; border-radius: 50%;"></span>
                                                {{ $color->name }}
                                                <span class="badge bg-secondary ms-1">{{ $color->pivot->stock }}</span>
                                            </span>
                                        @endforeach
                                    </div>
                                </div>
                            @endif

                            @if($product->has_size)
                                <div class="col-md-4">
                                    <h6 class="fw-bold mb-2">{{ __('products.items.form.has_size') }}</h6>
                                    <div class="d-flex flex-wrap gap-2">
                                        @foreach($product->sizes as $size)
                                            <span class="badge bg-white text-dark border p-2">
                                                {{ $size->name }}
                                                <span class="badge bg-secondary ms-1">{{ $size->pivot->stock }}</span>
                                            </span>
                                        @endforeach
                                    </div>
                                </div>
                            @endif

                            @if($product->has_measurement)
                                <div class="col-md-4">
                                    <h6 class="fw-bold mb-2">{{ __('products.items.form.has_measurement') }}</h6>
                                    <div class="d-flex flex-wrap gap-2">
                                        @foreach($product->measurements as $measurement)
                                            <span class="badge bg-white text-dark border p-2">
                                                {{ $measurement->name }}
                                                <span class="badge bg-secondary ms-1">{{ $measurement->pivot->stock }}</span>
                                            </span>
                                        @endforeach
                                    </div>
                                </div>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
            
            @include('products.items.modals.edit', ['product' => $product])
            
        @empty
            <div class="text-center py-5">
                <div class="mb-3 text-muted">
                    <i class="bi bi-box-seam display-1"></i>
                </div>
                <h4 class="text-muted">{{ __('products.items.no_products_found') }}</h4>
            </div>
        @endforelse
        
        <div class="mt-4">
            {{ $products->links() }}
        </div>
    </div>
</div>

@include('products.items.modals.add')

@endsection

@push('scripts')
<script>
    function confirmDelete(btn) {
        if(confirm('{{ __('common.confirm_delete_message') }}')) {
            btn.closest('form').submit();
        }
    }
</script>
<script src="{{ asset('js/products-items.js') }}?v={{ time() }}"></script>
@endpush
