function loadProducts(id) {
	$.ajax({
		url : "load-product-by-category.php",
		type : "POST",
		data : "id="+id+"&",
		success: function(data){
			$('#productMsg').html(data);
		},
		error: function(jXHR, textStatus, errorThrown){
			alert(jXHR.responseText);
		}
	})
}

function selectProduct(proid) {
	$.ajax({
		url : "load-product-by-id.php",
		type : "POST",
		data : "proid="+proid+"&",
		success: function(data){
			$('#myModalMsg').html(data);
			$('#myModal').modal('show');
		},
		error: function(jXHR, textStatus, errorThrown){
			alert(jXHR.responseText);
		}
	})
}

function addToCart(proid, qty, price) {
	var pro_dis = document.getElementById('pro_dis').value;
	var pro_qty = document.getElementById('pro_qty').value;
	if(qty<pro_qty){
		document.getElementById('errmsg').innerHTML = "Not available "+pro_qty+" Quantity in Stock..";
	}else{
		$.ajax({
			url : "load-product-add-cart.php",
			type : "POST",
			data : "proid="+proid+"& pro_dis="+pro_dis+"& pro_qty="+pro_qty+"& price="+price+"&",
			success: function(data){
				if(data>0){
					$('#errmsg').html('');
					$('#myModal').modal('hide');
					alertify.success("<h4>Success <br>Product Added..!</h4>");
					loadCartPanel();
				}else{
					$('#errmsg').html(data);
				}
			},
			error: function(jXHR, textStatus, errorThrown){
				alert(jXHR.responseText);
			}
		})
	}
}

function loadCartPanel() {
	$.ajax({
		url : "load-cart-panel.php",
		type : "POST",
		//data : "proid="+proid+"&",
		success: function(data){
			$('#cart_items').html(data);
		},
		error: function(jXHR, textStatus, errorThrown){
			alert(jXHR.responseText);
		}
	})
}

function loadCartPanel2() {
	$.ajax({
		url : "load-cart-panel-2.php",
		type : "POST",
		//data : "proid="+proid+"&",
		success: function(data){
			$('#scanmsg2').html(data);
		},
		error: function(jXHR, textStatus, errorThrown){
			alert(jXHR.responseText);
		}
	})
}

function deleteCartItem(proid, dis) {
	$.ajax({
		url : "load-cart-item-delete.php",
		type : "POST",
		data : "proid="+proid+"& dis="+dis+"&",
		success: function(data){
			if(data>0){
				loadCartPanel();
				loadCartPanel2()
				alertify.success("Product Deleted..!");
			}else{
				alertify.success(data);
			}
		},
		error: function(jXHR, textStatus, errorThrown){
			alert(jXHR.responseText);
		}
	})
}

function deleteAllCart() {
	alertify.confirm("Do you want clear all items in cart ?", function (e) {
		if (e) {

			$.ajax({
				url : "load-cart-item-all-delete.php",
				type : "POST",
		//data : "proid="+proid+"& dis="+dis+"&",
		success: function(data){
			if(data>0){
				loadCartPanel2();
				loadCartPanel();
				alertify.log("All Cart Items Deleted..!");
			}else{
				alertify.success(data);
			}
		},
		error: function(jXHR, textStatus, errorThrown){
			alert(jXHR.responseText);
		}
	})
		}
	});
}

function searchbarcode() {
	var searchbarcode = document.getElementById('searchbarcode').value;
	var searchQty = document.getElementById('searchQty').value;

	if(searchbarcode && searchQty>0){
		$.ajax({
			url : "load-search-barcode-by-barcode.php",
			type : "POST",
			data : "searchbarcode="+searchbarcode+"& searchQty="+searchQty+"&",
			success: function(data){
				if(data>0){
					$('#scanmsg').html("");
					loadCartPanel2();
					loadCartPanel();
				}else{
					$('#scanmsg').html(data);
				}
			},
			error: function(jXHR, textStatus, errorThrown){
				alert(jXHR.responseText);
			}
		})
	}else{
		$('#scanmsg').html("Enter barcode number & quantity..");
	}
}


function checkBarcodeStatus() {
	var str = document.getElementById('proSelect').value;
	if(str=='0'){
		loadCartPanel2();
		$('#myModal2').modal('show');
	}
}

function selectPaymentOption(option) {
	var totalbillvalue = document.getElementById("total_amount_view").innerHTML;

	if(option=='cash'){
		document.getElementById("paytypeview").innerHTML = 'cash';
		document.getElementById("cashamount").value = "";
		document.getElementById("cashamount").disabled = false; 
		document.getElementById("cardno").value = ''; 
		document.getElementById("cardno").disabled = true; 
		document.getElementById("balanceview").innerHTML = "";
		document.getElementById("cash").style.backgroundColor='#e3e3e3';
		document.getElementById("debit").style.backgroundColor='#fff';
		document.getElementById("credit").style.backgroundColor='#fff';
	}else if(option=='debit'){
		document.getElementById("paytypeview").innerHTML = 'debit';
		document.getElementById("cashamount").value = totalbillvalue; 
		document.getElementById("cashamount").disabled = true; 
		document.getElementById("cardno").value = ''; 
		document.getElementById("cardno").disabled = false; 
		document.getElementById("balanceview").innerHTML = "";
		document.getElementById("cash").style.backgroundColor='#fff';
		document.getElementById("debit").style.backgroundColor='#e3e3e3';
		document.getElementById("credit").style.backgroundColor='#fff';
	}else if(option=='credit'){
		document.getElementById("paytypeview").innerHTML = 'credit';
		document.getElementById("cashamount").value = totalbillvalue;
		document.getElementById("cashamount").disabled = false; 
		document.getElementById("cardno").value = ''; 
		document.getElementById("cardno").disabled = true; 
		document.getElementById("balanceview").innerHTML = "Select Customer";
		document.getElementById("cash").style.backgroundColor='#fff';
		document.getElementById("debit").style.backgroundColor='#fff';
		document.getElementById("credit").style.backgroundColor='#e3e3e3';

	}
}

function calBalance() {
	var totalbillvalue = document.getElementById("total_amount_view").innerHTML;
	var cash = document.getElementById("cashamount").value;
	var balance = +cash - +totalbillvalue;
	document.getElementById("balanceview").innerHTML ="Balance : "+ balance;
}

function openScannerPanel() {
	loadCartPanel2();
	$('#myModal2').modal('show');
}

function cloasemyModal() {
	$('#myModal').modal('hide');
}

function saveBill() {
	var totalbillvalue = document.getElementById("total_amount_view").innerHTML;
	var customer = document.getElementById("cusSelect2").value;
	var cashamount = document.getElementById("cashamount").value;
	var cardno = document.getElementById("cardno").value;
	var pay_type = document.getElementById("paytypeview").innerHTML;

	$.ajax({
		url : "load-save-bill.php",
		type : "POST",
		data : "totalbillvalue="+totalbillvalue+"& cashamount="+cashamount+"& customer="+customer+"& cardno="+cardno+"& pay_type="+pay_type+"&",
		success: function(data){
			if(data>0){
				$('#myModal2').modal('hide');
				alertify.alert("Order has been saved<br>Order ID : "+data);
				$('#myModal2').modal('hide');
				loadCartPanel();
				window.open("https://friendlypaws.xyz/dashboard/salesreciept.php?order_id="+data, "_blank");
			}else{
				alertify.error(data);
			}
		},
		error: function(jXHR, textStatus, errorThrown){
			alert(jXHR.responseText);
		}
	})

}