@extends('admin.layouts.container')

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

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

        @foreach($languages as $language)

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

        <div class="description_container col-md-12">
            @if(($service ?? null) && count($service->description))
                @foreach($service->descriptions as $descriptions)
                    <div class="form-group col-lg-12 card deleted_description descriptions_card">
                        <label>{{ __('services.descriptions') }}</label>
                        <div class="row">
                            @if($loop->iteration != 1)
                                <div class="form-group col-md-12 d-flex justify-content-end">
                                    <a href="#" class="btn btn-danger delete_description mr-3"><i class="fa fa-times"></i></a>
                                </div>
                            @endif
                            <div class="form-group col-lg-3">
                                <label>{{ __('services.description_icon') }} </label>
                                <input id="image_thumbnail_{{$loop->iteration}}" class="image_thumbnail_{{$loop->iteration}}" type="hidden" name="icon[]" value="{{$descriptions['icon'] ?? ""}}"/>
                                <div class="photos main_photo_{{$loop->iteration}} {{ $descriptions['icon'] ?? '' ? ' wow fadeInUp' : 'hide' }}">
                                    <div class="col-lg-12">
                                        <div class="items">
                                            <div class="img">
                                                <a href="#" id="lfm" data-input=".image_thumbnail_{{$loop->iteration}}" data-preview=".image_holder_{{$loop->iteration}}" class="lfm" data-hide=".pick_button">
                                                    <img src="{{asset($descriptions['icon'] ?? '')}} "  id="image_holder_{{$loop->iteration}}" class="image_holder_{{$loop->iteration}}"/>
                                                </a>
                                            </div>
                                            <a href="#" class="showHide resetImage close" data-empty=".image_thumbnail_{{$loop->iteration}}" data-hide=".main_photo_{{$loop->iteration}}" data-show=".pick_button_{{$loop->iteration}}"><i class="fas fa-times"></i></a>
                                        </div>
                                    </div>
                                </div>

                                <div class="clearfix"></div>
                                <span class="input-group-btn pick_button_{{$loop->iteration}} {{ ($descriptions['icon'] ?? "") ? 'hide' : '' }} ">
                                    <a id="lfm" data-input=".image_thumbnail_{{$loop->iteration}}" data-preview=".image_holder_{{$loop->iteration}}" data-show=".main_photo_{{$loop->iteration}}" data-hide=".pick_button_{{$loop->iteration}}" class="lfm btn btn-primary text-light">
                                        <i class="fas fa-camera-retro"></i> Choose
                                    </a>
                                </span>
                            </div>
                            <div class="col-md-9">
                                <div class="row">
                                    @foreach(languages() as $lang)
                                        <div class="form-group col-md-6">
                                            <input type="text" name="desc_{{$lang->local}}[]" placeholder="{{__('services.descriptions') . ' (' . $lang->name . ')'}}" class="form-control" value="{{ $descriptions['desc_'.$lang->local] }}"/>
                                        </div>
                                    @endforeach
                                </div>
                            </div>
                        </div>
                    </div>
                @endforeach
            @else
                <div class="form-group col-lg-12 card descriptions_card">
                    <label>{{ __('services.descriptions') }}</label>
                    <div class="row">
                        <div class="form-group col-lg-3">
                            <label>{{ __('services.description_icon') }} </label>
                            <input id="image_thumbnail_1" class="image_thumbnail_1" type="hidden" name="icon[]" value=""/>
                            <div class="photos main_photo_1 hide">
                                <div class="col-lg-12">
                                    <div class="items">
                                        <div class="img">
                                            <a href="#" id="lfm" data-input=".image_thumbnail_1" data-preview=".image_holder_1" class="lfm" data-hide=".pick_button_1">
                                                <img src=""  id="image_holder_1" class="image_holder_1"/>
                                            </a>
                                        </div>
                                        <a href="#" class="showHide resetImage close" data-empty=".image_thumbnail_1" data-hide=".main_photo_1" data-show=".pick_button_1"><i class="fas fa-times"></i></a>
                                    </div>
                                </div>
                            </div>

                            <div class="clearfix"></div>
                            <span class="input-group-btn pick_button_1">
                            <a id="lfm" data-input=".image_thumbnail_1" data-preview=".image_holder_1" data-show=".main_photo_1" data-hide=".pick_button_1" class="lfm btn btn-primary text-light">
                                <i class="fas fa-camera-retro"></i> Choose
                            </a>
                         </span>
                        </div>
                        <div class="col-md-9">
                            <div class="row">
                                @foreach(languages() as $lang)
                                    <div class="form-group col-md-6">
                                        <input type="text" name="desc_{{$lang->local}}[]" placeholder="{{__('services.descriptions') . ' (' . $lang->name . ')'}}" class="form-control" value=""/>
                                    </div>
                                @endforeach

                            </div>
                        </div>
                    </div>
                </div>
            @endif
        </div>
        <div class="form-group col-lg-12">
            <a href="#" class="btn btn-primary btn-block add_descriptions"><i class="fa fa-plus"></i> {{ __('services.descriptions') }}</a>
        </div>

        {{--Start image container--}}
        <div class="form-group col-lg-6">
            <label>{{ meta('image', getCurrentLocale()) ?? __('services.image') }} </label>
            <input id="image_thumbnail" class="image_thumbnail" type="hidden" name="image" value="{{old("image", $service->image ?? "")}}"/>

            <div class="photos main_photo {{ old('image', $service->image ?? '') ? ' wow fadeInUp' : 'hide' }}">
                <div class="col-lg-12">
                    <div class="items">
                        <div class="img">
                            <a href="#" id="lfm" data-input=".image_thumbnail" data-preview=".image_holder" class="lfm" data-hide=".pick_button">
                                <img src="{{asset(old('image', $service->image ?? ''))}} "  id="image_holder" class="image_holder"/>
                            </a>
                        </div>
                        <a href="#" class="showHide resetImage close" data-empty=".image_thumbnail" data-hide=".main_photo" data-show=".pick_button"><i class="fas fa-times"></i></a>
                    </div>
                </div>
            </div>

            <div class="clearfix"></div>
            <span class="input-group-btn pick_button {{ old('image', $service->image ?? '') ? 'hide' : '' }} ">
            <a id="lfm" data-input=".image_thumbnail" data-preview=".image_holder" data-show=".main_photo" data-hide=".pick_button" class="lfm btn btn-primary text-light">
                <i class="fas fa-camera-retro"></i> Choose
            </a>
        </span>
        </div>
        {{--End image container--}}

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

