<?php 
require("php_con.php");
require("function.php");

if(isset($_POST['pay_btn'])){
	$sale_details_cash_payment=mysqli_real_escape_string($connation,$_POST['sale_details_cash_payment']);
	$cs_mobile=mysqli_real_escape_string($connation,$_POST['cs_mobile']);
	$bill_id=bill_id();
	if(mysqli_query($connation,"INSERT INTO
	sale_details (sale_details_id, sale_details_billid, sale_details_cash_payment, sale_details_card_payment, sale_details_cus_payment, sale_details_discount, sale_details_user, sale_details_customer, sale_details_date, sale_details_time)
	VALUES (NULL, '$bill_id', '$sale_details_cash_payment', '0', '$sale_details_cash_payment', '0', '$_COOKIE[user_id]', '$cs_mobile', '$current_date', '$current_time')")){
	echo "<script>window.open('poin-of-sale?print=$bill_id','_parent');</script>";
	}
}
?>
<link rel="stylesheet" href="data_table/css/bootstrap.css">
<link rel="stylesheet" href="data_table/css/dataTables.bootstrap4.min.css">

<form method="post" autocomplete="off">
<input name="cs_mobile" type="text" autofocus="autofocus" required="required" class="form-control form-control-sm" id="cs_mobile" placeholder="Enter Customer Contact Number" list="cus_list">
</form>

<?php 
if(isset($_POST['cs_mobile'])){
$cs_mobile=(int)mysqli_real_escape_string($connation,$_POST['cs_mobile']);
$list_qury=mysqli_query($connation,"SELECT * FROM customer_details WHERE cs_mobile='$cs_mobile'");
if(mysqli_num_rows($list_qury)>0){
$list_resalt=mysqli_fetch_assoc($list_qury);
?>

<table class="table-sm" align="center">
  <tbody>
    <tr>
      <td>Name</td>
      <td><?php echo $list_resalt['cs_name']; ?></td>
    </tr>
    <tr>
      <td>Mobile</td>
      <td><?php echo '0'.(int)$list_resalt['cs_mobile']; ?></td>
    </tr>
    <tr>
      <td>NIC</td>
      <td><?php echo $list_resalt['cs_nic']; ?></td>
    </tr>
    <tr>
      <td>Reg. Time</td>
      <td><?php echo date('Y-m-d h:i:s A',strtotime($list_resalt['cs_add_date'].' '.$list_resalt['cs_add_time'])) ?></td>
    </tr>
    <tr>
      <td>Total Bill</td>
      <td><?php echo number_format((float)cus_bill($list_resalt['cs_mobile'])); ?></td>
    </tr>
    <tr>
      <td>Balance</td>
      <td>
		<h2>
		<?php 
		if(cus_balance($list_resalt['cs_mobile'])<0){
		?>
		<span class="badge badge-danger"><?php echo number_format((float)cus_balance($list_resalt['cs_mobile']),2); ?></span>
		<?php } else{ ?>
		<span class="badge badge-success"><?php echo number_format((float)cus_balance($list_resalt['cs_mobile']),2); ?></span>
		<?php } ?>
		</h2>
		</td>
    </tr>
	  <tr><td colspan="2"><hr></td></tr>
	  <form method="post" autocomplete="off">
		  <input name="cs_mobile" type="hidden" value="<?php echo $list_resalt['cs_mobile']; ?>">
	  <tr>
	  <td>Payment</td>
		  <td><input name="sale_details_cash_payment" type="text" required="required" class="form-control form-control-sm" id="sale_details_cash_payment" placeholder="Amount" pattern="[-+]?[0-9]*\.?[0-9]*"></td>
	  </tr>
<tr><td colspan="2" align="right"><button type="submit" name="pay_btn" class="btn btn-sm btn-dark">Pay Amount</button></td></tr>
		  </form>
  </tbody>
</table>
<?php } else{ ?>
<div class="alert alert-danger text-center">Customer not found! Please try again.</div>
<?php } } ?>

<datalist id="cus_list">
<?php 
$c_qury=mysqli_query($connation,"SELECT *
FROM customer_details
ORDER BY cs_name");
while($c_resalt=mysqli_fetch_assoc($c_qury)){
?>
<option value="<?php echo "0".(int)$c_resalt['cs_mobile']; ?>"><?php echo $c_resalt['cs_name']; ?> <?php echo $c_resalt['cs_nic']; ?></option>
<?php } ?>
</datalist>

<?php if(isset($_GET['print'])){ if(setting_view("bill_pint")==1){ ?>
<iframe src="bill_print.php?print=<?php echo $_GET['print']; ?>" style="display: none; width: 0px; height: 0px;"></iframe>
<?php } } ?>
	