<x-app-layout>
    <x-slot name="header"><i class="bi bi-archive me-2"></i>{{ __('Items / Stock') }}</x-slot>

    <div class="mb-3 d-flex justify-content-between align-items-center">
        <span class="text-muted small">{{ $items->total() }} {{ __('items') }}</span>
        @can('items.create')
        <a href="{{ route('items.create') }}" class="btn btn-primary btn-sm">
            <i class="bi bi-plus-circle me-1"></i>{{ __('Add Item') }}
        </a>
        @endcan
    </div>

    <div class="card shadow-sm">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="table-dark">
                        <tr>
                            <th>{{ __('#') }}</th>
                            <th>{{ __('Name') }}</th>
                            <th>{{ __('Category') }}</th>
                            <th>{{ __('Unit') }}</th>
                            <th class="text-end">{{ __('Quantity') }}</th>
                            <th class="text-end">{{ __('Unit Price') }}</th>
                            <th class="text-end">{{ __('Total Value') }}</th>
                            <th>{{ __('Description') }}</th>
                            @canany(['items.edit','items.delete'])
                            <th class="text-end">{{ __('Actions') }}</th>
                            @endcanany
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($items as $item)
                        <tr>
                            <td>{{ $loop->iteration }}</td>
                            <td><strong>{{ $item->name }}</strong></td>
                            <td><span class="badge bg-secondary text-capitalize">{{ $item->category }}</span></td>
                            <td>{{ $item->unit }}</td>
                            <td class="text-end">{{ number_format($item->quantity, 2) }}</td>
                            <td class="text-end">{{ number_format($item->unit_price, 2) }}</td>
                            <td class="text-end fw-bold text-success">{{ number_format($item->total_value, 2) }}</td>
                            <td class="text-muted small">{{ $item->description ?? '-' }}</td>
                            @canany(['items.edit','items.delete'])
                            <td class="text-end">
                                <div class="btn-group btn-group-sm">
                                    @can('items.edit')
                                    <a href="{{ route('items.edit', $item) }}" class="btn btn-outline-primary">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                    @endcan
                                    @can('items.delete')
                                    <form method="POST" action="{{ route('items.destroy', $item) }}" class="d-inline" onsubmit="return confirm('{{ __('Delete this item?') }}')">
                                        @csrf @method('DELETE')
                                        <button class="btn btn-outline-danger"><i class="bi bi-trash"></i></button>
                                    </form>
                                    @endcan
                                </div>
                            </td>
                            @endcanany
                        </tr>
                        @empty
                        <tr><td colspan="9" class="text-center text-muted py-4">{{ __('No items found.') }}</td></tr>
                        @endforelse
                    </tbody>
                    @if($items->count())
                    <tfoot class="table-light fw-bold">
                        <tr>
                            <td colspan="6" class="text-end">{{ __('Total Stock Value:') }}</td>
                            <td class="text-end text-success">{{ number_format($items->sum('total_value'), 2) }}</td>
                            <td colspan="2"></td>
                        </tr>
                    </tfoot>
                    @endif
                </table>
            </div>
        </div>
    </div>
    <div class="mt-3">{{ $items->links() }}</div>
</x-app-layout>
