//-------------------------------
// Increase the cart contents
function increasecart(id){
    // Get the cart contents
    const cart = new Cart( "withinwebcartkey04" );       // ??? can this go somewhere else ???

    // Increment by 1
    cart.increase(id);
    console.log("increase by 1");
}
//-------------------------------
function decreasecart(id){
    // Get the cart contents
    const cart = new Cart( "withinwebcartkey04" );       // ??? can this go somewhere else ???

    // Decrement by 1
    cart.decrease(id);
    console.log("decrease by 1");
}
//-------------------------------
// Add this id to the cart.
function addtocart(id) {    
    // Get the cart contents
    const cart = new Cart( "withinwebcartkey04" );       // ??? can this go somewhere else ???

    // Add the item
    cart.add(id);
    updatebaskettotal();
}
//-------------------------------
// Calculate the total price and add to total display
function updatebaskettotal() {

    // Read local storage using the given key
    const datacart = JSON.parse( localStorage.getItem("withinwebcartkey04") );       // ????? could this be put elsewhere

    var total = 0;
    var numberinbasket = 0;

    // Loop around the data array to calculate the total basket
    for (let i = 0; i < datacart.length; i++) {
        let item = datacart[i];
            numberinbasket++;
            total = Number(total) + Number(item.total);
    }

    $("#totalbasket").text(gblSetUp[0]["currency"] + total.toFixed(2));
    $("#numberinbasket").text(numberinbasket);

}
//-------------------------------
// From local storage update the cart display
function showCart() {

    // Read local storage using the given key
    const datacart = JSON.parse( localStorage.getItem("withinwebcartkey04") );       // ????? could this be put elsewhere

    //add in currency and paypalemail to cart array.

    // Merge the two arrays
    for(let i = 0, len = datacart.length; i < len; i++) {
        datacart[i]["currency"] = gblSetUp[0]["currency"];
        datacart[i]["paypalemail"] = gblSetUp[0]["paypalemail"];
    }

    // Loop around the data array to display the cart details
    for (let i = 0; i < datacart.length; i++) {
        let item = datacart[i];
          //console.log('item_name: ' + item.item_number);
          //console.log('item_number: ' + item.qty);
          //console.log('item: ' + item.color);
          //console.log('quantity: ' + item.mc_gross);
          //console.log('total: ' + item.total);

          $('.cartresult').append (

            `
            <div class="row">

                <div class="col-md-3">
                    ${item.item_number}
                </div>

                <div class="col-md-2">
                    ${item.qty}
                </div>
                
                <div class="col-md-2">
                    ${item.currency}${item.mc_gross}
                </div>

                <div class="col-md-2">    
                    ${item.currency}${item.total}
                </div>

                <div class="col-md-3">
                   <img id='${item.id}' name='plus' src='assets/plus.jpg' class='cssplus' />
                   &nbsp;&nbsp;
                   <img id='${item.id}' name='minus' src='assets/minus.jpg' class='cssminus' />
                </div>

            </row>
            `
        );
    }
    
    updatebaskettotal();

    //--------------------------------
    // Plus, increasecart
    $('.cssplus').on('click', function (e) {        
        //console.log(e.target.id);
        //console.log(e.target.name);
        //console.log(e);

        increasecart(e.target.id);
        $('.cartresult').text("");
        showCart();
    });

    //--------------------------------
    // Minus, decrease cart
    $('.cssminus').on('click', function (e) {        
        //console.log(e.target.id);
        //console.log(e.target.name);
        //console.log(e);

        decreasecart(e.target.id);
        $('.cartresult').text("");
        showCart();
    });

}
