@extends('admin.layouts.container')

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

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


            <div class="form-group col-lg-4">
                <label>{{ meta('is_special', $lang) ?? __('products.is_special') }}</label>
                <select name="is_special" class="form-control">
                    <option {{(old('is_special') ?? $product->is_special ?? null) == 1 ? 'selected' : ''}} value="1">{{__('products.yes')}}</option>
                    <option {{(old('is_special') ?? $product->is_special ?? null) == 0 ? 'selected' : ''}} value="0">{{__('products.no')}}</option>
                </select>
            </div>

            <div class="form-group col-lg-4">
                <label>{{ meta('show_advantages', $lang) ?? __('products.show_advantages') }}</label>
                <select name="show_advantages" class="form-control">
                    <option {{(old('show_advantages') ?? $product->show_advantages ?? null) == 1 ? 'selected' : ''}} value="1">{{__('products.yes')}}</option>
                    <option {{(old('show_advantages') ?? $product->show_advantages ?? null) == 0 ? 'selected' : ''}} value="0">{{__('products.no')}}</option>
                </select>
            </div>

            <div class="form-group col-lg-4">
                <label>{{ meta('show_table', $lang) ?? __('products.show_table') }}</label>
                <select name="show_table" class="form-control">
                    <option {{(old('show_table') ?? $product->show_table ?? null) == 1 ? 'selected' : ''}} value="1">{{__('products.yes')}}</option>
                    <option {{(old('show_table') ?? $product->show_table ?? null) == 0 ? 'selected' : ''}} value="0">{{__('products.no')}}</option>
                </select>
            </div>

        @foreach($languages as $language)
        <div class="form-group col-lg-12">
            <label>{{ meta('name', $language->local) ?? __('products.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) ?? __('products.name') }}" value='{{ old("name_{$language->local}", $product[$language->local]->name ?? "") }}'/>
        </div>

        <div class="form-group col-lg-12">
            <label>{{ meta('description', $language->local) ?? __('products.description') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
            <textarea name="description_{{ $language->local }}" class="form-control  {{ $errors->has('description_'.$language->local) ? 'is-invalid' : '' }}" placeholder="{{ meta('description', $language->local) ?? __('products.description') }}">{{ old("description_{$language->local}", $product[$language->local]->description ?? "") }}</textarea>
        </div>
        <div class="form-group col-lg-12">
            <label>{{ meta('advantages', $language->local) ?? __('products.advantages') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
            <textarea rows="7" name="advantages_{{ $language->local }}" class="form-control  {{ $errors->has('advantages_'.$language->local) ? 'is-invalid' : '' }}" placeholder="{{ meta('advantages', $language->local) ?? __('products.advantages') }}">{{ old("advantages_{$language->local}", $product[$language->local]->advantages ?? "") }}</textarea>
        </div>
        <div class="form-group col-lg-12">
            <label>{{ meta('advantage_desc', $language->local) ?? __('products.advantage_desc') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
            <input type="text" class="form-control {{ $errors->has('advantage_desc_'.$language->local) ? 'is-invalid' : '' }}" name="advantage_desc_{{ $language->local }}" placeholder="{{ meta('advantage_desc', $language->local) ?? __('products.advantage_desc') }}" value='{{ old("advantage_desc_{$language->local}", $product[$language->local]->advantage_desc ?? "") }}'/>
        </div>

        <div class="form-group col-lg-6">
            <label>{{ meta('minimum_title', $language->local) ?? __('products.minimum_title') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
            <input type="text" class="form-control {{ $errors->has('minimum_title_'.$language->local) ? 'is-invalid' : '' }}" name="minimum_title_{{ $language->local }}" placeholder="{{ meta('minimum_title', $language->local) ?? __('products.minimum_title') }}" value='{{ old("minimum_title_{$language->local}", $product[$language->local]->minimum_title ?? "") }}'/>
        </div>
        <div class="form-group col-lg-6">
            <label>{{ meta('maximum_title', $language->local) ?? __('products.maximum_title') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
            <input type="text" class="form-control {{ $errors->has('maximum_title_'.$language->local) ? 'is-invalid' : '' }}" name="maximum_title_{{ $language->local }}" placeholder="{{ meta('maximum_title', $language->local) ?? __('products.maximum_title') }}" value='{{ old("maximum_title_{$language->local}", $product[$language->local]->maximum_title ?? "") }}'/>
        </div>
        @endforeach
            <div class="card col-md-12 mb-5">
                <div class="card-header">
                    <h5 class="card-title">{{__('products.points')}}</h5>
                </div>
                <div class="card-body product_points_appended_container">
                    @if(count($points))
                        @foreach($points['minimum'] as $key =>  $minimum)
                            <div class="point_item row justify-content-between align-items-center mb-4 p-2 shadow position-relative">
                                <div class="form-group col-lg-6">
                                    <label>{{ __('products.minimum') }}</label>
                                    <input type="text" class="form-control" name="minimum[]" placeholder="{{ __('products.minimum') }}" value='{{$minimum}}'/>
                                </div>
                                <div class="form-group col-lg-6">
                                    <label>{{ __('products.maximum') }}</label>
                                    <input type="text" class="form-control" name="maximum[]" placeholder="{{ __('products.maximum') }}" value='{{$points['minimum'][$key]}}'/>
                                </div>

                                @if($loop->iteration > 1)
                                    <div class="form-group position-absolute" style="top: 1%; right: 1%">
                                        <a class="delete_point" href="#"><i class="fa fa-times fa-2x text-danger"></i></a>
                                    </div>
                                @endif

                                <div class="form-group col-lg-6">
                                    <label>{{ __('products.points') }} {{__('products.arabic')}} </label>
                                    <input type="text" class="form-control" name="points[ar][]" placeholder="{{ __('products.points') .__('products.arabic') }}" value='{{$points['ar'][$key]}}'/>
                                </div>
                                <div class="form-group col-lg-6">
                                    <label>{{ __('products.points') }} {{__('products.english')}} </label>
                                    <input type="text" class="form-control" name="points[en][]" placeholder="{{ __('products.points') . __('products.english')}}" value='{{$points['en'][$key]}}'/>
                                </div>
                            </div>
                        @endforeach
                        @else
                        <div class="point_item row justify-content-between align-items-center mb-4 p-2 shadow position-relative">
                            <div class="form-group col-lg-6">
                                <label>{{ __('products.minimum') }}</label>
                                <input type="text" class="form-control" name="minimum[]" placeholder="{{ __('products.minimum') }}" value=''/>
                            </div>
                            <div class="form-group col-lg-6">
                                <label>{{ __('products.maximum') }}</label>
                                <input type="text" class="form-control" name="maximum[]" placeholder="{{ __('products.maximum') }}" value=''/>
                            </div>

                            @foreach($languages as $language)
                                <div class="form-group col-lg-6">
                                    <label>{{ __('products.points') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
                                    <input type="text" class="form-control"name="points[{{$language->local}}][]" placeholder="{{ __('products.points') }} {{ count($languages) > 1 ? "({$language->name})" : '' }}" value=''/>
                                </div>
                            @endforeach
                        </div>
                    @endif
                </div>
                <div class="card-footer">
                    <a class="btn btn-primary btn-block add_point" href="#"><i class="fa fa-plus"></i></a>
                </div>
            </div>
            <div class="point_container d-none">
                @include('admin.products.point')
            </div>

            {{--Start image container--}}
            <div class="border-item photos col-md-12 other_image_container">
                <div class="row">
                    @if ($product ?? false)
                        @foreach ($product['images'] as $image)
                            <div class="col-md-4">
                                <div class="items">
                                    <div class="img">
                                        <a href="{{ asset($image ?? '')}}" data-fancybox="gallery"><img src="{{ asset($image ?? '')}}" alt=""></a>
                                    </div>
                                    <input type="hidden" name="images[]" value="{{ $image ?? "" }}">
                                    <a href="#" class="close"><i class="fas fa-times"></i></a>
                                </div>
                            </div>
                        @endforeach
                    @endif
                </div>
                <div class="text-center">
                    <input id="other_image_thumbnail" class="other_image_thumbnail" type="hidden"/>
                    <div class="clearfix"></div>
                    <span id="add">
                        <a id="lfm" data-width="" data-cache="article_gallery" data-input=".other_image_thumbnail" data-preview="" data-show="" data-hide="" class="lfm add">
                            {{__('dashboard.add_new_item')}}
                        </a>
                    </span>
                </div>
            </div>
            {{--End image container--}}

        <div class="text-center mt-5 col-12">
            <button class="butn add_product_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>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            currentLanguage = "{{$lang}}",
            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>

    <script>

        $('.add_product_button').click(async function (e) {
            e.preventDefault();
            $(this).prop('disabled', true);
            let form = $('.add_product_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){
                $(this).prop('disabled', false);
                toasterError(response.errors);
            }

        });


        $('.add_point').click(function (e) {
            e.preventDefault();
            let pointContainer = $('.point_container').html();
            $('.product_points_appended_container').append(pointContainer);
            $('.product_points_appended_container .appended_item input').each(function () {
                $(this).attr('name', $(this).data('name'));
                $(this).removeAttr('data-name')
            });

        });
        $(document).on('click', '.delete_point', function (e) {
            e.preventDefault();
            $(this).parents('.point_item').remove();
        })
    </script>

@endsection



