@extends('layouts.admin')

@section('title', 'Menus')

@section('content')
<div class="fundi-page-header">
    <div>
        <h1>Platform menus</h1>
        <p class="fundi-page-header__desc">Create, rename, reorder, and hide navigation links for admin and customer portals.</p>
    </div>
</div>

<div class="fundi-filters" style="margin-bottom:1rem;">
    <a href="{{ route('admin.menus.index', ['location' => 'admin']) }}" class="fundi-btn {{ $location === 'admin' ? 'fundi-btn--primary' : 'fundi-btn--secondary' }} fundi-btn--sm">Admin sidebar</a>
    <a href="{{ route('admin.menus.index', ['location' => 'portal']) }}" class="fundi-btn {{ $location === 'portal' ? 'fundi-btn--primary' : 'fundi-btn--secondary' }} fundi-btn--sm">Customer portal</a>
</div>

<div class="fundi-grid-2">
    <div class="fundi-card">
        <div class="fundi-card__header"><h3>Add menu item</h3></div>
        <div class="fundi-card__body fundi-card__body--padded">
            <form method="POST" action="{{ route('admin.menus.store') }}">
                @csrf
                <input type="hidden" name="location" value="{{ $location }}">
                <div class="fundi-form-group">
                    <label for="label">Label</label>
                    <input type="text" id="label" name="label" class="fundi-input" required maxlength="120" placeholder="e.g. Collections">
                </div>
                <div class="fundi-form-group">
                    <label for="route_name">Route name (preferred)</label>
                    <input type="text" id="route_name" name="route_name" class="fundi-input" placeholder="admin.loans.index">
                </div>
                <div class="fundi-form-group">
                    <label for="url">Or custom URL</label>
                    <input type="text" id="url" name="url" class="fundi-input" placeholder="/admin/custom">
                </div>
                <div class="fundi-form-group">
                    <label for="sort_order">Sort order</label>
                    <input type="number" id="sort_order" name="sort_order" class="fundi-input" value="{{ ($menus->max('sort_order') ?? 0) + 10 }}" min="0">
                </div>
                <div class="fundi-form-group">
                    <label for="badge_key">Badge key (optional)</label>
                    <input type="text" id="badge_key" name="badge_key" class="fundi-input" placeholder="pending_auth">
                </div>
                <label class="fundi-checkbox" style="margin-bottom:1rem;">
                    <input type="checkbox" name="is_visible" value="1" checked> Visible
                </label>
                <button type="submit" class="fundi-btn fundi-btn--primary">Create menu item</button>
            </form>
        </div>
    </div>

    <div class="fundi-card">
        <div class="fundi-card__header"><h3>{{ ucfirst($location) }} links ({{ $menus->count() }})</h3></div>
        <div class="fundi-card__body">
            <div class="fundi-table-wrap">
                <table class="fundi-table">
                    <thead>
                        <tr>
                            <th>Order</th>
                            <th>Label</th>
                            <th>Target</th>
                            <th>Visible</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($menus as $menu)
                            <tr>
                                <td colspan="5" style="padding:0.75rem;background:var(--fundi-sand);">
                                    <form method="POST" action="{{ route('admin.menus.update', $menu) }}" class="vfh-menu-edit">
                                        @csrf
                                        @method('PUT')
                                        <input type="hidden" name="location" value="{{ $menu->location }}">
                                        <div class="vfh-menu-edit__row">
                                            <input type="number" name="sort_order" class="fundi-input" value="{{ $menu->sort_order }}" style="width:4.5rem;" title="Sort">
                                            <input type="text" name="label" class="fundi-input" value="{{ $menu->label }}" required style="flex:1;" title="Label">
                                            <input type="text" name="route_name" class="fundi-input" value="{{ $menu->route_name }}" placeholder="route" style="flex:1.2;" title="Route">
                                            <input type="text" name="url" class="fundi-input" value="{{ $menu->url }}" placeholder="url" style="flex:1;" title="URL">
                                            <input type="hidden" name="badge_key" value="{{ $menu->badge_key }}">
                                            <input type="hidden" name="icon" value="{{ $menu->icon }}">
                                            <input type="hidden" name="open_in_new_tab" value="{{ $menu->open_in_new_tab ? '1' : '0' }}">
                                            <label class="fundi-checkbox" style="white-space:nowrap;">
                                                <input type="checkbox" name="is_visible" value="1" {{ $menu->is_visible ? 'checked' : '' }}> Show
                                            </label>
                                            <button type="submit" class="fundi-btn fundi-btn--primary fundi-btn--sm">Save</button>
                                        </div>
                                    </form>
                                    <form method="POST" action="{{ route('admin.menus.destroy', $menu) }}" style="margin-top:0.35rem;" onsubmit="return confirm('Delete this menu item?')">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="fundi-btn fundi-btn--danger fundi-btn--sm">Delete</button>
                                        <span class="text-muted" style="font-size:0.75rem;margin-left:0.5rem;">
                                            @if($menu->route_name) route: {{ $menu->route_name }} @endif
                                            @if($menu->badge_key) · badge: {{ $menu->badge_key }} @endif
                                        </span>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="5" class="fundi-empty">No menu items for this location.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection
