@extends('admin.layouts.container')

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

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

            @foreach($languages as $language)
                <div class="form-group col-lg-6">
                    <label>{{ meta('name', $language->local) ?? __('shippings.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) ?? __('shippings.name') }}" value='{{ old("name_{$language->local}", $shipping[$language->local]->name ?? "") }}'/>
                </div>
            @endforeach
            <div class="form-group col-lg-6">
                <label>{{ __('shippings.min_order') }} </label>
                <input type="number" class="form-control" step=".01" name="min_order" placeholder="{{ meta('min_order', getCurrentLocale()) ?? __('shippings.min_order') }}" value='{{ old("min_order",  $shipping->min_order ?? '') }}'/>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ __('shippings.discount') }} </label>
                <input type="number" class="form-control" step=".01" name="discount" placeholder="{{ meta('discount', getCurrentLocale()) ?? __('shippings.discount') }}" value='{{ old("discount",  $shipping->discount ?? '' )}}'/>
            </div>
            
{{--            Start image container--}}
                    <div class="form-group col-lg-6">
                        <label>{{ meta('image', $lang) ?? __('shippings.image') }} </label>
                        <input id="image_thumbnail" class="image_thumbnail" type="hidden" name="image" value="{{old("image", $shipping->image ?? null)}}"/>

                        <div class="photos main_photo {{ old('image', $shipping->image ?? null) ? ' wow fadeInUp' : 'hide' }}">
                            <div class="col-lg-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="{{($shipping->image ?? null) ? asset(old('image', $shipping->image ?? null)) : ''}} "  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', $shipping->image ?? null) ? '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="col-12">
                    <div  class="row pricing-list-item border-item mb-4" style="background-color: white;height:auto">
                        <div class="col-lg-12">
                            <h3> {{__('shippings.cities')}} </h3>
                            <div class="col-lg-12" style="height:100%;" id="cities">
                                @if($shipping ?? ''  && $shipping->id)
                                    @foreach($shippingCities as $key=>$city)
                                        <div class="row pricing-list-item border-item mb-4"  style="background-color: rgba(21,97,203,0.1);height:auto">

                                            <div class="form-group col-lg-6">
                                                <label>{{ __('shippings.tax') }} </label>
                                                <input type="number"  class="form-control {{ $errors->has('tax') ? 'is-invalid' : '' }}" name="cities[{{$key}}][tax]" placeholder="{{  __('shippings.tax') }}" value='{{  $city->tax }}'/>
                                            </div>
                                            <div class="form-group col-lg-6">
                                                <label>{{ __('shippings.country_id') }} </label>
                                                <select onchange="citySelect(this)" data-id="{{$key}}" name="cities[{{$key}}][country_id]"  class="form-control">
                                                    <option value="">{{__('dashboard.select')}}</option>
                                                    @foreach($countries as $row)
                                                        <option {{$city->city->country_id == $row->id ? 'selected' : ''}} value="{{$row->id}}">{{ $row->name }}</option>
                                                    @endforeach
                                                </select>
                                            </div>
                                            <div class="form-group col-lg-6">
                                                <label>{{ __('shippings.city_id') }} </label>
                                                <select id="city_id{{$key}}" name="cities[{{$key}}][city_id]"  class="form-control">
                                                    <option value="">{{__('dashboard.select')}}</option>
                                                    @foreach($cities as $row)
                                                        <option {{$city->city_id == $row->id ? 'selected' : ''}} value="{{$row->id}}">{{ $row->name }}</option>
                                                    @endforeach
                                                </select>
                                            </div>
    
    @foreach(languages() as $lang)
        <div class="form-group col-lg-6">
            <label>{{ __('shippings.timing') }} {{$lang->local}}</label>
            <input type="text"  class="form-control {{ $errors->has('tax') ? 'is-invalid' : '' }}" name="cities[{{$key}}][timing_{{$lang->local}}]" placeholder="{{  __('shippings.timing') }} {{$lang->local}}" value="{{$city->timingLang($lang->local)}}"/>
        </div>
    @endforeach
                                            <div class="form-group col-lg-4"></div>
                                            <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>
                        <div class="col-lg-12">
                            <a style="cursor: pointer;" @if($shipping ?? ''  && $shipping->id) data-id="{{count($cities)}}" @else data-id="0" @endif onclick="addcity(this)" class="btn_1 gray "><i class="fa fa-fw fa-plus-circle"></i>{{__("shippings.addcity")}}</a>
                        </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')
    <script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
    <script>
        function addcity(elem)
        {
            var num = $(elem).attr('data-id');
            var newrow = parseInt(num)+1;
            $(elem).attr('data-id', newrow);

            $.ajax({
                url : url_addCity,
                type : 'GET',
                data : 'key='+num,
                success:function(data)
                {
                    $('#cities').append(data);
                }
            })
        }
        function remove(elem){
            $(elem).closest('.row').remove();
        }
        function citySelect(elem)
        {
            var data = $(elem).val();
            let target = $(elem).attr('data-id');
            $.ajax({
                url : url_country,
                type : 'GET',
                data : 'id='+data,
                success:function(data)
                {
                    $('#city_id'+target).html(data);
                }
            })
        }
    </script>
    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            url_addCity = {!! json_encode(url(getCurrentLocale().'/admin/shippings/addCity')) !!},
            currentLanguage = "{{getCurrentLocale()}}",
            url_country = "{{url('/')}}/admin/country",
            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 src="{{ asset('assets/admin/js/citys.js') }}"></script>

@endsection



