text: $(document).ready(function()
  {
  	showData();
  	cartNoti();
  	showPrice();
  	$('.addToCart').on('click',function()
  	{
  		var id=$(this).data('id');
  		var name=$(this).data('name');
  		var price=$(this).data('price');
  		var discount=$(this).data('discount');
  		var item={
  			id:id,
  			name:name,
  			price:price,
  			discount:discount,
  			qty:1,
  		}
  		
  		var itemlist=localStorage.getItem('items');
  		var itemArray;
  		if (itemlist==null) {
  			itemArray=[];
  		}
  		else
  		{
  			itemArray=JSON.parse(itemlist);
  		}
  		var status=false;
  		itemArray.forEach(function(v,i){
  		if (id==v.id) 
  		{
  			v.qty++;
  			status=true;
  		}
  		})
  		if (status==false) {
  			itemArray.push(item);
  		}
  		
  		itemString=JSON.stringify(itemArray);
  		localStorage.setItem('items', itemString);
  		showData();
  		cartNoti();
  		showPrice();
  	});
  	function showData()
  	{
  		var itemlist=localStorage.getItem('items');
  		var html='';
  		var i=1;
  		if (itemlist) {
  			var itemArray=JSON.parse(itemlist);
  			var total=0
  			itemArray.forEach(function(v,i){
  			var subtotal=0;
  			
  			if (v.discount) {
  				subtotal+=v.qty*v.discount;
  			}
  			else
  			{
  				subtotal+=v.qty*v.price;
  			}
  
  			total+=subtotal;
  
  				html+=`
  				<tr>
  					<td>#</td>
  					<td>
  						<p>${v.name}</p>
  					</td>
  					<td>
  						<p>${v.discount}</p>
  					</td>
  					<td>
  						<button class="btn btn-success plus_btn mr-2" data-id="${i}"> +
  						</button>
  							${v.qty}
  						<button class="btn btn-danger minus_btn ml-2" data-id="${i}"> -
  					</button>
  					</td>
  					<td>${subtotal}</td>
  				</tr>
  			`
  		});
  			$("#Shopping").html(html);
  			var ht=0;
  			ht+=`<tr>
  			<td colspan='4'>Total</td>
  			<td>${total}KS</td>
      			</tr>`
      		$("#total").html(ht);
  		}
  		else
  		{
  			html+=`<h3>There is nothing shopping</h3>`
  
  			$("#noCart").html(html);
  		}
  
  	}
  	function cartNoti()
  	{
  		var id=$(this).data('id');
  		var itemlist=localStorage.getItem('items');
  	
  		if (itemlist) {
  			var total=0;
  			var itemArray=JSON.parse(itemlist);
  			itemArray.forEach(function(v,i){
  				if (id=v.id) {
  					total+=v.qty++;
  				}
  
  				$("#cartNoti").html(total);
  
  			})
  		}
  	}
  	function showPrice(){
  		var id=$(this).data('id');
  		var itemlist=localStorage.getItem('items');
  		if (itemlist) 
  		{
  			var itemArray=JSON.parse(itemlist);
  			var subtotal=0;
  			var total=0;
  			itemArray.forEach(function(v,i){
  			var subtotal=0;
  			
  			if (v.discount) {
  				subtotal+=v.qty*v.discount;
  			}
  			else
  			{
  				subtotal+=v.qty*v.price;
  			}
  
  			total+=subtotal;
  			$("#showPrice").html(total);
  			});
  		}
  	}
  	$("#Shopping").on('click','.plus_btn',function(){
  		var id=$(this).data('id');
  		var itemlist=localStorage.getItem('items');
  		if (itemlist) {
  			var itemArray=JSON.parse(itemlist);
  			itemArray.forEach(function(v,i){
  				if (id==i) {
  					v.qty++;
  				}
  			});
  		itemString=JSON.stringify(itemArray);
  		localStorage.setItem('items', itemString);
  		showData();
  		cartNoti();
  		showPrice();
  
  		}
  	});
  
  	$("#Shopping").on('click','.minus_btn',function(){
  		var id=$(this).data('id');
  		var itemlist=localStorage.getItem('items');
  		if (itemlist) {
  			var itemArray=JSON.parse(itemlist);
  			itemArray.forEach(function(v,i){
  				if (id==i) {
  					v.qty--;
  					if (v.qty==0) {
  						itemArray.splice(id,1);
  					}
  				}
  			});
  		itemString=JSON.stringify(itemArray);
  		localStorage.setItem('items', itemString);
  		showData();
  		cartNoti();
  		showPrice();
  		}
  	})
  	$('.checkoutBtn').on('click',function(){
  		//alert('Hello');
  		$.ajaxSetup({
  		    headers: {
  		        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  		    }
  		});
  		let ls=localStorage.getItem('items');
  		let note=$("#note").val();
  		
  		let lsAarray=JSON.parse(ls);
  		let total=lsAarray.reduce((acc, row) => acc + (row.qty*row.discount), 0);
  		console.log(total);
  		$.post("orders",{ls:ls,note:note,total:total},function(response){
  			 localStorage.clear();
  			$("#staticBackdrop").modal('show');
  		})
  	});
  })