$(function () {
$.ajaxSetup({
headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
});
if (localStorage.getItem("dismiss-message") == 1) $('.alert-dismissible').remove();
else $('.alert-dismissible').show();
$('#btn-close-message').click(function () { localStorage.setItem("dismiss-message", 1); });
if (localStorage.getItem("products") != null) objProducts = JSON.parse(localStorage.getItem("products"));
updateProductsInLocalStorage();
if (hasCartCode()) {
let productLink = getCartCode();
if (objProducts.length > 0) $('#modal-confirm-product-replacement').modal('show');
else $('#modal-confirm-addition-of-products').modal('show');
$('.btn-confirm-product-link').click(function () {
objProducts = JSON.parse(productLink);
updateProductsInLocalStorage();
window.location.replace($('meta[name="checkout"]').attr('content'));
});
}
$(document).on('click', '.btn-remove-product', function () {
let productId = $(this).attr('data-id');
let objProductInfo = objProducts.find(obj => obj.id == productId);
let index = objProducts.indexOf(objProductInfo);
if (index != -1) objProducts.splice(index, 1);
localStorage.setItem("products", JSON.stringify(objProducts));
$(this).parent().parent().remove();
$('#count-products-in-bag').text(getQuantityOfProducts());
if (objProducts.length > 0) {
$('.cart-link').show();
$(".btn-share").show();
$('#count-products-in-bag').show();
$('#list-products-in-bag').parent().parent().parent().parent().parent().find('.modal-footer').show();
$('#total-price').parent().parent().parent().show();
$('#no-products').hide();
$('#modal-buttons').show();
} else {
$('.cart-link').hide();
$(".btn-share").hide();
$('#count-products-in-bag').hide();
$('#list-products-in-bag').parent().parent().parent().parent().parent().find('.modal-footer').hide();
$('#total-price').parent().parent().parent().hide();
$('#no-products').show();
$('#modal-buttons').hide();
}
getInfoFromSelectedProduct().done(function (response, status) {
totalPrice = 0;
if (response.contents != undefined) response.contents.forEach(function (product) {
let objProduct = getProductInfo(product);
totalPrice += objProduct.preis * objProduct.quantity;
});
$('#total-price').text(numberToEuroFormat(totalPrice));
if (typeof updateProducts === "function") $('#total-checkout-price').text(numberToEuroFormat(totalPrice));
});
productId = $('meta[name="product-id"]').attr('content');
if (productId != undefined) {
let objProduct = objProducts.find(obj => obj.id == productId);
if (objProduct == undefined) {
$('#btn-add-to-bag').show();
$('#btn-show-bag').hide();
if (objProducts.length > 0) $('#btn-check-out').parent().hide();
else $('#btn-check-out').parent().show();
}
else {
$('#btn-add-to-bag').hide();
$('#btn-show-bag').show();
$('#btn-check-out').parent().hide();
$('#count-items-in-bag').html(objProduct.quantity + ``);
}
}
});
$(document).on('input', '.select-quantity', function () {
let quantity = $(this).val();
let productId = $(this).attr('data-id');
let objProductInfo = objProducts.find(obj => obj.id == productId);
let index = objProducts.indexOf(objProductInfo);
objProducts[index].quantity = quantity;
localStorage.setItem("products", JSON.stringify(objProducts));
let productPrice = $(this).parent().parent().find('.product-price');
$.get($('meta[name="get-contents"]').attr('content') + "/" + objProductInfo.id).done(function (response, status) {
let product = response.contents[0];
let objProduct = getProductInfo(product);
objProduct = calculatePrice(objProduct);
let elementPrice = `
${numberToEuroFormat(objProduct.calc_preis)}`;
if (objProduct.calc_preis != objProduct.preis) elementPrice += `
${(((objProduct.calc_preis - objProduct.preis) * 100) / objProduct.preis).toFixed(2)} %
`;
productPrice.html(elementPrice);
});
$('#count-products-in-bag').text(getQuantityOfProducts());
getInfoFromSelectedProduct().done(function (response, status) {
totalPrice = 0;
response.contents.forEach(function (product) {
let objProduct = getProductInfo(product);
calculatePrice(objProduct);
totalPrice += objProduct.calc_preis * objProduct.quantity;
});
$('#total-price').text(numberToEuroFormat(totalPrice));
});
productId = $('meta[name="product-id"]').attr('content');
if (productId != undefined) {
let objProduct = objProducts.find(obj => obj.id == productId);
if (objProduct == undefined) {
$('#btn-add-to-bag').show();
$('#btn-show-bag').hide();
if (objProducts.length > 0) $('#btn-check-out').parent().hide();
else $('#btn-check-out').parent().show();
}
else {
$('#btn-add-to-bag').hide();
$('#btn-show-bag').show();
$('#btn-check-out').parent().hide();
$('#count-items-in-bag').html(objProduct.quantity + ``);
}
}
});
$('#btn-copy-cart-link').click(function () {
$('.cart-link').select();
document.execCommand("copy");
$(this).html(``);
});
$(".btn-share").click(async function () {
if (navigator.share) { try { await navigator.share(shareData); } catch (err) { console.warn(err); } }
else { console.warn('Native Web Sharing not supported'); }
});
});