/**
 * 
 */


function add_to_cart(pphoto, pid, pname, pprice){
	
	let cart=localStorage.getItem("cart");
	if(cart==null){
		
		let products=[];
		let product={productphoto:pphoto, productId:pid,productName:pname,productPrice:pprice,productQuantity:1}
		products.push(product);
		localStorage.setItem("cart" ,JSON.stringify(products));
		myFunction("Item is added to Cart")
		
	}else{
		let pcart =JSON.parse(cart);
		
		let oldProduct = pcart.find((item)=> item.productId == pid)
		if(oldProduct){
			oldProduct.productQuantity= oldProduct.productQuantity+1
			pcart.map((item)=>{
				
				if(item.productId== oldProduct.productId){
					
					item.productQuantity= oldProduct.productQuantity;
					
					
				}
				
				
				
			})
			
			localStorage.setItem("cart" ,JSON.stringify(pcart));
			myFunction(oldProduct.productName+"quantity increased")
		}else{
			let product={productphoto:pphoto, productId:pid,productName:pname,productPrice:pprice,productQuantity:1}
			pcart.push(product);
			localStorage.setItem("cart" ,JSON.stringify(pcart));
			myFunction("Item is added to Cart")
			
		}
	
	}
	updateCart();
         
}


function updateCart(){
		
		let cartString= localStorage.getItem("cart");
		let cart= JSON.parse(cartString);
		if(cart==null || cart.length==0){
			console.log("Cart is empty")
			
			$(".cart-items").html("( 0 )");
			$(".cart-body").html("<h3> Cart does not have any item </h3>");
			$(".checkout-btn").attr('disabled',true);
			
			
		}else{
			
			console.log(cart)
			$(".cart-items").html(`(${cart.length})`);
			
			let table= `
			
			<table class='table'>
			<thead  class='thead-light'>
			<tr>
			<th>Photo</th>
			<th>Name</th>
			<th>Price</th>
			<th>Quantity</th>
			<th>Item total Price</th>
			<th>Action</th>
			
			
			
			
			
			
			</tr>
			</thead>
			
			
			`;
			let totalPrice=0;
			cart.map((item)=>{
				
				table+= `
				<tr>
				<td>
				<img src="images/products/${item.productphoto}" style="max-height: 100px; max-width: 100%; width: auto;">
				
				</td>
				
				<td> ${item.productName}</td>
				<td> ${item.productPrice}</td>
				<td> ${item.productQuantity}</td>
				<td> ${item.productQuantity*item.productPrice}</td>
				  
                       
				<td><button onclick= 'deleteItemFromCart(${item.productId})'class="btn btn-danger btn-sm">Remove</button></td>
				
				</tr>
				
				
				`
				totalPrice += item.productPrice * item.productQuantity;
			})
			
			table= table+`
			<tr><td colspan='5' class="text-right"> Total Price :  ${totalPrice} </td></tr>
			
			 </table>`
			
			$(".cart-body").html(table);
			$(".checkout-btn").attr('disabled',false);
		}
	}
	
	function deleteItemFromCart(pid){
		
		let cart= JSON.parse(localStorage.getItem('cart'));
		
		let newCart= cart.filter((item)=> item.productId!= pid)
		
		localStorage.setItem('cart' ,JSON.stringify(newCart))
		
		updateCart();
		
		
		
	}
	
	$(document).ready(function(){
		
		updateCart()
	})
	
	
	
  function myFunction() {
  
  var x = document.getElementById("snackbar");

  x.className = "show";
  setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
}
 
function goToCheckout(){
	
	window.location="checkout.jsp"
	
}