@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_images" 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.images')}}</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.status_active') }} </label>
                            <select id="status_active" class="form-control {{ $errors->has('status_active') ? 'is-invalid' : '' }}" name="status_active">
                                <option value="">{{__('dashboard.select')}}</option>
                                @foreach($statuses as $row)
                                    <option {{ (isset($product) ? $product->status_active : null) == $row ? 'selected' : '' }} value="{{$row}}">{{ __('products.active_statuses.'.$row) }}</option>
                                @endforeach
                            </select>
                        </div>
                        <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>
                                @foreach($sub_categories as $row)
                                    <option {{ (isset($product) ? $product->sub_category_id : null) == $row->id ? 'selected' : '' }} value="{{$row->id}}">{{$row->title}}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-group col-lg-6">
                            <label>{{ __('products.store_id') }} </label>
                            <select id="store_id" class="form-control {{ $errors->has('store_id') ? 'is-invalid' : '' }}" name="store_id">
                                <option value="">{{__('dashboard.select')}}</option>
                                @foreach($stores as $row)
                                    <option {{ (isset($product) ? $product->store_id : null) == $row->id ? 'selected' : '' }} value="{{$row->id}}">{{$row->title}}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-group col-lg-6">
                            <label>{{ __('products.brand_id') }} </label>
                            <select id="brand_id" class="form-control {{ $errors->has('brand_id') ? 'is-invalid' : '' }}" name="brand_id">
                                <option value="">{{__('dashboard.select')}}</option>
                                @foreach($brands as $row)
                                    <option {{ (isset($product) ? $product->brand_id : null) == $row->id ? 'selected' : '' }} value="{{$row->id}}">{{$row->title}}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-group col-lg-6">
                            <label>{{ __('products.attribute_id') }} </label>
                            <select id="attribute_id" class="form-control {{ $errors->has('attribute_id') ? 'is-invalid' : '' }}" name="attribute_id">
                                <option value="">{{__('dashboard.select')}}</option>
                                @foreach($attributes as $row)
                                    <option {{ (isset($product) ? $product->attribute_id : null) == $row->id ? 'selected' : '' }} value="{{$row->id}}">{{$row->title}}</option>
                                @endforeach
                            </select>
                        </div>
                        <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>

                        @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 {{ $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.discount_percentage') }} </label>
                            <input id="discount_percentage" type="number" class="form-control {{ $errors->has('discount_percentage') ? 'is-invalid' : '' }}" name="discount_percentage" placeholder="{{ __('products.discount_percentage') }}" value='{{ old("discount_percentage", $product->discount_percentage ?? "") }}'/>
                        </div>
                        <div class="form-group col-lg-6" >
                            <label>{{ __('products.max_count_order') }} </label>
                            <input type="text" class="form-control {{ $errors->has('max_count_order') ? 'is-invalid' : '' }}" name="max_count_order" placeholder="{{ __('products.max_count_order') }}" value='{{ old("max_count_order", $product->max_count_order ?? "") }}'/>
                        </div>
{{--
                        <div class="form-group col-lg-6 none_attr" >
                            <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 none_attr" >
                            <label>{{ __('products.cost') }} </label>
                            <input type="text" onkeyup="AddDicount()" id="cost" 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 none_attr" >
                            <label>{{ __('products.discount_percentage') }} </label>
                            <input id="discount_percentage" onkeyup="AddDicount()" type="text" class="form-control {{ $errors->has('discount_percentage') ? 'is-invalid' : '' }}" name="discount_percentage" placeholder="{{ __('products.discount_percentage') }}" value='{{ old("discount_percentage", $product->discount_percentage ?? "") }}'/>
                        </div>

                        <div class="form-group col-lg-6 none_attr" >
                            <label>{{ __('products.cost_discount') }} </label>
                            <p class="form-control" id="cost_discount" ></p>
                        </div>
--}}

                    </div>

                </div>
                <div class="tab-pane col-lg-12" id="section_images">
                    <div class="row">
                        <div class="col-lg-12">
                            <h3> {{__('products.images')}} </h3>
                        </div>
                        <div class="form-group col-lg-12 image_container" >
                            <label>{{ meta('image', getCurrentLocale()) ?? __('products.image') }} </label>
                            <input id="image_thumbnail" class="image_thumbnail" type="hidden" name="image" value="{{old("image", $product->image ?? "")}}"/>

                            <div class="photos main_photo {{ old('image', $product->image ?? '') ? ' wow fadeInUp' : 'hide' }}">
                                <div class="col-md-4">
                                    <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', $product->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', $product->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> {{__('dashboard.choose')}}
                </a>
             </span>
                        </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">
                                    {{__('products.add_photos')}}
                                </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>
                                        @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>
                                        @foreach($languages as $language)
                                            <div class="form-group col-lg-6">
                                                <label>{{  __('products.description') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
                                                <textarea class="form-control" id="items_{{$key}}_description_{{ $language->local }}"  name="items[{{$key}}][description_{{ $language->local }}]" placeholder="{{ __('products.description') }} {{ $language->local }}">{{$row->descriptions($language->id)->title}}</textarea>
                                            </div>
                                        @endforeach
                                    </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>
        function DeleteImg(elem)
        {
            $(elem).closest('.col-md-4').remove();
        }
    </script>
    <script>
        function selectAttribute()
        {
            let attr = $('#attribute_id').val()
            if(attr)
            {
                $('.none_attr').hide()
            }
            else
            {
                $('.none_attr').show()
            }
        }
        $('#attribute_id').trigger('change')
        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 AddDicount()
        {
            let cost = $('#cost').val()
            let discount_percentage = $('#discount_percentage').val()
            if(cost && discount_percentage)
            {
                let discount = cost * ( discount_percentage / 100 )
                $('#cost_discount').html(parseFloat(cost - discount))
            }
            else
            {
                $('#cost_discount').html('')
            }
        }
        $('#discount_percentage').trigger('keyup')

        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.subCategories')}}",
            currentLanguage = "{{getCurrentLocale()}}",
            imagePath = "{{asset('')}}";
    </script>
    <script src="{{ asset('assets/admin/js/editor.js') }}"></script>
    <script src="{{ asset('assets/admin/js/lfm.js') }}"></script>
@endsection



