<?php 
session_start();
require_once("class/connectdb.php");
$con = new DbConnect;
$con = $con->connect();

$user_id = $_SESSION['user']; 
if(!isset($_SESSION['cart_item'])){
	$_SESSION['cart_item'] = [];
}

$user_id = $_SESSION['user'];
// it will never let you open index(login) page if session is set
if(!isset($_SESSION['user'])) {
	header("location: index");
	exit;
}

$total_price = 0;
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1
	
	<link rel="apple-touch-icon" sizes="57x57" href="fav/apple-icon-57x57.png">
	<link rel="apple-touch-icon" sizes="60x60" href="fav/apple-icon-60x60.png">
	<link rel="apple-touch-icon" sizes="72x72" href="fav/apple-icon-72x72.png">
	<link rel="apple-touch-icon" sizes="76x76" href="fav/apple-icon-76x76.png">
	<link rel="apple-touch-icon" sizes="114x114" href="fav/apple-icon-114x114.png">
	<link rel="apple-touch-icon" sizes="120x120" href="fav/apple-icon-120x120.png">
	<link rel="apple-touch-icon" sizes="144x144" href="fav/apple-icon-144x144.png">
	<link rel="apple-touch-icon" sizes="152x152" href="fav/apple-icon-152x152.png">
	<link rel="apple-touch-icon" sizes="180x180" href="fav/apple-icon-180x180.png">
	<link rel="icon" type="image/png" sizes="192x192"  href="fav/android-icon-192x192.png">
	<link rel="icon" type="image/png" sizes="32x32" href="fav/favicon-32x32.png">
	<link rel="icon" type="image/png" sizes="96x96" href="fav/favicon-96x96.png">
	<link rel="icon" type="image/png" sizes="16x16" href="fav/favicon-16x16.png">
	<link rel="manifest" href="fav/manifest.json">
	<meta name="msapplication-TileColor" content="#ffffff">
	<meta name="msapplication-TileImage" content="fav/ms-icon-144x144.png">
	<meta name="theme-color" content="#ffffff">
	  
    <title>Trustee Admin! | </title>

    <!-- Bootstrap -->
    <link href="../vendors/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="../vendors/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <!-- NProgress -->
    <link href="../vendors/nprogress/nprogress.css" rel="stylesheet">
    <!-- iCheck -->
    <link href="../vendors/iCheck/skins/flat/green.css" rel="stylesheet">
    <!-- bootstrap-progressbar -->
    <link href="../vendors/bootstrap-progressbar/css/bootstrap-progressbar-3.3.4.min.css" rel="stylesheet">
    <!-- PNotify -->
    <link href="../vendors/pnotify/dist/pnotify.css" rel="stylesheet">
    <link href="../vendors/pnotify/dist/pnotify.buttons.css" rel="stylesheet">
    <link href="../vendors/pnotify/dist/pnotify.nonblock.css" rel="stylesheet">

    <!-- Custom Theme Style -->
    <link href="../build/css/custom.min.css" rel="stylesheet">
	
	<link href="css/custom.css" rel="stylesheet" type="text/css">
	
  </head>

