@extends('admin.layouts.container')

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



<div class="border-item">
    <ul class="nav nav-pills bg-nav-pills nav-justified mb-3">
        <li class="nav-item">
            <a href="#main" data-toggle="tab" aria-expanded="false" class="nav-link rounded-0 active">
                <i class="mdi mdi-home-variant d-lg-none d-block mr-1"></i>
                <span class="d-none d-lg-block">{{__('products.details')}}</span>
            </a>
        </li>
        <li class="nav-item">
            <a href="#section_items" data-toggle="tab" aria-expanded="false" class="nav-link rounded-0">
                <i class="mdi mdi-home-variant d-lg-none d-block mr-1"></i>
                <span class="d-none d-lg-block">{{__('products.items')}}</span>
            </a>
        </li>

    </ul>
    <form class="form-row" method="post" onsubmit="saveForm(this)" action="{{ $action }}" enctype="multipart/form-data">
        @if($product ?? ''  && $product->id)
            @method('PUT')
        @endif
        @csrf
        <div class="tab-content row col-12">
            <div class="tab-pane col-lg-12 active" id="main">
                <div class="form-row">


                    <div class="form-group col-lg-6">
                        <label>{{ __('products.category_id') }} </label>
                        <select onchange="categorySelect(this)" id="category_id" class="form-control {{ $errors->has('category_id') ? 'is-invalid' : '' }}" name="category_id">
                            <option value="">{{__('dashboard.select')}}</option>
                            @foreach($categories as $row)
                                <option {{ (isset($product) ? $product->category_id : null) == $row->id ? 'selected' : '' }} value="{{$row->id}}">{{$row->title}}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="form-group col-lg-6">
                        <label>{{ __('products.sub_category_id') }} </label>
                        <select id="sub_category_id" class="form-control {{ $errors->has('sub_category_id') ? 'is-invalid' : '' }}" name="sub_category_id">
                            <option value="">{{__('dashboard.select')}}</option>
                            @if(isset($sub_categories))
                                @foreach($sub_categories as $row)
                                    <option {{ (isset($product) ? $product->sub_category_id : null) == $row->id ? 'selected' : '' }} value="{{$row->id}}">{{$row->title}}</option>
                                @endforeach
                            @endif
                        </select>
                    </div>

                    @foreach($languages as $language)
                        <div class="form-group col-lg-6">
                            <label>{{ meta('title', $language->local) ?? __('products.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) ?? __('products.title') }}" value='{{ old("title_{$language->local}", $product[$language->local]->title ?? "") }}'/>
                        </div>
                        <div class="form-group col-lg-12">
                            <label>{{ meta('description', $language->local) ?? __('products.description') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
                            <textarea  class="form-control my-editor{{ $errors->has('description_'.$language->local) ? 'is-invalid' : '' }}" name="description_{{ $language->local }}" placeholder="{{ meta('description', $language->local) ?? __('products.description') }}">{{ old("description_{$language->local}", $product[$language->local]->description ?? "") }}</textarea>
                        </div>
                    @endforeach


                    <div class="form-group col-lg-6" >
                        <label>{{ __('products.code') }} </label>
                        <input type="text" class="form-control {{ $errors->has('code') ? 'is-invalid' : '' }}" name="code" placeholder="{{ __('products.code') }}" value='{{ old("code", $product->code ?? "") }}'/>
                    </div>

                    <div class="form-group col-lg-6" >
                        <label>{{ __('products.count') }} </label>
                        <input type="text" class="form-control {{ $errors->has('count') ? 'is-invalid' : '' }}" name="count" placeholder="{{ __('products.count') }}" value='{{ old("count", $product->count ?? "") }}'/>
                    </div>

                    <div class="form-group col-lg-6" >
                        <label>{{ __('products.warranty') }} </label>
                        <input type="text" class="form-control {{ $errors->has('warranty') ? 'is-invalid' : '' }}" name="warranty" placeholder="{{ __('products.warranty') }}" value='{{ old("warranty", $product->warranty ?? "") }}'/>
                    </div>

                    <div class="form-group col-lg-6" >
                        <label>{{ __('products.cost') }} </label>
                        <input type="text" class="form-control {{ $errors->has('cost') ? 'is-invalid' : '' }}" name="cost" placeholder="{{ __('products.cost') }}" value='{{ old("cost", $product->cost ?? "") }}'/>
                    </div>

                    <div class="form-group col-lg-6" >
                        <label>{{ __('products.cost_discount') }} </label>
                        <input type="text" class="form-control {{ $errors->has('cost_discount') ? 'is-invalid' : '' }}" name="cost_discount" placeholder="{{ __('products.cost_discount') }}" value='{{ old("cost_discount", $product->cost_discount ?? "") }}'/>
                    </div>


                    <div class="border-item photos col-md-12 other_image_container">
                        <div class="row">
                            @if ($product ?? false)
                                @foreach ($product['photos'] as $photo)
                                    <div class="col-md-4">
                                        <div class="items">
                                            <div class="img">
                                                <a href="{{ asset($photo->image ?? '')}}" data-fancybox="gallery"><img src="{{ asset($photo->image ?? '')}}" alt=""></a>
                                            </div>
                                            <input type="hidden" name="images[]" value="{{ $photo->image ?? "" }}">
                                            <a href="#" onclick="DeleteImg(this)" 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">
                                    Add New Item
                                </a>
                            </span>
                        </div>
                    </div>

                </div>

            </div>
            <div class="tab-pane col-lg-12" id="section_items">
                <div class="row">
                    <div class="col-lg-12">
                        <h3> {{__('products.items')}} </h3>
                    </div>
                    <div class="col-lg-12" id="items">
                        @if($product ?? ''  && $product->id)
                            @foreach($items as $key=>$row)
                                <div class="row pricing-list-item  mb-4 "  style="background-color: rgba(21,97,203,0.1);height:auto;padding: 20px">
                                        @foreach($languages as $language)
                                            <div class="form-group col-lg-5">
                                                <label>{{  __('products.title') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
                                                <input type="text" class="form-control" id="items_{{$key}}_title_{{ $language->local }}"  name="items[{{$key}}][title_{{ $language->local }}]" placeholder="{{ __('products.title') }} {{ $language->local }}" value="{{$row->descriptions($language->id)->title}}"/>
                                            </div>
                                            <div class="form-group col-lg-5">
                                                <label>{{  __('products.description') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
                                                <input type="text" class="form-control" id="items_{{$key}}_description_{{ $language->local }}"  name="items[{{$key}}][description_{{ $language->local }}]" placeholder="{{ __('products.description') }} {{ $language->local }}" value="{{$row->descriptions($language->id)->description}}"/>
                                            </div>

                                        @endforeach
                                        <div class="form-group col-lg-2" style="font-size: 30px;margin-top:35px;">
                                            <i class="fas fa-times-circle" onclick="remove(this)"></i>
                                        </div>
                                </div>
                            @endforeach
                        @endif
                    </div>
                    <div class="col-lg-12">
                        <a style="cursor: pointer;" @if($product ?? ''  && $product->id) data-id="{{count($items)}}" @else data-id="0" @endif onclick="additem(this)" class="btn_1 gray "><i class="fa fa-fw fa-plus-circle"></i>{{__("products.add_new")}}</a>
                    </div>
                </div>
            </div>
        </div>


            <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="//cdn.tinymce.com/4/tinymce.min.js"></script>--}}
    <script src="{{ asset('/assets/admin/js/lib/tinymce.min.js') }}"></script>
{{--    <script>--}}
{{--        tinymce.init({--}}
{{--            selector: '.description_ar',--}}
{{--            init_instance_callback: function (editor) {--}}
{{--                editor.on('change', function (e) {--}}
{{--                    $('#description_ar').val(e.level.content);--}}
{{--                });--}}
{{--            }--}}
{{--        });--}}
{{--        tinymce.init({--}}
{{--            selector: '.description_en',--}}
{{--            init_instance_callback: function (editor) {--}}
{{--                editor.on('change', function (e) {--}}
{{--                    $('#description_en').val(e.level.content);--}}
{{--                });--}}
{{--            }--}}
{{--        });--}}
{{--    </script>--}}
    <script>
        function DeleteImg(elem)
        {
            $(elem).closest('.col-md-4').remove();
        }
    </script>
    <script>
        function categorySelect(elem)
        {
            let category_id = $(elem).val()
            $.ajax({
                url : url_model,
                type : 'GET',
                data : 'category_id='+category_id,
                success:function(data)
                {
                    $('#sub_category_id').html(data);
                }
            })
        }
        function additem(elem)
        {
            var num = $(elem).attr('data-id');
            var newrow = parseInt(num)+1;
            $(elem).attr('data-id', newrow);

            $.ajax({
                url : url_item,
                type : 'GET',
                data : 'key='+num,
                success:function(data)
                {
                    $('#items').append(data);
                }
            })
        }
        function remove(elem)
        {
            $(elem).closest('.row').remove();
        }
    </script>
    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            url_item = "{{route('admin.item')}}",
            url_model = "{{route('admin.model')}}",
            currentLanguage = "{{getCurrentLocale()}}",
            imagePath = "{{asset('')}}";
    </script>
    <script src="{{ asset('assets/admin/js/editor.js') }}"></script>
    <script src="{{ asset('assets/admin/js/lfm.js') }}"></script>
@endsection



