@extends('admin.layouts.container')

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

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

        @foreach($languages as $language)
        <div class="form-group col-lg-6">
            <label>{{ meta('name', $language->local) ?? __('products.name') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
            <input type="text" id="name_{{ $language->local }}" 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>

       
       

        @endforeach
 <div class="form-group col-lg-6">
            <label>{{ meta('category_id', getCurrentLocale()) ?? __('products.category') }}</label>
            <select name="category_id" onchange="mainmodel(this)" id="category_id" class="form-control my-editor {{ $errors->has('category_id') ? 'is-invalid' : '' }}">
                <option value="">{{meta('category_id', getCurrentLocale()) ?? __('products.category')}}</option>
                @foreach ($categories as $category)
                    <option @if($product ?? ''  && $product->id) @if($product->category_id == $category->id) selected @endif @endif value="{{$category->id}}">{{descriptions($category)->name ?? ''}}</option>
                @endforeach
            </select>
        </div>

        <!--  <div class="form-group col-lg-6">
            <label>{{ meta('writer_id', getCurrentLocale()) ?? __('products.writer') }}</label>
            <select name="writer_id"  id="writer_id" class="form-control my-editor {{ $errors->has('writer_id') ? 'is-invalid' : '' }}">
                <option disabled="" selected="" value="">{{meta('writer_id', getCurrentLocale()) ?? __('products.select')}}</option>
                @foreach ($writers as $writer)
               <option {{old("writer_id", $product->writer_id ?? "") == $writer->id ? 'selected': ''}} value="{{$writer->id}}">{{ writername($writer->id) ?? ''}}  </option>
               @endforeach
            </select>
        </div> -->
        <!-- <select name="show"  id="show" class="form-control my-editor {{ $errors->has('show') ? 'is-invalid' : '' }}">
                <option disabled="" selected="" value="">{{meta('show', getCurrentLocale()) ?? __('products.select')}}</option>
                <option @if($product ?? ''  && $product->id) @if($product->show == 'yes') selected @endif  @endif value="yes">{{__('products.yes')}}</option>
                <option  @if($product ?? ''  && $product->id) @if($product->show == 'no') selected @endif  @endif value="no">{{__('products.no')}}</option>
            </select> -->
        <div class="form-group col-lg-6">
            <h5>{{ meta('show', getCurrentLocale()) ?? __('products.show') }}</h5>
            
            <div class="row">
              <div class="form-group col-lg-3">
            <input  name="show" type="radio" @if($product ?? ''  && $product->id) @if($product->show == 'yes') checked @endif  @endif value="yes"> {{__('products.yes')}}
            </div>
            <div class="form-group col-lg-6">
            <input  name="show" type="radio" @if($product ?? ''  && $product->id) @if($product->show == 'no') checked @endif  @endif value="no"> {{__('products.no')}}
          </div>
          </div>
        </div>
        <div class="form-group col-lg-6"></div>
<div class="form-group col-lg-6">
            <label>{{ meta('writer', $language->local) ?? __('products.writer')}} </label>
            <input type="text" name="writer" id="writer"  class="form-control {{ $errors->has('writer') ? 'is-invalid' : '' }}" placeholder="{{ meta('writer', $language->local) ?? __('products.writer')}}" value='{{ old("writer", $product->writer ?? "") }}'>
        </div>
       
        <div class="form-group col-lg-6">
            <label>{{ meta('model_id', getCurrentLocale()) ?? __('products.main_model') }}</label>
            <select name="model_id" onchange="model(this)" id="model_id" class="form-control my-editor {{ $errors->has('model_id') ? 'is-invalid' : '' }}">
                <option value="">{{meta('model_id', getCurrentLocale()) ?? __('products.selectcategory')}}</option>
                 @if($product ?? ''  && $product->id)
                @foreach($models as $row)
                <option @if($product->model_id == $row->id) selected @endif value="{{$row->id}}">{{modelname($row->id)}}</option>
                @endforeach
                @endif
            </select>
        </div>
      
        
        
        
       
       
       
        

        @foreach($languages as $language)
      

       
        <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 }}" id="description_{{ $language->local }}"  class="form-control my-editor {{ $errors->has('description_'.$language->local) ? 'is-invalid' : '' }}" placeholder="{{ meta('description', $language->local) ?? __('products.description')}}">{{ old("description_{$language->local}", $product[$language->local]->description ?? "") }}</textarea>
        </div>

        @endforeach
        
       
        
   <!--     
<div class="form-group col-lg-12 banner_container">
            <label>{{ meta('banner', getCurrentLocale()) ?? __('products.banner') }} </label>
            <input id="banner_thumbnail" class="banner_thumbnail" type="hidden" name="banner" value="{{old("banner", $product->banner ?? "")}}"/>

            <div class="photos mainbanner_photo {{ old('banner', $product->banner ?? '') ? ' wow fadeInUp' : 'hide' }}">
                <div class="col-md-4">
                    <div class="items">
                        <div class="img">
                            <a href="#" id="lfm" data-input=".banner_thumbnail" data-preview=".banner_holder" class="lfm" data-hide=".pickbanner_button">
                                <img src="{{asset(old('banner', $product->banner ?? ''))}} "  id="banner_holder" class="banner_holder"/>
                            </a>
                        </div>
                        <a href="#" class="showHide resetImage close" data-empty=".banner_thumbnail" data-hide=".mainbanner_photo" data-show=".pickbanner_button"><i class="fas fa-times"></i></a>
                    </div>
                </div>
            </div>

            <div class="clearfix"></div>
            <span class="input-group-btn pickbanner_button {{ old('banner', $product->banner ?? '') ? 'hide' : '' }} ">
                <a id="lfm"  data-input=".banner_thumbnail" data-preview=".banner_holder" data-show=".mainbanner_photo" data-hide=".pickbanner_button" class="lfm btn btn-primary text-light">
                    <i class="fas fa-camera-retro"></i> Choose
                </a>
             </span>
        </div>

