@extends('store.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>
            <li class="nav-item">
                <a href="#section_additions" 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.additions')}}</span>
                </a>
            </li>

            @if(@$product)
                <li class="nav-item">
                    <a href="#section_reasons" 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.reasons')}}</span>
                    </a>
                </li>
            @endif
        </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 {{ $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.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="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>

                </div>
                <div class="tab-pane  col-lg-12" id="section_images">

                    <div class="col-lg-12">
                        <h3> {{__('products.images')}} </h3>
                    </div>
                    <div class="row col-12">
                    <div class="form-group col-lg-6">
                        <label>{{ meta('image', getCurrentLocale()) ?? __('products.image') }} </label>
                        <input type="file" class="form-control {{ $errors->has('image') ? 'is-invalid' : '' }}" name="image" placeholder="{{ meta('image', getCurrentLocale()) ?? __('products.image') }}" value='{{ old("image", $product->image ?? "") }}'/>
                    </div>
                    
                    @if(isset($product))
                        <div class="form-group col-lg-6">
                            <a href="{{asset($product->image)}}" target="_blank">{{__('products.show')}}</a>
                        </div>
                    @endif
                    </div>
                    <div class="col-lg-12">
                        <h4>{{__('products.images_add')}}</h4>
                    </div>
                    <div class="col-lg-12" id="images">
                        @if(@$product)
                            @foreach($product->photos as $row)
                                @include('store.product.image',['image'=>$row])
                            @endforeach
                        @endif
                    </div>
                    <div class="col-lg-12">
                        <a style="cursor: pointer;" onclick="addimage(this)" class="btn_1 gray "><i class="fa fa-fw fa-plus-circle"></i>{{__("products.add_image")}}</a>
                    </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="product_items">
                            @if(@$product)
                                @foreach($items as $key=>$row)
                                    @include('store.product.product_item',['item'=>$row,'key'=>$key])
                                @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="addproductitem(this)" class="btn_1 gray "><i class="fa fa-fw fa-plus-circle"></i>{{__("products.add_new")}}</a>
                        </div>
                    </div>
                </div>
                <div class="tab-pane col-lg-12" id="section_additions">
                    <div class="row">
                        <div class="col-lg-12">
                            <h3> {{__('products.additions')}} </h3>
                        </div>
                        <div class="col-lg-12" id="additions">
                            @if(@$product)
                                @foreach($additions as $key=>$row)
                                    @include('store.product.addition',['addition'=>$row,'key'=>$key])
                                @endforeach
                            @endif
                        </div>
                        <div class="col-lg-12">
                            <a style="cursor: pointer;" @if(@$additions) data-id="{{count($additions)}}" @else data-id="0" @endif onclick="addaddition(this)" class="btn_1 gray "><i class="fa fa-fw fa-plus-circle"></i>{{__("products.add_addition")}}</a>
                        </div>
                    </div>
                </div>
                @if(@$product)
                    <div class="tab-pane col-lg-12" id="section_reasons">
                        <div class="row">
                            <div class="col-lg-12">
                                <h3> {{__('products.reasons')}} </h3>
                            </div>
                            <div class="col-lg-12">
                                @foreach($logs as $row)
                                    <div class="row pricing-list-item  mb-4 "  style="background-color: rgba(21,97,203,0.1);height:auto;padding: 20px">
                                        <div class="form-group col-lg-6">
                                            <label>{{  __('products.message') }} </label>
                                            <input type="text" class="form-control" disabled value="{{$row->message}}"/>
                                        </div>
                                        <div class="form-group col-lg-6">
                                            <label>{{  __('products.reason_id') }} </label>
                                            <input type="text" class="form-control" disabled value="{{$row->reason->currentDescription->title}}"/>
                                        </div>
                                        <div class="form-group col-lg-6">
                                            <label>{{  __('products.created_at') }} </label>
                                            <input type="text" class="form-control" disabled value="{{date('Y-m-d g:i a',strtotime($row->created_at))}}"/>
                                        </div>
                                    </div>
                                @endforeach
                            </div>
                        </div>
                    </div>
                @endif
            </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 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 addproductitem(elem)
        {
            var num = $(elem).attr('data-id');
            var newrow = parseInt(num)+1;
            $(elem).attr('data-id', newrow);

            $.ajax({
                url : url_product_item,
                type : 'GET',
                data : 'key='+num,
                success:function(data)
                {
                    $('#product_items').append(data);
                }
            })
        }
        function addaddition(elem)
        {
            var num = $(elem).attr('data-id');
            var newrow = parseInt(num)+1;
            $(elem).attr('data-id', newrow);

            $.ajax({
                url : url_addition,
                type : 'GET',
                data : 'key='+num,
                success:function(data)
                {
                    $('#additions').append(data);
                }
            })
        }

        function additem(elem)
        {
            var num = $(elem).attr('data-id');
            var key = $(elem).attr('key-id');
            var newrow = parseInt(num)+1;
            $(elem).attr('data-id', newrow);

            $.ajax({
                url : url_item,
                type : 'GET',
                data : 'keyItem='+num+"&key="+key,
                success:function(data)
                {
                    $('#items'+key).append(data);
                }
            })
        }

        function addimage(elem)
        {
            $.ajax({
                url : url_image,
                type : 'GET',
                data : '',
                success:function(data)
                {
                    $('#images').append(data);
                }
            })
        }

        function remove(elem)
        {
            $(elem).closest('.row').remove();
        }
    </script>
    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            url_addition = "{{route('store.addition')}}",
            url_product_item = "{{route('store.product.item')}}",
            url_item = "{{route('store.item')}}",
            url_model = "{{route('store.model')}}",
            url_image = "{{route('store.image')}}",
            currentLanguage = "{{getCurrentLocale()}}",
            imagePath = "{{asset('')}}";
    </script>
    <script src="{{ asset('assets/admin/js/editor.js') }}"></script>
    <script src="{{ asset('assets/admin/js/lfm.js') }}"></script>
@endsection