<body class="">
    <div class="container body">
      <div class="">
        
 

        <!-- page content -->
        <div class='right_col' role='main'>
		<div class="row">
            <div class="col-md-8 col-sm-8 col-xs-12">
              <div class="x_panel tile ">
                <div class="x_title">
                  <h2>
				    <a class="pointer glyphicon glyphicon-off" href="logout">Logout </a>
				    <a style="padding-left: 200px" class="pointer" href="vendor/mike42/escpos-php/example/interface/eod.php"><u>EOD Print</u> </a>
					<a style="padding-left: 100px" class="pointer" href="history"><u>Sent Transaction</u> </a>
				  </h2>
				  
                  <ul class="nav navbar-right panel_toolbox">
				    <input type="text" placeholder="Search" onkeyup="searchTest()" id="searchValue">
                  </ul>
                  <div class="clearfix"></div>
                </div>
                <div class="x_content">
                    <div class="row" id="test_result">
						<?php 
						 $sql = mysqli_query($con, "SELECT * FROM labtest LIMIT 44");
						 while($row = mysqli_fetch_array($sql)):
						?>
						<div class="animated flipInY col-lg-3 col-md-3 col-sm-6 col-xs-12 pointer" style="color:white;">
							<div class="tile-stats" style="height: 90px; background-color: blue" onclick="addLabTest(<?php echo $row['test_id'] ?>)">
							     <div class="count" style="font-size: 15px" id="price-<?php echo $row['test_id'] ?>" testprice="<?php echo $row['price'] ?>"><?php echo number_format($row['price'])?></div>
							     <p><span id="test_name-<?php echo $row['test_id'] ?>" testname="<?php echo $row['test_name'] ?>"><b><?php echo substr($row['test_name'],0, 120) ?>.</b></span></p>
							</div>
						</div>
						<?php endwhile; ?>
                    </div>
				</div>
              </div>
            </div>

            <div class="col-md-4 col-sm-4 col-xs-12 fixed"  style="color: white; background-color: black; height: 100%">
              <div class=" " style="color: white; background-color: black; height: 100%">
			  <div class="row x_title">
			      <div class="col-md-8 col-sm-8 col-xs-8 ">
					  <h2>Sales</h2> 
					  <div class="clearfix"></div>
				  </div>
				  <div class="col-md-4 col-sm-4 col-xs-4">
				     <button type="button" class="btn btn-default" data-toggle="modal" data-target=".bs-example-modal-lg">Add customer</button>
				  </div>
			  </div>
			  
			  
                
                <div class="x_content " >
				  <div class="row customer" style="color:white">
					  <div class='col-md-4 col-sm-4 col-xs-4'><b>Customer name :</b></div>
					  <div class='col-md-8 col-sm-8 col-xs-8' cid="" id="customerName"><b>No customer selected.</b></div>
				  </div>
				
				  </br>
				  
				  
				  
				  <div class="row">
					  <div class='col-md-6 col-sm-6 col-xs-6'><b>Test</b>
					  </div>
					  <div class='col-md-3 col-sm-3 col-xs-3'><b>Qty</b>
					  </div>
					  <div class='col-md-3 col-sm-3 col-xs-3'><b>Price</b>
					  </div>
				  </div>
				  
				  <div id="test" >
				      <?php foreach($_SESSION['cart_item'] as $key => $val):  
						  $resPrice = $_SESSION['cart_item'][$key]['price'] * $_SESSION['cart_item'][$key]['qty'];
						  $total_price += $resPrice;
					  ?>
					  <div class="row" id="testElement" style="padding-top: 10px" >
						  <div class="col-md-6 col-sm-6 col-xs-6"> <?php echo $_SESSION['cart_item'][$key]['test_name']; ?>
						  </div>
						  <input class="col-md-3 col-sm-3 col-xs-3" type="number" onchange="addToTill(<?php echo $_SESSION['cart_item'][$key]['tid']; ?>)" id="myNumber-<?php echo $_SESSION['cart_item'][$key]['tid']; ?>" value="<?php echo $_SESSION['cart_item'][$key]['qty']; ?>" style="color:black">
						  <div class="col-md-3 col-sm-3 col-xs-3">
						      <?php echo $resPrice ?>
							  <span class="cancel pointer" onclick="removeFromTill(<?php echo $_SESSION['cart_item'][$key]['tid']; ?>)"> x </span>
						  </div>
						 
					  </div>
					  <?php endforeach  ?>
				  </div>
				  
				  
				  
                </div>
				<div class="row" >
				    <div class="absolute">
					  <div class='col-md-6 col-sm-6 col-xs-6'><b><h2>Total</h2></b></div>
					  <div class='col-md-3 col-sm-3 col-xs-3'></div>
					  <div class='col-md-3 col-sm-3 col-xs-3'><b><h2><?php echo '₦' . number_format($total_price) ?></h2></b></div><br />
						<div class="divider"></div>
					      <span onclick="sendToCashier()"><button type="submit" name="submit" class="btn btn-primary" >Send for payment</button></span>
                          
                        
					</div>
				</div>
              </div>
            </div>
			
			<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-hidden="true" id="myModal">
				<div class="modal-dialog modal-lg">
				  <div class="modal-content">

					<div class="modal-header">
					  <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span>
					  </button>
					  <h4 class="modal-title" id="myModalLabel">Customer list</h4>
					</div>
					<div class="modal-body">
					  <h4>Below is a list of customers already registered.</h4>
					  <table id="datatable" class="table table-striped table-bordered">
						  <thead>
							<tr>
								<th><b>Name</b></th>
								<th ><b>Email</b></th>
								<th><b>Phone number</b></th>
								<th><b>Action</b></th>
							</tr>
						  </thead>
						 
						  <tbody>
							<?php 
								$sql =  mysqli_query($con, "SELECT * FROM loyalty ORDER BY loyal_id DESC "); 
										while($row = mysqli_fetch_array($sql)):
							?>
									<tr>
										<td><?php echo $row['first_name'] . ' ' . $row['last_name']; ?></td>
										<td><?php echo $row['email']; ?></td>
										<td><?php echo $row['phone']; ?></td>
										<td>
										  <div class="btn-group">
											 <span onclick="addCustomerToTest('<?php echo $row['first_name'] . ' ' . $row['last_name']; ?>', '<?php echo $row['loyalty_id']; ?>')"><a  class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span></a></span>
										  </div>
										</td>
									</tr>
							<?php endwhile; ?>
						  </tbody>
                    </table>
					</div>
				 </div>
				</div>
            </div>
          </div>
          </div>
        </div>
        <!-- /page content -->
 <!-- /footer content -->
      </div>
    </div>
     <!-- jQuery -->
    <script src="../vendors/jquery/dist/jquery.min.js"></script>
    <!-- Bootstrap -->
    <script src="../vendors/bootstrap/dist/js/bootstrap.min.js"></script>
    <!-- FastClick -->
    <script src="../vendors/fastclick/lib/fastclick.js"></script>
    <!-- NProgress -->
    <script src="../vendors/nprogress/nprogress.js"></script>
    <!-- bootstrap-progressbar -->
    <script src="../vendors/bootstrap-progressbar/bootstrap-progressbar.min.js"></script>
    <!-- iCheck -->
    <script src="../vendors/iCheck/icheck.min.js"></script>
	
	<script src="../vendors/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="../vendors/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
    <script src="../vendors/datatables.net-buttons/js/dataTables.buttons.min.js"></script>
    <script src="../vendors/datatables.net-buttons-bs/js/buttons.bootstrap.min.js"></script>
    <script src="../vendors/datatables.net-buttons/js/buttons.flash.min.js"></script>
    <script src="../vendors/datatables.net-buttons/js/buttons.html5.min.js"></script>
    <script src="../vendors/datatables.net-buttons/js/buttons.print.min.js"></script>
    <script src="../vendors/datatables.net-fixedheader/js/dataTables.fixedHeader.min.js"></script>
    <script src="../vendors/datatables.net-keytable/js/dataTables.keyTable.min.js"></script>
    <script src="../vendors/datatables.net-responsive/js/dataTables.responsive.min.js"></script>
    <script src="../vendors/datatables.net-responsive-bs/js/responsive.bootstrap.js"></script>
    <script src="../vendors/datatables.net-scroller/js/dataTables.scroller.min.js"></script>

    <!-- Custom Theme Scripts -->
    <script src="../build/js/custom.min.js"></script>
