var cart = [];
var cartForForm = [];
var products = [];
var productsForForm = [];
var new_product_total_price = 0.0;
var total_quantity = 0;
var cardAmount = 0;

const addToCard = (id, price, quantity, name, image, vendor) => {
    var exists = false;
    cart.map((item) => {
       if (item.id === id){
           item.price = price;
           item.quantity += quantity;
           item.name = name;
           item.image = image;
           item.total = parseFloat(price * quantity);
           item.vendor = vendor;
           exists = true;
       }
    });

    cartForForm.map((item) => {
        if (item.item === id){
            item.qty += quantity;
            item.price = price;
            item.vendor = vendor;
            exists = true;
        }
     });
     
     
     
     
    if (!exists){
        cart.push({
            id: id,
            price: price,
            quantity: quantity,
            name: name,
            image: image,
            total: parseFloat(price * quantity),
            vendor: vendor
        });

        cartForForm.push({
            item: id,
            qty: quantity,
            price: price,
            vendor: vendor
        });
    }
    localStorage.setItem("cart", JSON.stringify(cart));
    localStorage.setItem("cartForForm", JSON.stringify(cartForForm));
    console.log(localStorage.getItem("cartForForm"));
}


window.onload = () => {
    cart = JSON.parse(localStorage.getItem("cart")) ?? [];
    cartForForm = JSON.parse(localStorage.getItem("cartForForm")) ?? [];
    var totalQuantity = 0;
    var totalPrice = 0;
    cart.map((item) => {
         totalQuantity += parseInt(item.quantity);
         totalPrice += parseInt(item.price * item.quantity);
    });
    $("#cardQuantity").html(totalQuantity);
    $("#cardAmount").html(totalPrice.toFixed(2));

};


$(".addToCard").on('click', function (){
    let id = parseInt($(this).data('id'));
    let price = parseFloat($(this).data('price'));
    let quantity = parseInt($("#quantity"+id).val());
    let name = $(this).data('name')
    let image = $(this).data('image')
    let vendor = $(this).data('vendor')
    $("#cardAmount").html((parseFloat($("#cardAmount").html()) + (price * quantity)).toFixed(2));
    $("#cardQuantity").html(parseInt($("#cardQuantity").html()) + quantity);
    addToCard(id, price, quantity, name, image, vendor);
    toastrCustomSuccess("Sebede goşuldy");
});

$(".share_with_friend").on('click', function (){
    let link = $(this).data('link');
    var tempTextarea = $('<textarea>');
    $('body').append(tempTextarea);
    tempTextarea.val(link).select();
    document.execCommand('copy');
    tempTextarea.remove();
    toastrCustomSuccess("Haryt salgylanmasy kopýalandy");
});




function updateProducts(){
    const cartBody = $("#cartBody");
    cartBody.html('');
    new_product_total_price = 0;
    total_quantity = 0;
    cardAmount = 0;
    products.map((item) => {
        new_product_total_price = parseFloat(new_product_total_price) + parseFloat(item.total);
        total_quantity = total_quantity + parseInt(item.quantity);
        cartBody.append(`
                    <tr>
                    <td>
                        <img src="`+item.image+`" alt="`+item.name+`">
                    </td>
                    <td>`+item.name+`</td>
                    <td>`+item.price+`</td>
                    <td>
                        <div class="quantity input-counter">
                            <span class="minus-btn mr-2" data-id="`+item.id+`"><i class='icofont-minus'></i></span>
                            <input type="text" value="`+item.quantity+`">
                            <span class="plus-btn ml-2" data-id="`+item.id+`"><i class='icofont-plus'></i></span>
                        </div>
                    </td>
                    <td>`+item.price * item.quantity+`</td>
                </tr>
                `);
    });
    $("#cardQuantity").html(total_quantity);
    $("#cardAmount").html(new_product_total_price.toFixed(2));
    $("#card_amount").val(new_product_total_price.toFixed(2));
    $("#items").val(JSON.stringify(productsForForm));
    localStorage.setItem("cart", JSON.stringify(products));
    localStorage.setItem("cartForForm", JSON.stringify(productsForForm));
}