@extends('admin.layouts.container')

@section('content')
    <div class="side-title">
        <h3>{{ $head ?? ''}}</h3>
    </div>

    <div class="border-item">
        <form class="form-row" method="post" onsubmit="saveForm(this)" action="{{ $action }}" enctype="multipart/form-data">
            @if($city ?? ''  && $city->id)
                @method('PUT')
            @endif
            @csrf

            <div class="form-group col-lg-12">
                <label>{{ __('cities.area_id') }} </label>
                <input type="text" name="area" disabled id="area" value="{{ $area->currentDescription->name ?? '' }}" class="form-control {{ $errors->has('project_id') ? 'is-invalid' : '' }} area" data-rel="area">
                <input type="hidden" name="area_id" id="area_id" value="{{ $area_id }}" class="form-control" placeholder="area_id">
            </div>

            @foreach($languages as $language)

                <div class="form-group col-lg-6">
                    <label>{{ meta('name', $language->local) ?? __('cities.name') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
                    <input type="text" class="form-control {{ $errors->has('name_'.$language->local) ? 'is-invalid' : '' }}" name="name_{{ $language->local }}" placeholder="{{ meta('name', $language->local) ?? __('cities.name') }}" value='{{ old("name_{$language->local}", $city[$language->local]->name ?? "") }}'/>
                </div>
            @endforeach

            <div class="text-center mt-5 col-12">
                <button class="butn"><span>{{ __('dashboard.save') }}</span></button>
            </div>
        </form>
    </div>

@endsection

@section('inner_js')
    <!-- Editor JS orders -->
    <script src="{{ asset('/assets/admin/js/lib/tinymce.min.js') }}"></script>
    <script>
        if($( ".area" ).length){
            $( ".area" ).removeAttr('disabled')
            $( ".area" ).autocomplete({
                source: async function( request, response ) {
                    // get categories
                    let requestData= {
                        name: request.term
                    }

                    const data= await ajax({
                        url: areasAjaxRoute,
                        data: requestData,
                        method: 'get',
                        no_load: true
                    })
                    response($.map(data, function (item) {
                        return {
                            label: '#'+ item.id+' '+ item.name,
                            name: item.name,
                            area_id: item.id
                        };
                    }));
                },
                minLength: 1,
                select: function( event, ui ) {
                    setTimeout(() => {
                        if($( "#area" ).length){
                            $( "#area" ).val( ui.item.name )

                        }
                        if($( "#area_id" ).length){
                            $( "#area_id" ).val( ui.item.area_id )
                        }
                    }, 0);
                },
            });
        }
    </script>
    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            currentLanguage = "{{getCurrentLocale()}}",
            areasAjaxRoute = "{{route('admin.fetchAreas')}}"
            imagePath = "{{asset('')}}";
    </script>
    <script src="{{ asset('assets/admin/js/editor.js') }}"></script>
    <script src="{{ asset('assets/admin/js/lfm.js') }}"></script>
    <script src="{{ asset('assets/admin/js/articles.js') }}"></script>
@endsection



