@extends('layouts.app')

@section('title', 'Yarn Management')

@section('breadcrumb')
    @include('components.breadcrumb', ['items' => ['Stock' => null, 'Yarns' => null]])
@endsection

@section('content')
<div>
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 mb-6">
        <div>
            <h1 class="text-2xl font-bold text-slate-900 dark:text-white">Yarn Management</h1>
            <p class="text-sm text-slate-500 dark:text-slate-400 mt-1">{{ $yarns->total() }} yarn types</p>
        </div>
        <x-button variant="primary" icon="plus" :href="route('yarn.create')">Add Yarn</x-button>
    </div>

    <x-card :padding="false">
        <div class="overflow-x-auto">
            <table class="w-full text-sm">
                <thead>
                    <tr class="bg-slate-50 dark:bg-slate-900/50 border-b border-slate-200">
                        <th class="px-4 py-3 text-left text-[11px] font-bold uppercase text-slate-500">Name</th>
                        <th class="px-4 py-3 text-left text-[11px] font-bold uppercase text-slate-500">Code</th>
                        <th class="px-4 py-3 text-left text-[11px] font-bold uppercase text-slate-500">Category</th>
                        <th class="px-4 py-3 text-right text-[11px] font-bold uppercase text-slate-500">Count</th>
                        <th class="px-4 py-3 text-right text-[11px] font-bold uppercase text-slate-500">Reorder Level</th>
                        <th class="px-4 py-3 text-center text-[11px] font-bold uppercase text-slate-500">Status</th>
                        <th class="px-4 py-3 text-center text-[11px] font-bold uppercase text-slate-500">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    @forelse($yarns as $y)
                    <tr class="hover:bg-slate-50/50">
                        <td class="px-4 py-3.5 text-sm font-medium">{{ $y->name }}</td>
                        <td class="px-4 py-3.5 text-xs font-mono text-slate-600">{{ $y->code }}</td>
                        <td class="px-4 py-3.5 text-sm text-slate-600">{{ $y->category ?? '—' }}</td>
                        <td class="px-4 py-3.5 text-right text-sm">{{ $y->count ?? '—' }}</td>
                        <td class="px-4 py-3.5 text-right text-sm">{{ number_format($y->reorder_level) }}</td>
                        <td class="px-4 py-3.5 text-center">
                            <x-badge :variant="$y->is_active ? 'success' : 'gray'">{{ $y->is_active ? 'Active' : 'Inactive' }}</x-badge>
                        </td>
                        <td class="px-4 py-3.5 text-center">
                            <div class="flex items-center justify-center gap-1">
                                <a href="{{ route('yarn.show', $y->id) }}" class="w-8 h-8 flex items-center justify-center rounded-lg hover:bg-slate-100" title="View">
                                    <i data-lucide="eye" class="w-4 h-4"></i>
                                </a>
                                <a href="{{ route('yarn.edit', $y->id) }}" class="w-8 h-8 flex items-center justify-center rounded-lg hover:bg-slate-100" title="Edit">
                                    <i data-lucide="edit-2" class="w-4 h-4"></i>
                                </a>
                                <form action="{{ route('yarn.destroy', $y->id) }}" method="POST" onsubmit="return confirm('Delete {{ $y->name }}?')" class="inline">
                                    @csrf @method('DELETE')
                                    <button class="w-8 h-8 flex items-center justify-center rounded-lg hover:bg-red-50 text-slate-500 hover:text-red-600" title="Delete">
                                        <i data-lucide="trash-2" class="w-4 h-4"></i>
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr><td colspan="7" class="text-center py-8 text-slate-500">No yarns found. <a href="{{ route('yarn.create') }}" class="text-primary-600 font-semibold">Create one</a></td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($yarns->hasPages())
        <div class="px-4 py-3 border-t border-slate-200">{{ $yarns->withQueryString()->links() }}</div>
        @endif
    </x-card>
</div>
@endsection