<div class="form-group col-lg-6">
            <label>{{ meta('link', $language->local) ?? __('products.link')}} </label>
            <input type="text" name="link" id="link"  class="form-control {{ $errors->has('link') ? 'is-invalid' : '' }}" placeholder="{{ meta('link', $language->local) ?? __('products.link')}}" value='{{ old("link", $product->link ?? "") }}'>
        </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> 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>
                                    <input type="text" class="form-control" name="links[]" value="{{$photo->link}}">
                                </div>
                                <input type="hidden" name="images[]" value="{{ $photo->image ?? "" }}">
                                <a href="#" onclick="deleteimage(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">
                        اضافة ملف جديد
                    </a>
                </span>
            </div>
        </div>
      

        {{--End image container--}}

        <div class="text-center mt-5 col-12">
            <button class="butn"><span>{{ __('dashboard.save') }}</span></button>
        </div>
    </form>
</div>

@endsection

@section('inner_js')
<script type="text/javascript">

window.onload = function() {
    var latlng = new google.maps.LatLng({!! $product ?? ''  && $product->id ? $product->latlng : '21.4989, 39.2509' !!});
    var map = new google.maps.Map(document.getElementById('map'), {
        center: latlng,
        zoom: 11,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: 'Set lat/lon values for this property',
        draggable: true
    });
    google.maps.event.addListener(marker, 'dragend', function(a) {
        console.log(a);
        var div = document.createElement('div');
        div.innerHTML = a.latLng.lat().toFixed(4) + ', ' + a.latLng.lng().toFixed(4);
        document.getElementsByTagName('body')[0].appendChild(div);
         $("#latlng").val(function() {
    return  div.innerHTML;

    
});

    });
    
};

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    
   
    var latlng = new google.maps.LatLng(position.coords.latitude , position.coords.longitude);
      $("#mylatlng").val(function() {
    return  latlng;

    
});
    var map = new google.maps.Map(document.getElementById('map'), {
        center: latlng,
        zoom: 11,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: 'Set lat/lon values for this property',
        draggable: true
    });
    google.maps.event.addListener(marker, 'dragend', function(a) {
        console.log(a);
        var div = document.createElement('div');
        div.innerHTML = a.latLng.lat().toFixed(4) + ', ' + a.latLng.lng().toFixed(4);
       
        

    });
}

</script>
<script>
function saveproduct(elem){
    
       event.preventDefault();
       $('form *').removeClass('is-invalid');
       var action = $(elem).attr('action');
       
       var urls = action;
       var dataform = new FormData( $(elem)[0]);
      
       $.ajax({
          url : urls,
          type : 'POST',
          data : dataform,
          dataType : 'json',
  processData: false,
  contentType: false,
          success:function(data)
          {
              window.location.replace("{{url(getCurrentLocale().'/dashboard/products')}}");
          }
          ,error: function (data) {
              
              $('.ajaxResponse').css("display", "none");
    $('.ajaxErrorResponse').css("display", "block");
   $('.ajaxErrorResponse').html('');
   $.each(data.responseJSON.errors, function(key,value) {
     $('.ajaxErrorResponse').append('<div class="alert alert-danger">'+value+'</div');
     console.log(key);
     $('#'+key).attr('class','form-control is-invalid');
     
 }); 
 
}
        })
   
}
function countries(elem){
     var data = $(elem).val();
         
        
        
         $.ajax({
          url : url_country+'/'+data,
          type : 'GET',
          data : 'nothing',
          
          success:function(data)
          {
            $('#city_id').html(data);
          }
        })   
}
    function mainmodel(elem){
      
        var data = $(elem).val();
        
        
        
         $.ajax({
          url : url_main+'/'+data,
          type : 'GET',
          data : 'nothing',
          
          success:function(data)
          {
            $('#model_id').html(data);
          }
        })   
        $.ajax({
          url : url_attr+'/'+data,
          type : 'GET',
          data : 'nothing',
          
          success:function(data)
          {
               $("#attributes").css("display", "block");
            $('#attributes').html(data);
          }
        })   
    }
    function model(elem){
          var data = $(elem).val();
         
        
        
         $.ajax({
          url : url_model+'/'+data,
          type : 'GET',
          data : 'nothing',
          
          success:function(data)
          {
            $('#model_id').html(data);
          }
        })   
    }
    function submodel(elem){
         var data = $(elem).val();
         
        
        
         $.ajax({
          url : url_model+'/'+data,
          type : 'GET',
          data : 'nothing',
          
          success:function(data)
          {
            $('#sub_id').html(data);
          }
        })  
    }
    function deleteimage(elem){
            setTimeout(function(){ $(elem).closest('.col-md-4').remove(); }, 500);
            
        }
</script>
    <!-- Editor JS files -->
  <script src="{{ asset('/assets/admin/js/lib/tinymce.min.js') }}"></script>
        
    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            currentLanguage = "{{getCurrentLocale()}}",
            url_main = {!! json_encode(url(getCurrentLocale().'/dashboard/main/')) !!},
            url_model = {!! json_encode(url(getCurrentLocale().'/dashboard/model/')) !!},
            url_attr = {!! json_encode(url(getCurrentLocale().'/dashboard/attr/')) !!},
            url_country = {!! json_encode(url(getCurrentLocale().'/dashboard/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>
@endsection



