@extends('layouts.admin')

@section('title', 'Categories')

@section('content')
    <section class="dashboard-head">
        <div>
            <span class="eyebrow">Catalog</span>
            <h1>Categories</h1>
        </div>
        <a class="button primary" href="{{ route('admin.categories.create') }}">Add category</a>
    </section>

    @if (session('status'))
        <p class="form-status">{{ session('status') }}</p>
    @endif

    <section class="panel">
        <div class="table-wrap">
            <table>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Slug</th>
                        <th>Products</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($categories as $category)
                        <tr>
                            <td><strong>{{ $category->name }}</strong></td>
                            <td>{{ $category->slug }}</td>
                            <td>{{ $category->products_count }}</td>
                            <td><span class="status {{ $category->is_active ? 'paid' : 'refunded' }}">{{ $category->is_active ? 'Active' : 'Inactive' }}</span></td>
                            <td>
                                <div class="table-actions">
                                    <a class="text-button" href="{{ route('admin.categories.edit', $category) }}">Edit</a>
                                    <form method="POST" action="{{ route('admin.categories.toggle', $category) }}">
                                        @csrf
                                        @method('PATCH')
                                        <button class="text-button" type="submit">{{ $category->is_active ? 'Disable' : 'Enable' }}</button>
                                    </form>
                                    <form method="POST" action="{{ route('admin.categories.destroy', $category) }}" onsubmit="return confirm('Delete this category and its products?')">
                                        @csrf
                                        @method('DELETE')
                                        <button class="text-button danger" type="submit">Delete</button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5">No categories yet.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        {{ $categories->links() }}
    </section>
@endsection
