@extends('layouts.app')

@section('title', 'Admin - Categories')

@section('content')
<div class="max-w-7xl mx-auto space-y-6">
    <div class="flex justify-between items-center">
        <h1 class="text-3xl font-semibold">Categories</h1>
        <a href="{{ route('admin.dashboard') }}" class="text-slate-700 hover:text-slate-900">← Back</a>
    </div>

    <!-- Add Category Form -->
    <div class="rounded-3xl bg-white p-8 shadow-sm">
        <h2 class="text-2xl font-semibold mb-6">Add New Category</h2>
        
        <form action="{{ route('admin.categories.store') }}" method="POST" enctype="multipart/form-data" class="space-y-4">
            @csrf

            <div>
                <label class="block text-sm font-medium text-slate-700 mb-2">Category Name</label>
                <input type="text" name="name" required class="w-full rounded-3xl border border-slate-200 px-4 py-3 focus:border-slate-900 focus:outline-none">
            </div>

            <div>
                <label class="block text-sm font-medium text-slate-700 mb-2">Description</label>
                <textarea name="description" rows="3" class="w-full rounded-3xl border border-slate-200 px-4 py-3 focus:border-slate-900 focus:outline-none"></textarea>
            </div>

            <div>
                <label class="block text-sm font-medium text-slate-700 mb-2">Category Image</label>
                <input type="file" name="image" accept="image/*" class="w-full">
            </div>

            <button type="submit" class="rounded-full bg-slate-900 px-6 py-3 text-white hover:bg-slate-800">
                Add Category
            </button>
        </form>
    </div>

    <!-- Categories List -->
    <div class="overflow-x-auto rounded-3xl bg-white shadow-sm">
        <table class="min-w-full divide-y divide-slate-200 text-sm">
            <thead class="bg-slate-50 text-slate-700">
                <tr>
                    <th class="px-6 py-4 text-left font-semibold">Name</th>
                    <th class="px-6 py-4 text-left font-semibold">Products</th>
                    <th class="px-6 py-4 text-left font-semibold">Created</th>
                    <th class="px-6 py-4 text-left font-semibold">Actions</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-slate-200 bg-white">
                @foreach($categories as $category)
                    <tr>
                        <td class="px-6 py-4 font-semibold">{{ $category->name }}</td>
                        <td class="px-6 py-4">{{ $category->products_count }}</td>
                        <td class="px-6 py-4">{{ $category->created_at->format('M d, Y') }}</td>
                        <td class="px-6 py-4 space-x-2">
                            <button class="text-slate-700 hover:text-slate-900">Edit</button>
                            @if($category->products_count === 0)
                                <form action="{{ route('admin.categories.delete', $category) }}" method="POST" class="inline">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="text-red-600 hover:text-red-800" onclick="return confirm('Are you sure?')">Delete</button>
                                </form>
                            @endif
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>

    {{ $categories->links() }}
</div>
@endsection
