/*
 * Custom code goes here.
 * A template should always ship with an empty custom.js
 */
$(document).ready(function () {
    $("#scroll-view .qty-btn").click(function() {
        var qty = $('#scroll-view-qty').val();
        $('.product-add-to-cart input[name="qty"]').val(qty);
    });
    // $('.gift').each(function(){
    //     let p = $(this).parents()[2]
    //     $(p).appendTo('#gift-items')
    // })
});

// const $map_preview=$('#map_preview');

// function initMap(){
// console.log('inside init map')
// let target_point = { lat: 46.372768, lng:  1.998769 };
// if($("#map_list li").length>0){
//     target_point = { lat: parseFloat($('#map_list li:first-child').attr('data-lat')), lng:  parseFloat($('#map_list li:first-child').attr('data-lng')) };
// }
// const map = new google.maps.Map(document.getElementById("map_preview"), {
//     zoom: 4,
//     center: target_point,
// });

// $('#map_list li').each(function(key,item){
//     if($(this).attr('data-lat')){
    
//     const locate = { lat: parseFloat($(this).attr('data-lat')), lng:  parseFloat($(this).attr('data-lng')) };
//     const marker = new google.maps.Marker({
//         position: locate,
//         map: map,
//     });

//     }
// })}





$( document ).ready(function() {
    
    if ($( ".date-picker" ).length > 0) {
        $( ".date-picker" ).datepicker({
            dateFormat: 'dd/mm/yy',
            changeYear: true,
            changeMonth: true,
            yearRange: "-115:+0"
        });
        $('.date-picker').on('change', function(){
            try {
                $.datepicker.parseDate("dd/mm/yy", $(this).val())
            } catch (e) {
                $(this).val('')
            }
        })
    }

    if($(".js-value-shipping").attr("data-amount")!=0){
        $('.mes-bonus-box .mes-bonus__item').css('background-size','0')
        $('.mes-bonus-box .mes-bonus__item').css('text-decoration','line-through')
    }

    prestashop.on('updatedCart', () => {
        if($('.cart-detailed-totals').length>0){
            $('.cart-hide-empty').show()
            $('.product-selection-section').show()
        }else{
            $('.cart-hide-empty').hide()
            $('.product-selection-section').hide()
        }
        if($(".js-value-shipping").attr("data-amount")==0){
            $('.mes-bonus-box .mes-bonus__item').css('background-size','20px')
            $('.mes-bonus-box .mes-bonus__item').css('text-decoration','none')

        }else{
            $('.mes-bonus-box .mes-bonus__item').css('background-size','0')
            $('.mes-bonus-box .mes-bonus__item').css('text-decoration','line-through')

        }
    });

    prestashop.on('updatedProduct', function (data) {
        const $productFacadeSlider = $('.product-facade__slider');
        $productFacadeSlider.removeClass('slick-initialized slick-slider slick-dotte');
        $productFacadeSlider.html(data.product_images);
        if ($productFacadeSlider.length > 0) {
            $productFacadeSlider.slick({
                arrows: false,
                dots: true,
                slidesToShow: 1
            });
        }
    });

    $('#gc-success-modal').on('shown.bs.modal', function (e) {
        alert('a')
       $('#modal_gift').modal('hidden')
      })
      
      let target_point = { lat: 46.372768, lng:  1.998769 };
      const defaultBounds = {
        north: target_point.lat + 0.1,
        south: target_point.lat - 0.1,
        east: target_point.lng + 0.1,
        west: target_point.lng - 0.1,
      }
      const input_au = document.getElementsByName("address1")[0]
      const input_a2 = document.getElementsByName("address2")[0]
      const input_ct = document.getElementsByName('city')[0]
      const input_pc = document.getElementsByName('postcode')[0]
      const options = {
          bounds: defaultBounds,
          fields: ["address_components", "geometry", "icon", "name"],
          types: ['address'],
          origin: target_point,
          strictBounds: false
      };
      if(input_au){
          const autocomplete = new google.maps.places.Autocomplete(input_au, options);
          autocomplete.addListener("place_changed", function(){
            const place = autocomplete.getPlace();
            let address1 = "";
            let postcode = "";
            for (const component of place.address_components) {
                // @ts-ignore remove once typings fixed
                const componentType = component.types[0];
            
                switch (componentType) {
                  case "street_number": {
                    address1 = `${component.long_name} ${address1}`;
                    break;
                  }
            
                  case "route": {
                    address1 += component.short_name;
                    break;
                  }
            
                  case "postal_code": {
                    postcode = `${component.long_name}${postcode}`;
                    break;
                  }

                  case "locality":
                    input_ct.value = component.long_name;
                    break;
            
                }
            }

            input_au.value = address1;
            input_pc.value = postcode;
            
            input_a2.focus()
         
          });
          $(input_au).on('keypress',function(e) {
              if(e.which == 13) {
                  e.preventDefault();
              }
          });
    }

    const input_au_pro = document.getElementsByName("ob-getpro-adress")[0]
    const input_ct_pro = document.getElementsByName('ob-getpro-city')[0]
    const input_pc_pro = document.getElementsByName('ob-getpro-postal-code')[0]

    if(input_au_pro){
        const autocomplete_pro = new google.maps.places.Autocomplete(input_au_pro, options);
        // console.log(autocomplete_pro)
        autocomplete_pro.addListener("place_changed", function(){
          const place = autocomplete_pro.getPlace();
        //   console.log(place)
          let address1 = "";
          let postcode = "";
          for (const component of place.address_components) {
              // @ts-ignore remove once typings fixed
              const componentType = component.types[0];
          
              switch (componentType) {
                case "street_number": {
                  address1 = `${component.long_name} ${address1}`;
                  break;
                }
          
                case "route": {
                  address1 += component.short_name;
                  break;
                }
          
                case "postal_code": {
                  postcode = `${component.long_name}${postcode}`;
                  break;
                }

                case "locality":
                  input_ct_pro.value = component.long_name;
                  break;
          
              }
          }
          input_au_pro.value = address1;
          input_pc_pro.value = postcode;
          
        });

        $(input_au).on('keypress',function(e) {
            if(e.which == 13) {
                e.preventDefault();
            }
        });

        $(input_au_pro).on('keypress',function(e) {
            if(e.which == 13) {
                e.preventDefault();
            }
        });
    }  

    $('.js-country').on('focus', function(){
        $(this).attr('size',10)
      })
      $('.js-country').on('blur', function(){
      $(this).attr('size',1)
      })
      $('.js-country').on('change', function(){
      $(this).attr('size',1)
      $(this).trigger('blur')
      })
      $('[data-toggle="tooltip"]').tooltip({
        trigger : 'hover focus'
      })
});
