@extends('layouts.admin', ['heading' => 'Products & Catalog', 'subheading' => 'Manage SKUs, categories, and stock'])

@section('content')
    <div class="grid grid-cols-1 gap-6 xl:grid-cols-3">
        <div class="admin-card p-5">
            <div class="mb-4 text-sm font-semibold text-slate-700">Add Category</div>
            <form method="POST" action="{{ route('admin.products.categories.store') }}" class="space-y-3">
                @csrf
                <input name="name" placeholder="Category name" class="w-full rounded border p-2" required>
                <select name="parent_id" class="w-full rounded border p-2">
                    <option value="">Parent Category (optional)</option>
                    @foreach($categories as $category)
                        <option value="{{ $category->id }}">{{ $category->name }}</option>
                    @endforeach
                </select>
                <button class="w-full rounded bg-slate-900 px-4 py-2 text-sm text-white">Create Category</button>
            </form>
        </div>

        <div class="admin-card p-5 xl:col-span-2">
            <div class="mb-4 text-sm font-semibold text-slate-700">Add Product</div>
            <form method="POST" action="{{ route('admin.products.store') }}" enctype="multipart/form-data" class="grid grid-cols-1 gap-3 md:grid-cols-2">
                @csrf
                <input name="sku" placeholder="SKU" class="rounded border p-2 text-sm" required>
                <input name="name" placeholder="Product Name" class="rounded border p-2 text-sm" required>
                <select name="category_id" class="rounded border p-2 text-sm">
                    <option value="">Category</option>
                    @foreach($categories as $category)
                        <option value="{{ $category->id }}">{{ $category->name }}</option>
                    @endforeach
                </select>
                <select name="subcategory_id" class="rounded border p-2 text-sm">
                    <option value="">Subcategory</option>
                    @foreach($categories as $category)
                        <option value="{{ $category->id }}">{{ $category->name }}</option>
                    @endforeach
                </select>
                <input name="dp_price" type="number" step="0.01" min="0" placeholder="DP Price" class="rounded border p-2 text-sm" required>
                <input name="mrp" type="number" step="0.01" min="0" placeholder="MRP" class="rounded border p-2 text-sm">
                <input name="bv" type="number" step="0.0001" min="0" placeholder="BV" class="rounded border p-2 text-sm">
                <input name="gst_percent" type="number" step="0.01" min="0" placeholder="GST %" class="rounded border p-2 text-sm">
                <input name="stock" type="number" min="0" placeholder="Stock Qty" class="rounded border p-2 text-sm">
                <label class="flex items-center gap-2 text-sm">
                    <input type="checkbox" name="is_active" value="1" checked> Active
                </label>
                <label class="flex items-center gap-2 text-sm">
                    <input type="checkbox" name="is_digital" value="1"> Digital Product
                </label>
                <input type="file" name="image" class="rounded border p-2 text-sm md:col-span-2">
                <button class="rounded bg-emerald-600 px-4 py-2 text-sm text-white md:col-span-2">Create Product</button>
            </form>
        </div>
    </div>

    <div class="mt-6 admin-card p-5">
        <form method="GET" class="mb-4 flex flex-wrap gap-2">
            <input name="q" value="{{ $search }}" placeholder="Search SKU / name" class="w-full rounded border p-2 md:max-w-sm" />
            <button class="rounded bg-slate-900 px-4 py-2 text-white">Search</button>
        </form>

        <div class="overflow-x-auto">
            <table class="min-w-full text-sm">
                <thead class="bg-slate-100 text-left">
                    <tr>
                        <th class="p-3">Product</th>
                        <th class="p-3">Price</th>
                        <th class="p-3">BV</th>
                        <th class="p-3">Stock</th>
                        <th class="p-3">Status</th>
                        <th class="p-3">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($products as $product)
                        <tr class="border-t">
                            <td class="p-3">
                                <div class="font-semibold">{{ $product->name }}</div>
                                <div class="text-xs text-slate-500">{{ $product->sku }}</div>
                                <div class="text-xs text-slate-500">{{ $product->category?->name }} {{ $product->subcategory?->name ? '/ '.$product->subcategory->name : '' }}</div>
                            </td>
                            <td class="p-3">
                                <div>DP: {{ number_format((float) $product->dp_price, 2) }}</div>
                                <div class="text-xs text-slate-500">MRP: {{ number_format((float) $product->mrp, 2) }}</div>
                            </td>
                            <td class="p-3">{{ number_format((float) $product->bv, 2) }}</td>
                            <td class="p-3">{{ $product->stock }}</td>
                            <td class="p-3">
                                <span class="admin-badge {{ $product->is_active ? 'success' : 'danger' }}">{{ $product->is_active ? 'Active' : 'Inactive' }}</span>
                            </td>
                            <td class="p-3">
                                <form method="POST" action="{{ route('admin.products.update', $product) }}" enctype="multipart/form-data" class="grid grid-cols-1 gap-2">
                                    @csrf
                                    <input name="sku" value="{{ $product->sku }}" class="rounded border p-1 text-xs">
                                    <input name="name" value="{{ $product->name }}" class="rounded border p-1 text-xs">
                                    <input name="dp_price" type="number" step="0.01" min="0" value="{{ $product->dp_price }}" class="rounded border p-1 text-xs">
                                    <input name="mrp" type="number" step="0.01" min="0" value="{{ $product->mrp }}" class="rounded border p-1 text-xs">
                                    <input name="bv" type="number" step="0.0001" min="0" value="{{ $product->bv }}" class="rounded border p-1 text-xs">
                                    <input name="gst_percent" type="number" step="0.01" min="0" value="{{ $product->gst_percent }}" class="rounded border p-1 text-xs">
                                    <input name="stock" type="number" min="0" value="{{ $product->stock }}" class="rounded border p-1 text-xs">
                                    <label class="text-xs">
                                        <input type="checkbox" name="is_active" value="1" {{ $product->is_active ? 'checked' : '' }}> Active
                                    </label>
                                    <label class="text-xs">
                                        <input type="checkbox" name="is_digital" value="1" {{ $product->is_digital ? 'checked' : '' }}> Digital
                                    </label>
                                    <button class="rounded bg-slate-900 px-2 py-1 text-xs text-white">Update</button>
                                    <button formaction="{{ route('admin.products.delete', $product) }}" class="rounded bg-rose-600 px-2 py-1 text-xs text-white" data-confirm="Delete this product?">Delete</button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="6" class="p-4 text-center text-slate-500">No products found.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <div class="mt-4">{{ $products->links() }}</div>
    </div>

    <div class="mt-6 admin-card p-5">
        <div class="mb-3 text-sm font-semibold text-slate-700">Category Management</div>
        <div class="overflow-x-auto">
            <table class="min-w-full text-sm">
                <thead class="bg-slate-100 text-left">
                    <tr>
                        <th class="p-3">Name</th>
                        <th class="p-3">Parent</th>
                        <th class="p-3">Status</th>
                        <th class="p-3">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($categories as $category)
                        <tr class="border-t">
                            <td class="p-3">{{ $category->name }}</td>
                            <td class="p-3">{{ $category->parent?->name ?? '-' }}</td>
                            <td class="p-3">{{ $category->is_active ? 'Active' : 'Inactive' }}</td>
                            <td class="p-3">
                                <form method="POST" action="{{ route('admin.products.categories.update', $category) }}" class="flex flex-wrap gap-2">
                                    @csrf
                                    <input name="name" value="{{ $category->name }}" class="rounded border p-1 text-xs">
                                    <select name="parent_id" class="rounded border p-1 text-xs">
                                        <option value="">Parent</option>
                                        @foreach($categories as $parent)
                                            <option value="{{ $parent->id }}" @selected($category->parent_id === $parent->id)>{{ $parent->name }}</option>
                                        @endforeach
                                    </select>
                                    <label class="text-xs">
                                        <input type="checkbox" name="is_active" value="1" {{ $category->is_active ? 'checked' : '' }}> Active
                                    </label>
                                    <button class="rounded bg-slate-900 px-2 py-1 text-xs text-white">Update</button>
                                    <button formaction="{{ route('admin.products.categories.delete', $category) }}" class="rounded bg-rose-600 px-2 py-1 text-xs text-white" data-confirm="Delete this category?">Delete</button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="4" class="p-4 text-center text-slate-500">No categories found.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
@endsection
