<!--PHP VARIABLE CONTROLS-->
<?php
	if(count($detail['voters']) >= 51 && count($detail['voters']) <= 200){
		$projectSize = 200;
	} else {
		$projectSize = count($detail['voters']);
	}
 ?>

 <!--EXT RESOURCES-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.debug.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.3.0/jspdf.plugin.autotable.js"></script>

<script>
	$(function(){
		$('#totalCost').click(function(){
			if($('#hdnTotalCost').is(":visible")){
				$('#hdnTotalCost').slideUp();
			}else{
				$('#hdnTotalCost').slideDown();
			}
		});

		$('#unpaidCost').click(function(){
			if($('#hdnUnpaidCost').is(":visible")){
				$('#hdnUnpaidCost').slideUp();
			}else{
				$('#hdnUnpaidCost').slideDown();
			}
		});

		$('#paidCost').click(function(){
			if($('#hdnPaidCost').is(":visible")){
				$('#hdnPaidCost').slideUp();
			}else{
				$('#hdnPaidCost').slideDown();
			}
		});
	});

	function checkout(){ //create new bill
		var amount = <?php echo (isset($detail['totalUnpaidCost']))?$detail['totalUnpaidCost']*100:"0"; ?>; // in cents
		var desc = <?php echo ($detail['status'] == 'Not Paid')?"'Payment for project with a size of ".$projectSize."'":"'Payment for additional voters'"; ?>;

		if(amount >= 100){
			$('#checkoutBtn').prop('disabled', true);

			var checkoutData = {
				'redirect_url' : '<?php echo base_url() . "checkout/saveBill" ?>',
				'callback_url' : '<?php echo base_url() . "checkout/saveBill" ?>',
				'itemID' : 'adyar6lr', // prod
				//'itemID' : 'cmm4dwlh', // dev
				'email' : $('#userEmail').val(),
				'mobile' : '6'+$('#userNum').val(),
				'name' : $('#userName').val(),
				'amount' :  amount,
				'desc' : desc
			};

			$.ajax({
				url:'<?php echo base_url(); ?>payment/createNewBill',
				type: 'POST',
				data: checkoutData,
				dataType: 'JSON',
				complete: function(data){
					var res = JSON.parse(data.responseText);

					if(res.error){
						alert(res.error.message[0]);
					}else{
						window.open(res.url);
					}
				}
			});
		} else {
			alert('Minimum cost per transaction is RM1.00.');
		}
	}

	function showBill(billID){
		var doc = new jsPDF();
		doc.text(20, 20, 'Bill Invoice');

		// Output as Data URI
		doc.output('dataurl');

		// $.ajax({
		// 	url:'<?php echo base_url(); ?>payment/getBillData',
		// 	type: 'POST',
		// 	data: {
		// 		'billID' : billID
		// 	},
		// 	dataType: 'JSON',
		// 	complete: function(data){
		// 		console.log(data);
		// 		if(data.statusText === "OK"){
		// 			var res = JSON.parse(data.responseText);

		// 			$.each(res, function(k, v){
		// 				console.log(k + ': ' + v);
		// 			});
		// 		}
		// 	}
		// });
	}
</script>