@extends('admin.layouts.app')

@section('title', 'Edit Menu Item')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h1 class="h3">Edit Menu Item</h1>
                <div>
                    <a href="{{ route('admin.menus.show', $menu) }}" class="btn btn-info me-2">
                        <i class="bi bi-eye"></i> View
                    </a>
                    <a href="{{ route('admin.menus.index') }}" class="btn btn-secondary">
                        <i class="bi bi-arrow-left"></i> Back to Menu List
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-8">
            <div class="card">
                <div class="card-body">
                    <form action="{{ route('admin.menus.update', $menu) }}" method="POST">
                        @csrf
                        @method('PUT')
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="title" class="form-label">Title <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control @error('title') is-invalid @enderror" 
                                           id="title" name="title" value="{{ old('title', $menu->title) }}" required>
                                    @error('title')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="icon" class="form-label">Icon (Bootstrap Icons)</label>
                                    <input type="text" class="form-control @error('icon') is-invalid @enderror" 
                                           id="icon" name="icon" value="{{ old('icon', $menu->icon) }}" 
                                           placeholder="e.g., bi bi-house">
                                    @error('icon')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                    <small class="form-text text-muted">Use Bootstrap Icons classes (e.g., bi bi-house)</small>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="parent_id" class="form-label">Parent Menu</label>
                                    <select class="form-select @error('parent_id') is-invalid @enderror" id="parent_id" name="parent_id">
                                        <option value="">-- No Parent (Top Level) --</option>
                                        @foreach($parentMenus as $parentMenu)
                                            <option value="{{ $parentMenu->id }}" 
                                                {{ old('parent_id', $menu->parent_id) == $parentMenu->id ? 'selected' : '' }}>
                                                {{ $parentMenu->title }}
                                            </option>
                                        @endforeach
                                    </select>
                                    @error('parent_id')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="sort_order" class="form-label">Sort Order <span class="text-danger">*</span></label>
                                    <input type="number" class="form-control @error('sort_order') is-invalid @enderror" 
                                           id="sort_order" name="sort_order" value="{{ old('sort_order', $menu->sort_order) }}" min="0" required>
                                    @error('sort_order')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Link Type</label>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-check">
                                        <input class="form-check-input" type="radio" name="link_type" id="route_type" value="route" 
                                               {{ old('link_type', $menu->route_name ? 'route' : 'url') == 'route' ? 'checked' : '' }}>
                                        <label class="form-check-label" for="route_type">
                                            Internal Route
                                        </label>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-check">
                                        <input class="form-check-input" type="radio" name="link_type" id="url_type" value="url"
                                               {{ old('link_type', $menu->route_name ? 'route' : 'url') == 'url' ? 'checked' : '' }}>
                                        <label class="form-check-label" for="url_type">
                                            Custom URL
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div id="route_section" class="mb-3" style="{{ old('link_type', $menu->route_name ? 'route' : 'url') == 'url' ? 'display: none;' : '' }}">
                            <label for="route_name" class="form-label">Route Name</label>
                            <select class="form-select @error('route_name') is-invalid @enderror" id="route_name" name="route_name">
                                <option value="">-- Select Route --</option>
                                @foreach($availableRoutes as $routeName => $routeLabel)
                                    <option value="{{ $routeName }}" 
                                        {{ old('route_name', $menu->route_name) == $routeName ? 'selected' : '' }}>
                                        {{ $routeLabel }}
                                    </option>
                                @endforeach
                            </select>
                            @error('route_name')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div id="url_section" class="mb-3" style="{{ old('link_type', $menu->route_name ? 'route' : 'url') == 'route' ? 'display: none;' : '' }}">
                            <label for="url" class="form-label">URL</label>
                            <input type="url" class="form-control @error('url') is-invalid @enderror" 
                                   id="url" name="url" value="{{ old('url', $menu->url) }}" 
                                   placeholder="https://example.com">
                            @error('url')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="target" class="form-label">Target</label>
                                    <select class="form-select @error('target') is-invalid @enderror" id="target" name="target">
                                        <option value="_self" {{ old('target', $menu->target) == '_self' ? 'selected' : '' }}>Same Window</option>
                                        <option value="_blank" {{ old('target', $menu->target) == '_blank' ? 'selected' : '' }}>New Window</option>
                                    </select>
                                    @error('target')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <div class="form-check form-switch mt-4">
                                        <input class="form-check-input" type="checkbox" id="is_active" name="is_active" 
                                               {{ old('is_active', $menu->is_active) ? 'checked' : '' }}>
                                        <label class="form-check-label" for="is_active">
                                            Active
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="is_external" name="is_external" 
                                       {{ old('is_external', $menu->is_external) ? 'checked' : '' }}>
                                <label class="form-check-label" for="is_external">
                                    External Link
                                </label>
                            </div>
                            <small class="form-text text-muted">Check if this is an external link (will not use Laravel routing)</small>
                        </div>

                        <div class="mb-3">
                            <label for="description" class="form-label">Description</label>
                            <textarea class="form-control @error('description') is-invalid @enderror" 
                                      id="description" name="description" rows="3" 
                                      placeholder="Optional description for this menu item">{{ old('description', $menu->description) }}</textarea>
                            @error('description')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="d-flex justify-content-between">
                            <div>
                                <a href="{{ route('admin.menus.index') }}" class="btn btn-secondary">
                                    <i class="bi bi-x-circle"></i> Cancel
                                </a>
                                <a href="{{ route('admin.menus.show', $menu) }}" class="btn btn-info ms-2">
                                    <i class="bi bi-eye"></i> View
                                </a>
                            </div>
                            <div>
                                <button type="submit" class="btn btn-primary">
                                    <i class="bi bi-check-circle"></i> Update Menu Item
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        
        <div class="col-lg-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">Menu Information</h5>
                </div>
                <div class="card-body">
                    <table class="table table-sm">
                        <tr>
                            <td><strong>ID:</strong></td>
                            <td>{{ $menu->id }}</td>
                        </tr>
                        <tr>
                            <td><strong>Created:</strong></td>
                            <td>{{ $menu->created_at->format('M d, Y H:i') }}</td>
                        </tr>
                        <tr>
                            <td><strong>Updated:</strong></td>
                            <td>{{ $menu->updated_at->format('M d, Y H:i') }}</td>
                        </tr>
                        @if($menu->children->count() > 0)
                        <tr>
                            <td><strong>Children:</strong></td>
                            <td>{{ $menu->children->count() }} item(s)</td>
                        </tr>
                        @endif
                    </table>
                </div>
            </div>
            
            <div class="card mt-3">
                <div class="card-header">
                    <h5 class="mb-0">Help & Tips</h5>
                </div>
                <div class="card-body">
                    <h6>Icon Guidelines:</h6>
                    <ul class="small">
                        <li>Use Bootstrap Icons classes (e.g., <code>bi bi-house</code>)</li>
                        <li>Visit <a href="https://icons.getbootstrap.com/" target="_blank">Bootstrap Icons</a> for available icons</li>
                    </ul>
                    
                    <h6>Route vs URL:</h6>
                    <ul class="small">
                        <li><strong>Route:</strong> Use for internal Laravel routes</li>
                        <li><strong>URL:</strong> Use for external links or custom URLs</li>
                    </ul>
                    
                    @if($menu->children->count() > 0)
                    <div class="alert alert-warning small">
                        <strong>Note:</strong> This menu has {{ $menu->children->count() }} child item(s). Changing the parent will affect the menu structure.
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script>
$(document).ready(function() {
    // Toggle between route and URL sections
    $('input[name="link_type"]').change(function() {
        if ($(this).val() === 'route') {
            $('#route_section').show();
            $('#url_section').hide();
            $('#url').removeAttr('required');
        } else {
            $('#route_section').hide();
            $('#url_section').show();
            $('#url').attr('required', 'required');
        }
    });
    
    // Update external checkbox based on link type
    $('input[name="link_type"]').change(function() {
        if ($(this).val() === 'url') {
            $('#is_external').prop('checked', true);
        } else {
            $('#is_external').prop('checked', false);
        }
    });
});
</script>
@endpush
@endsection