<x-app-layout>
    <div class="container-fluid p-0">
        <!-- Header Section -->
        <div class="bg-dark text-white p-4 rounded-bottom-4 shadow-sm mb-4">
            <h2 class="mb-0 fw-bold">{{ __('Category Management') }}</h2>
            <p class="mb-0 text-success">Manage product categories for: {{ $shop->name }}</p>
        </div>

        <div class="mx-3">
            @if (session('success'))
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    {{ session('success') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @endif

            @if (session('error'))
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    {{ session('error') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @endif

            <div class="row g-4">
                {{-- Category Creation Form --}}
                <div class="col-md-4">
                    <div class="card shadow-sm border-0">
                        <div class="card-body">
                            <h5 class="card-title fw-bold mb-3">Add New Category</h5>
                            <form method="POST" action="{{ route('seller.categories.store') }}">
                                @csrf
                                <div class="mb-3">
                                    <label for="categoryName" class="form-label">Category Name</label>
                                    <input type="text" class="form-control" id="categoryName" name="name" value="{{ old('name') }}" required>
                                    @error('name')
                                        <div class="text-danger small mt-1">{{ $message }}</div>
                                    @enderror
                                </div>
                                <button type="submit" class="btn btn-success w-100">Create Category</button>
                            </form>
                        </div>
                    </div>
                </div>

                {{-- Category Listing Table --}}
                <div class="col-md-8">
                    <div class="card shadow-sm border-0">
                        <div class="card-body">
                            <h5 class="card-title fw-bold mb-3">Existing Categories ({{ $categories->count() }})</h5>

                            @if ($categories->isEmpty())
                                <div class="alert alert-info text-center">No categories registered yet.</div>
                            @else
                                <div class="table-responsive">
                                    <table class="table table-striped table-hover align-middle">
                                        <thead>
                                            <tr>
                                                <th>Name</th>
                                                <th>Items</th>
                                                <th>Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach ($categories as $category)
                                                <tr>
                                                    <td>{{ $category->name }}</td>
                                                    <td>
                                                        <span class="badge rounded-pill bg-primary">{{ $category->items_count }}</span>
                                                    </td>
                                                    <td>
                                                        <!-- Edit Button (Toggles Modal) -->
                                                        <button type="button" class="btn btn-sm btn-outline-primary me-2" data-bs-toggle="modal" data-bs-target="#editModal{{ $category->id }}">
                                                            Edit
                                                        </button>

                                                        <!-- Delete Button -->
                                                        <form action="{{ route('seller.categories.destroy', $category) }}" method="POST" class="d-inline" onsubmit="return confirm('Are you sure you want to delete this category and all {{ $category->items_count }} items under it?');">
                                                            @csrf
                                                            @method('DELETE')
                                                            <button type="submit" class="btn btn-sm btn-outline-danger" @disabled($category->items_count > 0)>
                                                                Delete
                                                            </button>
                                                        </form>
                                                        
                                                        <!-- Note for deletion -->
                                                        @if ($category->items_count > 0)
                                                            <div class="small text-danger mt-1">Cannot delete ({{ $category->items_count }} items exist).</div>
                                                        @endif
                                                    </td>
                                                </tr>

                                                <!-- Edit Category Modal -->
                                                <div class="modal fade" id="editModal{{ $category->id }}" tabindex="-1" aria-labelledby="editModalLabel{{ $category->id }}" aria-hidden="true">
                                                    <div class="modal-dialog">
                                                        <div class="modal-content">
                                                            <form method="POST" action="{{ route('seller.categories.update', $category) }}">
                                                                @csrf
                                                                @method('PUT')
                                                                <div class="modal-header bg-primary text-white">
                                                                    <h5 class="modal-title" id="editModalLabel{{ $category->id }}">Edit Category: {{ $category->name }}</h5>
                                                                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                                                                </div>
                                                                <div class="modal-body">
                                                                    <div class="mb-3">
                                                                        <label for="editName{{ $category->id }}" class="form-label">New Category Name</label>
                                                                        <input type="text" class="form-control" id="editName{{ $category->id }}" name="name" value="{{ old('name', $category->name) }}" required>
                                                                        @error('name')
                                                                            <div class="text-danger small mt-1">{{ $message }}</div>
                                                                        @enderror
                                                                    </div>
                                                                </div>
                                                                <div class="modal-footer">
                                                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                                                                    <button type="submit" class="btn btn-primary">Save changes</button>
                                                                </div>
                                                            </form>
                                                        </div>
                                                    </div>
                                                </div>
                                            @endforeach
                                        </tbody>
                                    </table>
                                </div>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
