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

name('fibre_types');

new class extends Component {
    public $fibre_types;
    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 . '%')
                  ->orWhere('description', 'like', '%' . $this->search . '%');
            });
        }

        $this->fibre_types = $query->latest()->get();
    }

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

    // Example method if you want to restore delete functionality later:
    public function deleteFibreType($fibreTypeId)
    {
        FibreType::find($fibreTypeId)->delete();
        $this->loadFibreTypes();
    }
};
?>

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

            <div class="flex items-center justify-between mb-5">
                <x-app.heading
                    title="Type de file"
                    description="Vérifiez les types de fil ci-dessous"
                    :border="false"
                />
                <x-button tag="a" href="/fibreType/create">Nouveau type</x-button>
            </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, couleur ou description..."
                            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 fils -->
                    <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)
                        🔍 {{ $fibre_types->count() }} type de fil trouvé(s)
                        @else
                        🧵 {{ $fibre_types->count() }} type(s) au total
                        @endif
                    </div>
                </div>
            </div>

            @if($this->fibre_types?->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 fil trouvé</p>
                            <p class="text-gray-500">Aucun type de fil 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
                        <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="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"></path>
                            </svg>
                            <p class="text-lg font-medium text-gray-900 mb-2">Aucun type de fil trouvé</p>
                            <p class="text-gray-500">Commencez par créer votre premier type de fil.</p>
                        </div>
                    @endif
                </div>
            @else
                <div class="overflow-x-auto border rounded-lg">
                    <table class="min-w-full bg-white">
                        <thead class="text-sm bg-gray-200">
                            <tr>
                                <th class="px-4 py-2 text-left">Nom</th>
                                <th class="px-4 py-2 text-left">Couleur</th>
                                <th class="px-4 py-2 text-left">Quantité en stock (kg)</th>
                                <th class="px-4 py-2 text-left">Description</th>
                                <th class="px-4 py-2 text-left">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($this->fibre_types as $fibreType)
                                <tr>
                                    <td class="px-4 py-2 font-medium text-gray-900">{{ $fibreType->name }}</td>
                                    <td class="px-4 py-2">{{ $fibreType->color }} </td>
                                    <td class="px-4 py-2">
                                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium {{ $fibreType->stock && $fibreType->stock->quantity > 0 ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800' }}">
                                            {{ $fibreType->stock ? number_format($fibreType->stock->quantity, 2) : '0.00' }} kg
                                        </span>
                                    </td>
                                    <td class="px-4 py-2 text-gray-600">{{ $fibreType->description ?? 'Aucune description' }}</td>
                                    <td class="px-4 py-2 text-left">
                                        <div class="flex space-x-2">
                                            <x-button 
                                                tag="a" 
                                                href="/fibreType/{{ $fibreType->id }}/edit"
                                                class="p-2 text-white bg-blue-600 rounded hover:bg-blue-700 transition-colors"
                                                title="Modifier le type de fil"
                                            >
                                                <svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"></path>
                                                </svg>
                                            </x-button>
                                           <button 
                                                type="button" 
                                                wire:click="deleteFibreType({{ $fibreType->id }})"
                                                wire:confirm="Êtes-vous sûr de vouloir supprimer ce type de fil ?"
                                                class="p-2 text-white bg-red-600 rounded hover:bg-red-700 transition-colors"
                                                title="Supprimer le type de fil"
                                            >
                                                <svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path>
                                                </svg>
                                            </button> 
                                        </div>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            @endif

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