@extends('backend.admin.layouts.app')

@section('content')
    <div class="pagetitle">
        <h1>Dashboard</h1>
        <nav>
            <ol class="breadcrumb">
                <li class="breadcrumb-item">
                    <a href="{{ route('admin.dashboard') }}">Home</a>
                </li>
                <li class="breadcrumb-item">
                    <a href="{{ route('admin.product.index') }}">Products</a>
                </li>
                <li class="breadcrumb-item active">
                    Manage Categories – {{ $product->name }}
                </li>
            </ol>
        </nav>
    </div><!-- End Page Title -->

    <section class="section dashboard">
        <div class="row">
            <div class="col-lg-8">

                <div class="card">
                    <div class="card-body mt-3">

                        <div class="mb-3">
                            <h5 class="card-title mb-1">
                                Manage Main Categories for:
                                <span class="text-primary">{{ $product->name }}</span>
                            </h5>
                            <div class="text-muted small">
                                Primary Category:
                                {{ optional($product->category)->name ?? '—' }}
                            </div>
                        </div>

                        <form action="{{ route('admin.product.categories.update', $product->id) }}" method="POST">
                            @csrf

                            @php
                                $selectedMenus = $product->menus->pluck('id')->toArray();
                            @endphp

                            <div class="mb-3">
                                <label class="form-label">Select Main Categories (multiple)</label>

                                <div class="border rounded p-3" style="max-height: 420px; overflow-y: auto;">
                                    @forelse($menus as $menu)
                                        <div class="form-check mb-1">
                                            <input class="form-check-input"
                                                   type="checkbox"
                                                   name="menu_ids[]"
                                                   value="{{ $menu->id }}"
                                                   id="menu_{{ $menu->id }}"
                                                   {{ in_array($menu->id, $selectedMenus) ? 'checked' : '' }}>
                                            <label class="form-check-label" for="menu_{{ $menu->id }}">
                                                {{ $menu->name }}
                                            </label>
                                        </div>
                                    @empty
                                        <p class="text-muted mb-0">No main categories found. Please create menus first.</p>
                                    @endforelse
                                </div>

                                @error('menu_ids')
                                    <div class="text-danger small mt-1">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="mb-3">
                                <button type="submit" class="btn btn-primary">
                                    Save Categories
                                </button>
                                <a href="{{ route('admin.product.index') }}" class="btn btn-outline-secondary ms-1">
                                    Back to Products
                                </a>
                            </div>

                            <p class="text-muted small mb-0">
                                <strong>Note:</strong>
                                Ekta product chaile ekadhik main category-te thakte parbe.
                                Ei mapping frontend er category page gula-te use kora jabe.
                            </p>
                        </form>

                    </div>
                </div>

            </div>
        </div>
    </section>
@endsection
