var list = $('#menu-categories');
var items = $('#menu-items');
var sessionId;
var itemsArray;
var $galleryFilter;
var tooltipAdd;
var tooltipRemove;
var cartPrice;
var minPrice;
var orderStatus;
var orderStatusLoop;
var orderStatusLoopStarted;
var name;
var surname;
var street;
var street_num;
var postcode;
var city;
var msisdn;
var email;
var followToken;
var paymentMethod;
var isOpened;
var couponTooltipTimeout;
var paymentMethods = {
1: 'Dostawa: gotówka',
2: 'Dostawa: karta',
3: 'Płatność online',
4: 'Odbiór osobisty (gotówka/karta)'
};
var statusList = {
1: 'Nowe zamówienie',
2: 'Przyjęte do realizacji',
3: 'Gotowe do transportu',
4: 'Gotowe do odbioru',
5: 'W dostawie',
6: 'Odebrane osobiście',
7: 'Dostarczone',
8: 'Anulowane'
};
var couponErrors = {
1: 'Kupon nie obejmuje tej metody płatności',
2: 'Kupon nie spełnia minimalnej wartości zamówienia',
3: 'W zamówieniu brak produktów objętych rabatem',
4: 'Podany kod jest nieprawidłowy',
5: 'Podany kod został wyczerpany'
};
var statusListForPayment = [];
statusListForPayment[1] = [1, 2, 3, 5, 7, 8];
statusListForPayment[2] = [1, 2, 3, 5, 7, 8];
statusListForPayment[3] = [1, 2, 3, 5, 7, 8];
statusListForPayment[4] = [1, 2, 4, 6, 8];
function initCart() {
$.post('https://panel.aseto.pl/api/menu/ajax.php?token=f8fa8c40414af339a92cebd43831f5df1e0865b5dd2bf0f435b9a19918413459', { action: 'initCart' }).done(function(response, textStatus, jqXHR) {
data = JSON.parse(response);
if(data.status) {
window.sessionId = data.data;
if(!Cookies.get('cartId')) {
Cookies.set('cartId', window.sessionId, { expires: 1 });
} else {
window.sessionId = Cookies.get('cartId');
}
refreshCart();
getMinPrice();
} else {
// to do
}
});
}
function setItemsArray() {
$.post('https://panel.aseto.pl/api/menu/ajax.php?token=f8fa8c40414af339a92cebd43831f5df1e0865b5dd2bf0f435b9a19918413459', { action: 'getMenuItemsDelivery' }).done(function(response, textStatus, jqXHR) {
data = JSON.parse(response);
if(data.status) {
itemsArray = data.data;
initCart();
}
});
}
function getMenuItems() {
$.post('https://panel.aseto.pl/api/menu/ajax.php?token=f8fa8c40414af339a92cebd43831f5df1e0865b5dd2bf0f435b9a19918413459', { action: 'getMenuItemsDelivery' }).done(function(response, textStatus, jqXHR) {
data = JSON.parse(response);
if(data.status) {
$galleryFilter = $('.menu-filter-items').isotope({
itemSelector: '.menu-item',
masonry: {
columnWidth: '.menu-item'
}
});
$.each(data.data, function(key, value) {
var price = '';
var firstprice = '';
//firstprice = value.prices[0].price;
$.each(value.prices, function(k, v) {
if(price.length > 0) {
price = 'od ' + v.price + ' zł';
}
else {
price = v.price + ' zł';
}
});
$galleryFilter.isotope('insert', $('
'));
});
$('.menu-filter-list').on('click', 'li', function() {
var filterValue = $(this).attr('data-filter');
$('.menu-filter-list').find('.is-checked').removeClass('is-checked');
$(this).addClass('is-checked');
$galleryFilter.isotope({filter: filterValue});
});
$galleryFilter.isotope('layout');
}
});
}
function getMenuCategories() {
$.post('https://panel.aseto.pl/api/menu/ajax.php?token=f8fa8c40414af339a92cebd43831f5df1e0865b5dd2bf0f435b9a19918413459', { action: 'getMenuCategories' }).done(function(response, textStatus, jqXHR) {
data = JSON.parse(response);
if(data.status) {
$.each(data.data, function(key, value) {
$(list).append('' + value.name + ' ');
});
getMenuItems();
}
});
}
function openProductModal(id) {
$('#modalBody').empty();
$('#title_desc').empty();
$('#title_name').html(itemsArray[id].title);
$('#title_desc').append('' + itemsArray[id].description + '
');
$('#title_desc').append('' + itemsArray[id].smalltxt + '
');
$.each(itemsArray[id].prices, function(key, value) {
$('#modalBody').append('');
});
$('#productModal').modal();
}
function getMinPrice() {
$.post('https://panel.aseto.pl/api/menu/ajax.php?token=f8fa8c40414af339a92cebd43831f5df1e0865b5dd2bf0f435b9a19918413459', { action: 'getMinPrice' }).done(function(response, textStatus, jqXHR) {
data = JSON.parse(response);
if(data.status) {
minPrice = parseFloat(data.data);
} else {
// to do
}
});
}
function addToCart(id) {
$.post('https://panel.aseto.pl/api/menu/ajax.php?token=f8fa8c40414af339a92cebd43831f5df1e0865b5dd2bf0f435b9a19918413459', { action: 'addToCart', id: id, sessionId: sessionId }).done(function(response, textStatus, jqXHR) {
data = JSON.parse(response);
if(data.status) {
$(tooltip).tooltip('show');
setTimeout(function() {
$(tooltip).tooltip('hide');
}, 2500);
refreshCart();
} else {
// to do
}
});
}
function removeFromCart(id) {
$.post('https://panel.aseto.pl/api/menu/ajax.php?token=f8fa8c40414af339a92cebd43831f5df1e0865b5dd2bf0f435b9a19918413459', { action: 'removeFromCart', id: id, sessionId: sessionId }).done(function(response, textStatus, jqXHR) {
data = JSON.parse(response);
if(data.status) {
refreshCart();
} else {
// to do
}
});
}
function refreshCart() {
$.post('https://panel.aseto.pl/api/menu/ajax.php?token=f8fa8c40414af339a92cebd43831f5df1e0865b5dd2bf0f435b9a19918413459', { action: 'getCartContent', sessionId: sessionId }).done(function(response, textStatus, jqXHR) {
data = JSON.parse(response);
$.each($('.cart-list').find('li').slice(0, -1), function(key, value) {
$(this).remove();
});
cartPrice = 0;
if(data.status) {
$('.cart-count').html(Object.keys(data.data).length);
if(Object.keys(data.data).length > 0) {
$.each(data.data, function(key, value) {
$('.cart-list > li:last').before(' ' + itemsArray.find(x => x.id === value[1].toString()).prices.find(x => x.id === value[0].toString()).price + ' zł
');
cartPrice = cartPrice + parseFloat(itemsArray.find(x => x.id === value[1].toString()).prices.find(x => x.id === value[0].toString()).price);
});
}
$('#cart-order-header').removeClass('disabled');
} else {
$('.cart-list > li:last').before('');
$('.cart-count').html(0);
$('#cart-order-header').addClass('disabled');
}
$('.cart-price').html(cartPrice.toFixed(2));
if($('#cart-order-items > tbody').length) {
$('#cart-order-items > tbody').empty();
if(data.status) {
var i = 1;
$.each(data.data, function(key, value) {
$('#cart-order-items > tbody').append('' + i++ + ' | ' + itemsArray.find(x => x.id === value[1].toString()).title + ' | ' + itemsArray.find(x => x.id === value[1].toString()).prices.find(x => x.id === value[0].toString()).name + ' (' + itemsArray.find(x => x.id === value[1].toString()).prices.find(x => x.id === value[0].toString()).descr + ')' + itemsArray.find(x => x.id === value[1].toString()).prices.find(x => x.id === value[0].toString()).price + ' zł | |
');
});
$('#cart-order-finalize').removeClass('disabled');
} else {
$('#cart-order-items > tbody').append('Brak produktów w koszyku! |
');
$('#cart-order-finalize').addClass('disabled');
}
}
if($('#cart-order-summary').length) {
$('#cart-order-summary > tbody').empty();
if(data.status) {
$.each(data.data, function(key, value) {
$('#cart-order-summary > tbody').append('' + itemsArray.find(x => x.id === value[1].toString()).title + ' (' + itemsArray.find(x => x.id === value[1].toString()).prices.find(x => x.id === value[0].toString()).name + ' (' + itemsArray.find(x => x.id === value[1].toString()).prices.find(x => x.id === value[0].toString()).descr + ')) | ' + itemsArray.find(x => x.id === value[1].toString()).prices.find(x => x.id === value[0].toString()).price + ' zł |
');
});
$('#cart-order-summary > tbody').append('Suma | ' + cartPrice.toFixed(2) + ' zł |
');
$('#cart-order-summary-finalize').removeClass('disabled');
} else {
$('#cart-order-summary > tbody').append('Brak produktów w koszyku! |
');
$('#cart-order-summary-finalize').addClass('disabled');
}
}
if($('#cart-order-form').length) {
getPaymentMethod();
}
});
}
function finalizeOrder() {
if(!$('#agree').is(':checked')) {
var agreeTooltip = $('#agree').tooltip({
trigger: 'manual',
container: 'body',
title: 'Zaakceptuj warunki zamówienia!'
});
$(agreeTooltip).tooltip('show');
setTimeout(function() {
$(agreeTooltip).tooltip('hide');
}, 2500);
return;
}
var paymentMethod = $('input[name="paymentmethod"]:checked').val();
$.post('https://panel.aseto.pl/api/menu/ajax.php?token=f8fa8c40414af339a92cebd43831f5df1e0865b5dd2bf0f435b9a19918413459', { action: 'setPaymentMethod', method: paymentMethod, sessionId: sessionId }).done(function(response, textStatus, jqXHR) {
data = JSON.parse(response);
if(data.status) {
window.location.href = '/zamowienie.php';
} else {
// to do
}
});
}
function checkIsRestOpened() {
$.post('https://panel.aseto.pl/api/menu/ajax.php?token=f8fa8c40414af339a92cebd43831f5df1e0865b5dd2bf0f435b9a19918413459', { action: 'checkIsOpened' }).done(function(response, textStatus, jqXHR) {
data = JSON.parse(response);
if(data.status) {
isOpened = true;
} else {
isOpened = false;
}
});
}
function getPaymentMethod() {
$.post('https://panel.aseto.pl/api/menu/ajax.php?token=f8fa8c40414af339a92cebd43831f5df1e0865b5dd2bf0f435b9a19918413459', { action: 'getPaymentMethod', sessionId: sessionId }).done(function(response, textStatus, jqXHR) {
data = JSON.parse(response);
if(data.status) {
if(data.data > 0 && data.data < 5) {
paymentMethod = parseInt(data.data);
switch(parseInt(data.data)) {
case 1:
$('#email').parent().remove();
break;
case 2:
$('#email').parent().remove();
break;
case 3:
break;
case 4:
$('#email').parent().remove();
break;
}
} else {
// to do
}
} else {
$('#cart-order-form').remove();
}
});
}
function checkCouponCode(code) {
$.post('https://panel.aseto.pl/api/menu/ajax.php?token=f8fa8c40414af339a92cebd43831f5df1e0865b5dd2bf0f435b9a19918413459', { action: 'checkCouponCode', coupon: code, sessionId: sessionId }).done(function(response, textStatus, jqXHR) {
data = JSON.parse(response);
if(data.status) {
$('#coupon').prop('disabled', true);
$('#check-coupon').prop('disabled', true);
var summaryTooltip = $('.cart-price-summary').tooltip({
trigger: 'manual',
placement: 'bottom',
container: 'body',
title: 'Cena została zaktualizowana o kupon rabatowy'
});
$(summaryTooltip).tooltip('show');
setTimeout(function() {
$(summaryTooltip).tooltip('hide');
}, 5000);
$('#cart-order-summary > tbody > tr:last').before(`Kod rabatowy: ${data.data.name} | - ${data.data.diff.toFixed(2)} zł |
`);
let sum = parseFloat($('.cart-price-summary').html());
sum = sum - data.data.diff;
$('.cart-price-summary').html(sum.toFixed(2));
} else {
var couponTooltip = $('#coupon').tooltip({
trigger: 'manual',
placement: 'bottom',
container: 'body'
});
clearTimeout(couponTooltipTimeout);
$(couponTooltip).tooltip('hide');
$(couponTooltip).attr('data-original-title', couponErrors[parseInt(data.data)]);
$(couponTooltip).tooltip('show');
couponTooltipTimeout = setTimeout(function() {
$(couponTooltip).tooltip('hide');
}, 5000);
}
});
}
function getDeliveryPrice(address) {
$('#loadingModal').modal();
$.post('https://panel.aseto.pl/api/menu/ajax.php?token=f8fa8c40414af339a92cebd43831f5df1e0865b5dd2bf0f435b9a19918413459', { action: 'getDeliveryPrice', address: address, sessionId: sessionId }).done(function(response, textStatus, jqXHR) {
data = JSON.parse(response);
if(data.status) {
$('[data-section="coupon-code-block"]').show();
cartPrice = cartPrice + parseFloat(data.data);
$('#cart-order-summary > tbody > tr:last').before('dostawa na podstawie podanego adresu | + ' + data.data + ' zł |
');
$('.cart-price-summary').html(cartPrice.toFixed(2));
var summaryTooltip = $('.cart-price-summary').tooltip({
trigger: 'manual',
placement: 'bottom',
container: 'body',
title: 'Cena została zaktualizowana o koszt dostawy'
});
$(summaryTooltip).tooltip('show');
setTimeout(function() {
$(summaryTooltip).tooltip('hide');
}, 5000);
$(street).removeClass('is-invalid').attr('disabled', true);
$(postcode).removeClass('is-invalid').attr('disabled', true);
$(city).removeClass('is-invalid').attr('disabled', true);
$('#cart-order-summary-finalize').after('');
$('#cart-order-summary-finalize').css({display: 'none'});
} else {
$(street).addClass('is-invalid');
$(postcode).addClass('is-invalid');
$(city).addClass('is-invalid');
var streetTooltip = $(street).tooltip({
trigger: 'manual',
placement: 'bottom',
container: 'body',
title: 'Dostawa nie jest możliwa pod podany adres. Sprawdź dokładnie adres i spróbuj ponownie!'
});
$(streetTooltip).tooltip('show');
setTimeout(function() {
$(streetTooltip).tooltip('hide');
}, 5000);
setTimeout(function() {
$('#cart-order-summary-finalize').attr('disabled', false);
}, 1000);
}
$('#loadingModal').modal('hide');
});
}
function order() {
if($('#cart-order-form').length) {
name = $('#name');
surname = $('#surname');
street = $('#street');
street_num = $('#street_num');
postcode = $('#postcode');
city = $('#city');
msisdn = $('#msisdn');
email = $('#email');
setOrderDetails();
}
}
function changeAddress() {
$(street).attr('disabled', false);
$(postcode).attr('disabled', false);
$(city).attr('disabled', false);
$('#cart-order-summary-change-address').remove();
$('#cart-order-summary-submit').remove();
$('#cart-order-summary-finalize', '#cart-order-form').css({display: 'block'});
$('#cart-order-summary-finalize', '#cart-order-form').attr('disabled', false);
$('[data-section="coupon-code-block"]').hide();
$('#coupon').val('');
$('#coupon').prop('disabled', false);
$('#check-coupon').prop('disabled', false);
$('.cart-price-summary').tooltip('hide');
refreshCart();
}
function submitOrder() {
$.post('https://panel.aseto.pl/api/menu/ajax.php?token=f8fa8c40414af339a92cebd43831f5df1e0865b5dd2bf0f435b9a19918413459', { action: 'addOrder', sessionId: sessionId }).done(function(response, textStatus, jqXHR) {
console.log(response);
data = JSON.parse(response);
$('#order-content').empty();
if(data.status) {
Cookies.set('followToken', data.data.followToken, { expires: 1/12 });
if(data.data.trnUrl) {
$('#order-content').empty().html('Trwa przekierowanie do systemu płatności...');
window.location.href = data.data.trnUrl;
} else {
getOrderStatus();
}
} else {
$('#order-content').empty().html('Wystąpił błąd z Twoim zamówieniem! :( Skontaktuj się z nami telefonicznie lub spróbuj ponownie!');
}
});
}
function getOrderStatus() {
getPaymentMethod();
$.post('https://panel.aseto.pl/api/menu/ajax.php?token=f8fa8c40414af339a92cebd43831f5df1e0865b5dd2bf0f435b9a19918413459', { action: 'getOrderStatus', followToken: Cookies.get('followToken') }).done(function(response, textStatus, jqXHR) {
data = JSON.parse(response);
if(data.status) {
$('#order-content').empty().html('Zamówienie zostało złożone! Poniżej możesz obserwować status swojego zamówienia.
');
if(!orderStatusLoopStarted) {
orderStatusLoop = setInterval(getOrderStatus, 5000);
orderStatusLoopStarted = true;
}
orderStatus = parseInt(data.data.status);
$.each(statusListForPayment[parseInt(paymentMethod)], function(key, value) {
$('#status-' + value).css({ display: 'inline-block' });
if(orderStatus >= value) {
$('#status-' + value).addClass('active');
}
});
if(parseInt(data.data.eta) > 0) {
$('#eta-content').html('Szacowany czas realizacji: ' + data.data.eta + ' minut');
}
$('#order-content-status').css({ display: 'block' });
} else {
$('#order-content').empty().html('Informacja o poprawnym statusie płatności nie dotarła do naszego systemu. Spróbuj odświeżyć stronę.
');
}
});
}
function setOrderDetails() {
$.post('https://panel.aseto.pl/api/menu/ajax.php?token=f8fa8c40414af339a92cebd43831f5df1e0865b5dd2bf0f435b9a19918413459', {
action: 'setOrderDetails',
name: $('#name').val(),
surname: $('#surname').val(),
street: $('#street').val(),
street_num: $('#street_num').val(),
postcode: $('#postcode').val(),
city: $('#city').val(),
msisdn: $('#msisdn').val(),
email: $('#email').val(),
note: $('#note').val(),
sessionId: sessionId
}).done(function(response, textStatus, jqXHR) {
data = JSON.parse(response);
if(data.status) {
// to do
} else {
// to do
}
});
}
$(document).ready(function() {
checkIsRestOpened();
setItemsArray(); // do headera
if($('#menu-categories').length) {
getMenuCategories();
}
$('#cart-order-header').on('click', function(e) {
e.preventDefault();
console.log('hujek');
if(parseFloat(cartPrice) < minPrice) {
var cartPriceTooltip = $('.cart-price').tooltip({
trigger: 'manual',
placement: 'bottom',
container: 'nav',
title: 'Minimalna kwota zamówienia to ' + minPrice.toFixed(2) + ' zł!'
});
$(cartPriceTooltip).tooltip('show');
setTimeout(function() {
$(cartPriceTooltip).tooltip('hide');
}, 2500);
return;
}
if(isOpened) {
window.location.href = 'koszyk.php';
} else {
$('#closedModal').modal();
}
});
if($('#cart-order-form').length) {
$('.cart-nav').remove();
if(Cookies.get('followToken')) {
followToken = Cookies.get('followToken');
setTimeout(getOrderStatus, 500);
}
$('#cart-order-form').on('click', '#cart-order-summary-finalize', function() {
order();
});
$('#cart-order-form').on('click', '#cart-order-summary-submit', function() {
submitOrder();
});
$('#cart-order-form').on('click', '#cart-order-summary-change-address', function() {
changeAddress();
});
$('#cart-order-form').submit(function(e) {
e.preventDefault();
var regex = /^[\S\s]+\d+[A-z]?$/g;
if(!$('#street').val().match(regex)) {
$('#street').addClass('is-invalid');
$('#street').focus();
var streetTooltip = $('#street').tooltip({
trigger: 'manual',
placement: 'bottom',
container: 'body',
title: 'Adres jest niepoprawny, podaj numer domu'
});
$(streetTooltip).tooltip('show');
setTimeout(function() {
$(streetTooltip).tooltip('hide');
}, 5000);
return;
}
$('#cart-order-summary-finalize').attr('disabled', true);
if(paymentMethod !== 4) {
getDeliveryPrice(street.val() + ', ' + postcode.val() + ' ' + city.val());
} else {
submitOrder();
}
});
}
if($('[data-section="coupon-code-block"]').length) {
$('[data-section="coupon-code-block"]').hide();
}
$('#menu-items').on('click', '.open-cart-add', function() {
console.log('onclick nie działa');
openProductModal($(this).attr('data-item-id'));
});
$('#modalBody').on('click', '.price-item', function() {
addToCart($(this).attr('data-price-id'));
});
$('.cart-list').on('click', '.removeItem', function() {
removeFromCart($(this).closest('li').attr('data-cart-item-id'));
});
$('#cart-order-items').on('click', '.item-remove', function() {
removeFromCart($(this).closest('tr').attr('data-cart-item-id'));
});
$('#check-coupon').on('click', function() {
checkCouponCode($(this).closest('.row').find('#coupon').val());
});
$('#cart-order-finalize').on('click', function() {
if(isOpened) {
finalizeOrder();
} else {
$('#closedModal').modal();
}
});
$(document).on('click', '#sticky-wrapper .dropdown-menu', function(e) {
e.stopPropagation();
});
tooltip = $('.cart-nav').tooltip({
trigger: 'manual',
container: 'nav',
title: 'Produkt dodany do koszyka!'
});
});