// show mobile menu content
function show_menu() {
  const menu = document.getElementsByClassName('hamburger')[0];
  if (!menu.classList.contains('active')) {
    menu.classList.add('active');
  } else {
    menu.classList.remove('active');
  }
}
(function ($) {

    /*------------------
        Preloader
    --------------------*/
    $(window).on('load', function () {
        $(".loader").delay(1300).fadeOut();
        $("#preloder").delay(1300).fadeOut("slow");

        /*------------------
            Gallery filter
        --------------------*/
        $('.filter__controls li').on('click', function () {
            $('.filter__controls li').removeClass('active');
            $(this).addClass('active');
        });
        if ($('.product__filter').length > 0) {
            var containerEl = document.querySelector('.product__filter');
            var mixer = mixitup(containerEl);
        }
    });

    //Search Switch
    $('.search-switch').on('click', function () {
        $('.search-model').fadeIn(400);
    });

    $('.search-close-switch').on('click', function () {
        $('.search-model').fadeOut(400, function () {
            $('#search-input').val('');
        });
    });

    /*-------------------
		Radio Btn
	--------------------- */
  //   $(".product__color__select label, .shop__sidebar__size label, .product__details__option__size label").on('click', function () {
  //       $(".product__color__select label, .shop__sidebar__size label, .product__details__option__size label").removeClass('active');
  //       $(this).addClass('active');
  //   });

  //   $("#coloring label").on('click', function () {
  //     $("#coloring label").removeClass('active');
  //     $(this).addClass('active');
  // });
})(jQuery);


 /*--------------
 open_filters_page
 ---------------*/
 function filters_page() {
     const target = document.getElementById('filters_page');
     if(target.classList.contains('show')) {
         target.classList.remove('show');
     } else {
        target.classList.add('show');
     }
 }

$(document).ready(function () {
  $(window).scroll(function () {
    if ($(window).scrollTop() > 600) {
      $(".scroll_up").css({ opacity: "1", "pointer-events": "visible" });
    } else {
      $(".scroll_up").css({ opacity: "0", "pointer-events": "none" });
    }
  });
});


// alert
function am_warning(status, text, link) {
  var elem = document.getElementsByClassName('am_alert_warning')[0].parentElement;
  if (text != undefined) {
      var h6 = elem.getElementsByTagName('h6')[0].innerHTML = text;
  }
  if (link != undefined) {
      var a = elem.getElementsByTagName('a')[0].href = link;
  }
  if (status == 1) {
      elem.classList.add('show_up');
  } else {
      elem.classList.remove('show_up');
  }
}

// progress
function Progress(display=true) {
  if (display) {
    document.getElementById('progressbar').style.display = 'flex';
  } else {
    document.getElementById('progressbar').style.display = '';
  }
}



function Addhtml() {
  var area_place = document.getElementById('area_content');
  var newhtml = '<div class="item" id="address_card['+ area_place.children.length +']"><span class="d-block font-weight-bold text-center text-primary2 title mt-3">ثبت آدرس جدید:</span><span class="d-block text-secondary w-100 text-right mt-2" style="line-height:26px;font-size:14px">آدرس: </span><input class="form-control" type="text" value="" name="newaddress['+ area_place.children.length +']"><span class="d-block text-secondary w-100 text-right mt-2" style="line-height:26px;font-size:14px">کد پستی: </span><input class="form-control " type="text" value="" name="newcode['+ area_place.children.length +']"><div class="w-100 text-left"><span onclick="removeelement(this)" class="btn-danger rounded my-3">لغو</span></div></div>'
  area_place.insertAdjacentHTML('afterbegin', newhtml);
}
function removeelement(obj) {
  var item = obj.parentElement.parentElement;
  //var item = document.getElementById(id);
  item.remove();
}


function like(id,obj) {
  let data = {id:id}
  $.ajax({
    type: "get",
    url: "/accounts/like/"+ id +"",
    data:data,
    success: function (response) {
      if (response != "false") {
        if(response == 'like'){
          obj.children[0].src = '/static/base/img/heart-fill.svg';
        } else {
          obj.children[0].src = '/static/base/img/heart-pink.svg';
        }
    } else {
      am_warning(1,'ابتدا در سایت ثبت نام کنید','/accounts/login/')
    }
    },
    error: function(){
      
  },
  });
}


function set_recommend(obj,agree,id) {
  $.ajax({
    type: "get",
    url: "/accounts/recommend/"+ id +"",
    data:{agree:agree},
    success: function (response) {
      if (response == 'true' || response == 'false') {
        obj.children[1].innerHTML = Number(obj.children[1].innerHTML) + 1;
      } else {
      }
    },
    error: function(response){
  },
  });
}




// price range filter
/* price filter */
function getVals() {
  // Get slider values
  var parent = this.parentNode;
  var slides = parent.getElementsByTagName("input");
  var slide1 = parseFloat(slides[0].value);
  var slide2 = parseFloat(slides[1].value);
  // Neither slider will clip the other, so make sure we determine which is larger
  if (slide1 > slide2) {
    var tmp = slide2;
    slide2 = slide1;
    slide1 = tmp;
  }

  var displayElement = parent.getElementsByClassName("rangeValues")[0];
  displayElement.innerHTML = "بین " + ToRial(slide2) + " تا " + ToRial(slide1);
}

window.onload = function () {
  // Initialize Sliders
  var sliderSections = document.getElementsByClassName("range-slider");
  for (var x = 0; x < sliderSections.length; x++) {
    var sliders = sliderSections[x].getElementsByTagName("input");
    for (var y = 0; y < sliders.length; y++) {
      if (sliders[y].type === "range") {
        sliders[y].oninput = getVals;
        // Manually trigger event first time to display values
        sliders[y].oninput();
      }
    }
  }
};


function Copy() {
  var dummy = document.createElement('input'),
    text = window.location.href;
  document.body.appendChild(dummy);
  dummy.value = text;
  dummy.select();
  document.execCommand('copy');
  document.body.removeChild(dummy);
  alert('آدرس این محصول برای اشتراک کپی شد')
}
