document.addEventListener("DOMContentLoaded", function(event) {
simpleCart({
cartColumns: [
{ attr: "code", label: "Cod"}
,{ attr: "quantity", label: "Cant"}
,{ attr: "name", label: "Descripcion"}
,{ view: "currency", attr: "price", label: "P.U"}
,{ view: "currency", attr: "total", label: "SubTotal" }
,{ view: function(item, column){
return '';
} , text: "increment", label: false}
,{ view: function(item, column){
return '';
}, text: "decrement", label: false}
,{ view: function(item, column){
return '';
} , text: "X", label: false}
],
cartStyle: "table",
data: {},
language: "english-us",
excludeFromCheckout: [],
shippingCustom: null,
shippingFlatRate: 0,
shippingQuantityRate: 0,
shippingTotalRate: 0,
taxRate: 0,
taxShipping: false,
});
simpleCart.checkoutStatus = false;
simpleCart.currency({
code: "ARG" ,
name: "ARGENTINA" ,
symbol: "$ " ,
delimiter: "" ,
decimal: "," ,
after: false ,
accuracy: 2
});
setInterval(function(){
if(typeof simpleCart !='undefined'){
$('.simpleCart_quantity').html(simpleCart.quantity());
}
},3000);
simpleCart.bind( 'beforeRemove' , function( item ){
if(simpleCart.checkoutStatus==false){
setTimeout(function(){
if(simpleCart.items().length==0){
$("#carro_compras").modal("hide");
}
$("a[href='#carro_compras']:visible").trigger("message",[(simpleCart.items().length==0)?"Pedido Cancelado":"Producto Removido","danger"]);
},350);
}
});
simpleCart.bind( 'beforeAdd' , function( item ){
$("a[href='#carro_compras']:visible").trigger("message",["Producto Agregado","success"]);
});
simpleCart.checkout= function() {
if(this.items().length==0){
alert("Ingrese al menos 1 producto");
return false;
}
var dataSEND = {
"email": $("#emailconfirmacion").val() ,
"items" :[],
"metodoPago":$("[name='metodoPago']").val() ,
"metodoEntrega":$("[name='retiro']").val(),
"metodoCompra":$("[name='metodoCompra']").val(),
};
// valido inputs
$inputs = $("form[name='datos_personales'], form[name='datos_personales2']")
.find("input[required]:visible(), textarea[required], select[required]");
$inputs_vacios= $inputs.filter(function(){
return ($(this).val()=="" || $(this).val().length==0);
});
$inputs.each(function(){
if($(this).val()=="" || $(this).val().length==0){
$(this).css("border-color","red");
$(this).focus();
fnPopMessage(null,"Ingrese todos los datos","danger",$(this))
return false
}
$(this).css("border-color","gray");
dataSEND [$(this).attr("name")] = $(this).val();
});//fin validacion de inputs
if($inputs_vacios.length>0){ console.log(3);
return false;
}
//agrego productos
$.each(simpleCart.items(),function( item , x ){
dataSEND.items.push({
"code": this.get("code") ,
"name": this.get("name") ,
"quantity": this.quantity() ,
"price": this.get("price"),
"url": this.get("url")
});
});//fin agregado productos
//muestor mensaje
$(".simpleCart_checkout").prop("disabled",true).text("Procesando");
this.checkoutStatus=true;
fnPopMessage(null,"Enviando Pedido","info",$("simpleCart_checkout"));
$.ajax({
type: "POST",
url: PATH+"checkout.php",
method:"POST",
contentType: "application/json",
dataType: "json",
success: function (data) {
if(data.status==true){
$("#carro_compras").modal("hide");
simpleCart.empty();
simpleCart.checkoutStatus=false;
$("a[href='#carro_compras']:visible").trigger("message",["Pedido Enviado","success"]);
//window.location = PATH + "panel/compras";
}
if(typeof data.message !="undefined" || data.status ==false){
alert(data.message||"No se pudo generar el pedido");
}
if(typeof data['loadContent'] !="undefined" && typeof loadContent == "function"){
$("#carro_compras").modal("hide");
loadContent(PATH+data['loadContent']);
}else if(typeof data['redirection'] !="undefined"){
window.location = data['redirection'];
}else if(typeof data['url'] !="undefined"){
window.location = data['url'];
}
$(".simpleCart_checkout").prop("disabled",false).text("Siguiente");
},
data: JSON.stringify(dataSEND)
}).fail(function(){
alert("Servicio no disponible, intente en otro momento.");
$(".simpleCart_checkout").prop("disabled",false).text("Siguiente");
});
};
$("a[href='#carro_compras']").click(function(event){
event.preventDefault();
if(simpleCart.items().length==0){
$(this).trigger("message",["Sin Productos","warning"]);
return false;
}
$modal_id = $(this).attr('href');
$($modal_id).modal('show');
return true;
});
function fnPopMessage(event,messages,color="info",$obj=null){
if($obj == null){
var $obj = $("a[href='#carro_compras']:visible");
}
if($obj.parent().find(".popover-content").length>0){
return false;
}
$obj.on('shown.bs.popover', function() {
$obj.parent().find(".popover-content").addClass("alert-"+color);
setTimeout(function() {
$obj.popover("destroy");
}, 1500);
});
$obj.popover({placement: 'bottom',title:"Notificación",content:messages});
$obj.popover("show");
};
$("a[href='#carro_compras']").on("message",fnPopMessage);
/*
setInterval(function() {
var $obj = $("a[href='#carro_compras']:visible");
if($obj.parent().find(".popover-content").length>0){return false;}
$obj.on('shown.bs.popover', function() {
$obj.parent().find(".popover-content").addClass("alert-info");
setTimeout(function() {
$obj.popover("destroy");
}, 1500);
});
$obj.popover({placement: 'bottom',title:"Aviso",content:"Tiene una compra sin finalizar."})
$obj.popover("show");
}, 5500);*/
$("button#agregar").click(function(){
var btn = $(this);
var html = btn.html();
btn.text("Agregando...");
btn.removeClass("bgnaranja").addClass("bgverde");
btn.attr("disabled",true);
$(".item_add.hidden").trigger("click");
setTimeout(function(){
btn.html(html);
btn.attr("disabled",false);
btn.addClass("bgnaranja").removeClass("bgverde");
},500);
});
});