@endsection

@section('inner_js')
    <!-- Editor JS files -->
        <script src="{{ asset('/assets/admin/js/lib/tinymce.min.js') }}"></script>
{{--    <script src="//cdn.tinymce.com/4/tinymce.min.js"></script>--}}
    <script>
        var route_prefix = "{{ url(config('lfm.prefix')) }}";
        var url = "{{url('/')}}/",
        currentLanguage = "{{getCurrentLocale()}}",
        inputName = null;
    </script>
    <script src="{{ asset('assets/admin/js/editor.js') }}"></script>
    <script src="{{ asset('assets/admin/js/lfm.js') }}"></script>


    <script>
        $('.add_descriptions').click(function (e) {
            e.preventDefault();
            let counter = parseInt(Math.random()*99999)*parseInt(new Date().getSeconds());
            let descriptionDiv = `
                <div class="form-group col-lg-12 card descriptions_card deleted_description">
                    <label>{{ __('services.descriptions') }}</label>
                    <div class="row">
                        <div class="form-group col-md-12 d-flex justify-content-end">
                            <a href="#" class="btn btn-danger delete_description mr-3"><i class="fa fa-times"></i></a>
                        </div>
                        <div class="form-group col-lg-3">
                        <label>{{ __('services.description_icon') }} </label>
                        <input id="image_thumbnail_`+counter+`" class="image_thumbnail_`+counter+`" type="hidden" name="icon[]" value=""/>

                        <div class="photos main_photo_`+counter+` hide">
                            <div class="col-lg-12">
                                <div class="items">
                                    <div class="img">
                                        <a href="#" id="lfm" data-input=".image_thumbnail_`+counter+`" data-preview=".image_holder_`+counter+`" class="lfm" data-hide=".pick_button_`+counter+`">
                                            <img src=""  id="image_holder_`+counter+`" class="image_holder_`+counter+`"/>
                                        </a>
                                    </div>
                                    <a href="#" class="showHide resetImage close" data-empty=".image_thumbnail_`+counter+`" data-hide=".main_photo_`+counter+`" data-show=".pick_button_`+counter+`"><i class="fas fa-times"></i></a>
                                </div>
                            </div>
                        </div>

                        <div class="clearfix"></div>
                        <span class="input-group-btn pick_button_`+counter+`">
                            <a id="lfm" data-input=".image_thumbnail_`+counter+`" data-preview=".image_holder_`+counter+`" data-show=".main_photo_`+counter+`" data-hide=".pick_button_`+counter+`" class="lfm btn btn-primary text-light">
                                <i class="fas fa-camera-retro"></i> Choose
                            </a>
                        </span>
                    </div>
                    <div class="col-md-9">
                    <div class="row">
                        @foreach(languages() as $lang)
                            <div class="form-group col-md-6">
                                <input type="text" name="desc_{{$lang->local}}[]" placeholder="{{__('services.descriptions') . ' (' . $lang->name . ')'}}" class="form-control" value=""/>
                            </div>
                        @endforeach

                    </div>
                        </div>
                    </div>
                </div>`;

            $('.description_container').append(descriptionDiv);

        });

        $(document).on('click', '.delete_description', function (e) {
            e.preventDefault();
            $(this).parents('.deleted_description').remove();
        });

        $('body').on('DOMNodeInserted', '.description_container, .actors_container', function(e) {
            if ($(e.target).find('.lfm')) {
                $('.lfm').filemanager('image', {prefix: route_prefix});
            }
        });



        $('.service_button').click(async function (e) {
            e.preventDefault();

            let form = $('.service_form'),
                data = new FormData(form[0]),
                url = form.attr('action'),
                method = form.attr('method');

            const response=await ajax({
                url,
                data,
                dataType: 'json',
                method
            });

            if (response.url){
                window.location.href = response.url;
            }

            if (response.errors){
                toasterError(response.errors);
            }
        });

    </script>

@endsection



