<?php
use function Laravel\Folio\{middleware, name};
use App\Models\FibreType;
use Livewire\Volt\Component;
middleware('auth');

name('stock.summary');

new class extends Component {
    public $fibreTypes;
    public $search = '';

    public function mount()
    {
        $this->loadFibreTypes();
    }

    public function loadFibreTypes()
    {
        $query = FibreType::with('stock');

        if ($this->search) {
            $query->where(function($q) {
                $q->where('name', 'like', '%' . $this->search . '%')
                  ->orWhere('color', 'like', '%' . $this->search . '%');
            });
        }

        $this->fibreTypes = $query->get();
    }

    public function updatedSearch()
    {
        $this->loadFibreTypes();
    }
};
?>

<x-layouts.app>
    @volt('stock.summary')
        <x-app.container class="max-w-full">

            <div class="flex items-center justify-between mb-5">
                <x-app.heading
                    title="Résumé du stock"
                    description="Niveaux de stock actuels pour tous les types de fibres"
                    :border="false"
                />
                <div class="flex space-x-2">
                    <x-button tag="a" href="/stock" variant="outline">Voir tous les mouvements</x-button>
                    <x-button tag="a" href="/stock/create">Ajouter du stock</x-button>
                </div>
            </div>

            <!-- Filters -->
            <div class="mb-6">
                <div class="flex items-center justify-between mb-5">
                    <!-- Champ de recherche -->
                    <div class="relative w-full md:w-1/2 lg:w-1/2">
                        <input type="text" id="search" wire:model.live.debounce.300ms="search"
                            placeholder="Rechercher par nom ou couleur..."
                            class="block w-full pl-10 pr-10 py-2 border border-gray-300 rounded-md shadow-sm
                                focus:border-indigo-400 focus:ring focus:ring-indigo-200 focus:ring-opacity-50
                                transition duration-200" />

                        @if($search)
                            <button wire:click="$set('search', '')"
                                class="absolute inset-y-0 right-0 pr-3 flex items-center text-gray-400 hover:text-gray-600 transition">
                                <svg class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                        d="M6 18L18 6M6 6l12 12" />
                                </svg>
                            </button>
                        @endif
                    </div>

                    <!-- Résumé du nombre de types de fibres -->
                    <div class="text-sm text-gray-600 bg-gray-50 px-4 py-2 rounded-md border border-gray-200 shadow-sm w-fit md:ml-auto ml-4">
                        @if($search)
                            🔍 {{ $fibreTypes->count() }} type(s) de fibre trouvé(s)
                        @else
                            📦 {{ $fibreTypes->count() }} type(s) de fibre au total
                        @endif
                    </div>
                </div>
            </div>

            @if($this->fibreTypes?->isEmpty())
                <div class="w-full p-20 text-center bg-gray-100 rounded-xl">
                    @if($search)
                        <div class="text-gray-500">
                            <svg class="mx-auto h-12 w-12 text-gray-400 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
                            </svg>
                            <p class="text-lg font-medium text-gray-900 mb-2">Aucun type de fibre trouvé</p>
                            <p class="text-gray-500">Aucun type de fibre correspond à vos critères de recherche "{{ $search }}"</p>
                            <button 
                                wire:click="$set('search', '')" 
                                class="mt-3 text-indigo-600 hover:text-indigo-500 font-medium"
                            >
                                Effacer la recherche
                            </button>
                        </div>
                    @else
                        <p class="text-gray-500">Aucun type de fibre trouvé.</p>
                    @endif
                </div>
            @else
                <div class="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
                    @foreach($this->fibreTypes as $fibreType)
                        <div class="p-6 bg-white border border-gray-200 rounded-lg shadow-sm">
                            <div class="flex items-center justify-between mb-4">
                                <div class="flex items-center">
                                    <h3 class="text-lg font-semibold text-gray-900  ">{{ $fibreType->name }}</h3>
                                </div>
                                <span class="px-2 py-1 text-xs rounded-full text-center
                                    @if($fibreType->stock && $fibreType->stock->stock_status === 'out_of_stock') bg-red-100 text-red-800
                                    @elseif($fibreType->stock && $fibreType->stock->stock_status === 'low_stock') bg-yellow-100 text-yellow-800
                                    @elseif($fibreType->stock && $fibreType->stock->stock_status === 'medium_stock') bg-blue-100 text-blue-800
                                    @elseif($fibreType->stock && $fibreType->stock->stock_status === 'good_stock') bg-green-100 text-green-800
                                    @else bg-gray-100 text-gray-800
                                    @endif">
                                    @if($fibreType->stock)
                                        @if($fibreType->stock->stock_status === 'out_of_stock') Rupture de stock
                                        @elseif($fibreType->stock->stock_status === 'low_stock') Stock faible
                                        @elseif($fibreType->stock->stock_status === 'medium_stock') Stock moyen
                                        @else Bon stock
                                        @endif
                                    @else
                                        Aucun stock
                                    @endif
                                </span>
                            </div>
                            <div class="flex justify-between mb-2">
                                <span class="text-sm text-gray-600">Couleur :</span>
                                <span class="text-sm font-medium text-gray-900">{{ $fibreType->color }}</span>
                            </div>
                            
                            <div class="space-y-3">
                                <div class="flex justify-between">
                                    <span class="text-sm text-gray-600">Stock actuel :</span>
                                    <span class="text-sm font-medium text-gray-900">
                                        {{ $fibreType->stock ? number_format($fibreType->stock->quantity, 2) . ' kg' : '0.00 kg' }}
                                    </span>
                                </div>
                                   <!--  <div class="flex justify-between">
                                    <span class="text-sm text-gray-600">Valeur totale :</span>
                                    <span class="text-sm font-medium text-gray-900">
                                        {{ number_format(($fibreType->stock ? $fibreType->stock->quantity : 0) * $fibreType->price, 2) }} DT
                                    </span>
                                </div> -->
                                </div>
                                
                                @if($fibreType->stock)
                                    <div class="mt-4 pt-4 border-t border-gray-200">
                                        <x-button tag="a" href="/stock/adjust/{{ $fibreType->stock->id }}" variant="outline" size="sm" class="w-full">
                                            Ajuster le stock
                                        </x-button>
                                    </div>
                                @endif
                        </div>
                    @endforeach
                </div>
            @endif

        </x-app.container>
    @endvolt
</x-layouts.app>

