@extends('layouts.yellow')

@section('content')
<nav class="page-breadcrumb">
    <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="{{route('dashboard')}}">Dashboard</a></li>
        <li class="breadcrumb-item"><a href="{{route('routes.index')}}">{{ __('common.route-permissions') }}</a></li>
        <li class="breadcrumb-item active" aria-current="page">{{ __('common.detect-routes') }}</li>
    </ol>
</nav>

<div class="row">
    <div class="col-md-12 grid-margin stretch-card">
        <div class="card">
            <div class="card-body">
                <h6 class="card-title">{{ __('common.detected-routes') }}</h6>
                
                @if ($errors->any())
                <div class="alert alert-danger">
                    <ul class="mb-0">
                        @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
                @endif
                
                @if(count($detectedRoutes) === 0)
                <div class="alert alert-info">
                    {{ __('common.no-new-routes-detected') }}
                </div>
                @else
                <p>{{ __('common.routes-detected-message', ['count' => count($detectedRoutes)]) }}</p>
                
                <form action="{{ route('routes.import') }}" method="POST">
                    @csrf
                    <div class="table-responsive">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th><input type="checkbox" id="selectAll"> {{ __('common.select') }}</th>
                                    <th>{{ __('common.path') }}</th>
                                    <th>{{ __('common.method') }}</th>
                                    <th>{{ __('common.name') }}</th>
                                    <th>{{ __('common.action') }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($detectedRoutes as $index => $route)
                                <tr>
                                    <td>
                                        <input type="checkbox" name="routes[]" value="{{ $route['path'] }}" class="route-checkbox">
                                    </td>
                                    <td>{{ $route['path'] }}</td>
                                    <td>{{ implode(', ', $route['methods']) }}</td>
                                    <td>{{ $route['name'] ?: '-' }}</td>
                                    <td>{{ $route['action'] }}</td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                    
                    <div class="mt-3">
                        <button type="submit" class="btn btn-primary">{{ __('common.import-selected-routes') }}</button>
                        <a href="{{ route('routes.index') }}" class="btn btn-secondary">{{ __('common.cancel') }}</a>
                    </div>
                </form>
                @endif
            </div>
        </div>
    </div>
</div>
@endsection

@push('custom-scripts')
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const selectAllCheckbox = document.getElementById('selectAll');
        const routeCheckboxes = document.querySelectorAll('.route-checkbox');
        
        selectAllCheckbox.addEventListener('change', function() {
            const isChecked = this.checked;
            routeCheckboxes.forEach(checkbox => {
                checkbox.checked = isChecked;
            });
        });
        
        routeCheckboxes.forEach(checkbox => {
            checkbox.addEventListener('change', function() {
                const allChecked = Array.from(routeCheckboxes).every(cb => cb.checked);
                selectAllCheckbox.checked = allChecked;
            });
        });
    });
</script>
@endpush