</html>

<!---- Personal Dev -->
<script>

function addCustomerToTest(name, cid){
	$("#customerName").attr("cid", cid);
	$('#customerName').html('<b>' + name + '</b>');
	$('#myModal').modal('hide');
}

function addLabTest(id){
		var testName  =  $('#test_name-'+id).attr('testname');
		var testPrice =  $('#price-'+id).attr('testprice');
		var qty       = 1 ;
		var query = 'add';
	    var action = 'increment';
		
		$.ajax({
			url	  :	"process.php",
			type  :	"POST",
			data  :	{
				attend_test_sale : 1, 
				query : query,
				action : action,
				test_name : testName,
				price : testPrice,
				test_id: id,
				qty : qty
			},
			success	:	function(result){ 
			//var res = JSON.parse(result);
			    location.reload();
			}
		})
};

function addToTill(id){
	var testName  =  $('#test_name-'+id).attr('testname');
	var testPrice =  $('#price-'+id).attr('testprice');
	var query = 'replace';
	var currentqty = $('#myNumber-'+id).val();
	var action = 'replace';
	
	$.ajax({
			url	  :	"process.php",
			type  :	"POST",
			data  :	{
				attend_test_sale : 1, 
				query : query,
				action : action,
				test_name : testName,
				price : testPrice,
				test_id: id,
				qty : currentqty
			},
			success	:	function(result){ 
			  location.reload();
			}
		})
}

function removeFromTill(id){
	
	var testName  =  $('#test_name-'+id).attr('testname');
	var testPrice =  $('#price-'+id).attr('testprice');
	var query = 'remove';
	var currentqty = $('#myNumber-'+id).val();
	var action = '';
	
	$.ajax({
			url	  :	"process.php",
			type  :	"POST",
			data  :	{
				attend_test_sale : 1, 
				query : query,
				action : action,
				test_name : testName,
				price : testPrice,
				test_id: id,
				qty : currentqty
			},
			success	:	function(result){ 
			  location.reload();
			}
	})
}

function sendToCashier(){
	  var cid = $('#customerName').attr('cid');
	  var d = new Date();
	  var transaction_id = ''+Math.floor((Math.random() * 100) + 1) + 'lab' + d.getTime();
		
	$.ajax({
		url	  :	"process.php",
		type  :	"POST",
		data  :	{
			send_to_cashier: 1,
			customer_id: cid,
			transaction_id: transaction_id
		},
		success	:	function(result){ 
		    var data = JSON.parse(result);
		    alert(data.status);
			window.location.reload();
		}
	})
};

function searchTest(){
	var searchValue = $('#searchValue').val();
	
	$.ajax({
		url	  :	"process.php",
		type  :	"POST",
		data  :	{
			search_test: 1,
			search_value: searchValue,
		},
		success	:	function(result){ 
			$('#test_result').html(result);
		}
	})
}

</script>




 