//Set up a flow control switch
$(".m-cartItemToCalculate_btn").on("click",function(){
var that=$(this);
var numChangeId = that.attr('id');
var valItem=that.parent(".m-cartItemToCalculate").find("input[name='quantity']");
if(numChangeId == 'num-reduce' && valItem.val()<=1){
switchCalculate(that);
}else{
ajaxUpdate(that);
}
});
function throttle1(func, wait) {
let timeout;
return function(){
let context = this;
let args = arguments;
if (!timeout) {
timeout = setTimeout(() => {
timeout = null;
func.apply(context, args)
}, wait)
}
}
};
function switchCalculate(proItem){
var numChangeId = proItem.attr('id');
var valItem=proItem.parent(".m-cartItemToCalculate").find("input[name='quantity']");
console.log(valItem.val()+"valItem");
if(numChangeId == 'num-reduce'){
$("#num-increase").removeAttr("disabled");
if( valItem.val() <= 1 ){
$(".o-maskLayer-cartList").css("display","block");
$(".o-maskLayer-cartList").find($(".a-radioOn")).attr("checked","checked");
$(".o-maskLayer-cartList").find($(".a-radioOff")).removeAttr("checked");
var removeId = proItem.attr("removeflag");
$(".a-remove-btn").attr("removeId", removeId);
valItem.value = 1;
return false;
}else{
valItem.val(parseInt(valItem.val()) - 1);
}
}else if(numChangeId == 'num-increase'){
proItem.parents(".flexBox").next(".Msgitemlimit-phone").hide();
proItem.removeAttr("disabled");
valItem.val(parseInt(valItem.val()) + 1);
}
}
function ajaxUpdate(obj) {
switchCalculate(obj);
throttle1(setAjax, 1000)(obj);
}
function setAjax(obj) {
var guid=obj.parents(".o-cartItemDetailsWrap").find(".a-guidval").val();
var productCode=obj.parents(".o-cartItemDetailsWrap").find(".a-productCode").html();
var newquantity = obj.parent(".m-cartItemToCalculate").find("input[name='quantity']").val();
var oldQty=obj.parent(".m-cartItemToCalculate").find("input[name='quantity']").attr("old");
var param = [{
"product": {
"code": productCode
},
"quantity": newquantity
}];
$.ajax({
type:"post",
url:"updateEntry/"+guid,
data:JSON.stringify(param),
datatype:"json",
contentType: "application/json; charset=utf-8",
success:function(res){
if(res.success){
for(var i=0;i<res.data.entries.length;i++){
if(res.data.entries[i].guid==guid){
var resWebprice = res.data.entries[i].price.totalwebPriceFmt;
var resSaleprice = res.data.entries[i].price.totalPriceWithoutCouponFmt;
obj.parents(".o-cartItemDetailsWrap").find("input[name='quantity']").attr("old",newquantity);
//obj.parents(".o-cartItemDetailsWrap").find("input[name='quantity']").val(newquantity);
obj.parents(".o-cartItemDetailsWrap").find(".m-cartItemPrice_webprice .a-webprice").html(resWebprice);
obj.parents(".o-cartItemDetailsWrap").find(".m-cartItemPrice_saleprice .a-saleprice").html(resSaleprice);}
}
}else{
for(var i=0;i<res.error.length;i++){
console.log(res.error[i].message+"error msg!");
globalErrorMsgForCMS("",res.error[i].message);
}
obj.parents(".o-cartItemDetailsWrap").find("input[name='quantity']").val(oldQty);
}
},
error:function(){
console.log("Update failed!");
obj.parents(".o-cartItemDetailsWrap").find("input[name='quantity']").val(oldQty);
}
})
};
function globalErrorMsgForCMS(errKey, errMsg) {
if(errKey){
$(".errorMsg").css("display","block").html(errorMsgJson[errKey]);
} else {
$(".errorMsg").css("display","block").html(errMsg);
}
}