@extends('admin.layouts.app')

@section('title', 'Categories')
@section('page_title', 'Categories')

@section('content')
@php
    $items = $categories ?? $items ?? collect();
@endphp

<div class="page-shell">
    <div class="page-head">
        <div>
            <h1 class="page-title">Categories</h1>
            <div class="page-subtitle">Create, organize, and manage product categories from one place.</div>
        </div>
    </div>

    <div class="grid-3">
        <div class="card">
            <h3 style="margin-top:0;">Add Category</h3>

            @if(Route::has('admin.categories.store'))
                <form method="POST" action="{{ route('admin.categories.store') }}">
                    @csrf
                    <label for="name">Category Name</label>
                    <input type="text" id="name" name="name" value="{{ old('name') }}" placeholder="Enter category name" required>

                    @if(Route::has('admin.categories.index'))
                        <div class="form-actions">
                            <button type="submit" class="btn primary">Save Category</button>
                            <a href="{{ route('admin.categories.index') }}" class="btn btn-light">Reset</a>
                        </div>
                    @else
                        <div class="form-actions">
                            <button type="submit" class="btn primary">Save Category</button>
                        </div>
                    @endif
                </form>
            @else
                <div class="alert alert-info">Category store route is not available yet.</div>
            @endif
        </div>

        <div class="card" style="grid-column:span 2;">
            <div class="page-head" style="margin-bottom:10px;">
                <div>
                    <h3 style="margin:0;">Category List</h3>
                    <div class="page-subtitle">All saved categories are shown here.</div>
                </div>
            </div>

            <div class="table-wrap">
                <table class="table">
                    <thead>
                        <tr>
                            <th style="width:80px;">#</th>
                            <th>Name</th>
                            <th class="text-end">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($items as $index => $item)
                            @php
                                $name = $item->name ?? $item->category_name ?? '—';
                            @endphp
                            <tr>
                                <td>{{ method_exists($items, 'firstItem') ? $items->firstItem() + $index : $index + 1 }}</td>
                                <td>{{ $name }}</td>
                                <td class="text-end" style="white-space:nowrap;">
                                    @if(Route::has('admin.categories.edit'))
                                        <a href="{{ route('admin.categories.edit', $item) }}" class="btn btn-light">Edit</a>
                                    @endif

                                    @if(Route::has('admin.categories.destroy'))
                                        <form method="POST" action="{{ route('admin.categories.destroy', $item) }}" style="display:inline-block;" onsubmit="return confirm('Delete this category?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-danger">Delete</button>
                                        </form>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="3">No categories found.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            @if(method_exists($items, 'links'))
                <div style="margin-top:14px;">
                    {{ $items->links() }}
                </div>
            @endif
        </div>
    </div>
</div>
@endsection