@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 }}">
            @if($notification ?? ''  && $notification->id)
                @method('PUT')
            @endif
            @CSRF

            <div class="form-group col-lg-6">
                <label>{{ __('notifications.status') }} </label>
                <select onchange="selectStatus(this)" name="status"  class="form-control">
                    @foreach($statuses as $row)
                        <option value="{{$row}}">{{__('notifications.'.$row)}}</option>
                    @endforeach
                </select>
            </div>

            <div class="form-group col-lg-6" id="users">
                <label>{{ __('notifications.user_id') }} </label>
                <input type="text" name="user" disabled id="user" value="{{ $notification->user->name ?? '' }}" class="form-control user" data-rel="user">
                <input type="hidden" name="user_id" id="user_id" value="{{ $notification->user_id ?? null }}" class="form-control" placeholder="user_id">
            </div>


            @foreach($languages as $language)

                <div class="form-group col-lg-6">
                    <label>{{ meta('name', $language->local) ?? __('sliders.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) ?? __('sliders.name') }}" value='{{ old("name_{$language->local}", $slider[$language->local]->name ?? "") }}'/>
                </div>
                <div class="form-group col-lg-12">
                    <label>{{ meta('description', $language->local) ?? __('sliders.description') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
                    <textarea class="form-control {{ $errors->has('description_'.$language->local) ? 'is-invalid' : '' }}" name="description_{{ $language->local }}" placeholder="{{ meta('description', $language->local) ?? __('sliders.description') }}" >{{ old("description_{$language->local}", $slider[$language->local]->description ?? "") }}</textarea>
                </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')
    <script>
        if($( ".user" ).length){
            $( ".user" ).removeAttr('disabled')
            $( ".user" ).autocomplete({
                source: async function( request, response ) {
                    // get users
                    let requestData= {
                        name: request.term
                    }

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

                        }
                        if($( "#user_id" ).length){
                            $( "#user_id" ).val( ui.item.user_id )
                        }
                    }, 0);
                },
            });
        }
    </script>
    <script>
        function selectStatus(elem)
        {
            let status = $(elem).val()
            if(status == 'all')
            {
                $('#users').hide()
            }
            else
            {
                $('#users').show();
            }
        }
    </script>

    <!-- Editor JS notifications -->
    <script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            usersAjaxRoute = "{{route('admin.fetchUser')}}",
            imagePath = "{{asset('')}}";
    </script>
    <script src="{{ asset('assets/admin/js/editor.js') }}"></script>
    <script src="{{ asset('assets/admin/js/lfm.js') }}"></script>
@endsection
