<?php
session_start();
include 'load.php';

$user_id = $_SESSION['user_id'];
$branch_id = $_SESSION['branch_id'];

if(isset($_POST['save']))

{		
        $tot = 0;
		$customer = $_POST['customer'];
		$credit = $_POST['amount'];
		$paymethod = $_POST['paymethod'];
		$cheque_number = $_POST['cheque_number'];
		$cheque_date = $_POST['cheque_date'];
		
		
		
        $cusinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobilecustomers WHERE coid ='$customer'"));
        $credit_limit =  $cusinfo['credit_limit'];
        $ccredit =  $cusinfo['ccredit'];
       // $tot = $ccredit + $credit;
	   if($ccredit > $credit){
			$tot = $ccredit - $credit;
	   }else{
			$errMSG = "Your Balance is ".$ccredit;
	   }
        if($tot >= $credit_limit){
            $errMSG = "Credit Limit is ".$credit_limit;
        }

		if(!isset($errMSG))

		{

           
			if($paymethod=="cash"){
						$sql = "UPDATE mobilecustomers SET ccredit='$tot' WHERE coid='$customer'";
						if (mysqli_query($conn, $sql)) 
						{
							$sql1 = "INSERT INTO mobile_credit_payments (customer_id, amount,paymethod,chequenumber,chequedate) VALUES ('$customer', '$credit','$paymethod','$cheque_number','$cheque_date')";
							if ($conn->query($sql1) === TRUE) {
								$successMSG = "Successfully! update Credit Amount";
								header("refresh:2;listcredit.php"); // redirects image view page after 5 seconds.
							}

							

						}

						else

						{

							$errMSG = "error while inserting....";

						}
			}else{
				$sql1 = "INSERT INTO mobile_credit_payments (customer_id, amount,paymethod,chequenumber,chequedate) VALUES ('$customer', '$credit','$paymethod','$cheque_number','$cheque_date')";
				if ($conn->query($sql1) === TRUE) {
					$successMSG = "Successfully! update Credit Amount";
					header("refresh:2;listcredit.php"); // redirects image view page after 5 seconds.
				}
			}
			

		}

	}

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
<meta name="description" content="POS - Eat More Resturant   | Pos & Invetory System | Dashboard
">
<meta name="keywords" content="Eat More Resturant   | Pos & Invetory System | Dashboard
">
<meta name="author" content="Eat More Resturant   | Pos & Invetory System | Dashboard
">
<meta name="robots" content="noindex, nofollow">

<title>Pyament Settlemet | Eat More Resturant   | Pos & Invetory System | Dashboard
</title>


<?php
require_once 'headercss.php';
?>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.1.0-rc.0/js/select2.min.js"></script>

</head>
<body>
<div id="global-loader">
<div class="whirly-loader"> </div>
</div>

<div class="main-wrapper">
<?php
require_once 'header.php';
?>

<?php
require_once 'navbar.php';
?>

<div class="page-wrapper">
<div class="content">
<div class="page-header">
<div class="page-title">
<h4>Pyament Settlemet Management</h4>
<h6>Add/Update Pyament Settlemet</h6>
</div>
</div>

<div class="card">
<div class="card-body">
<form method="POST" enctype="multipart/form-data">
									<?php

									if(isset($errMSG)){

									?>
			
									<div class="alert alert-danger alert-dismissible alert-alt solid fade show">
									<button type="button" class="close h-100" data-dismiss="alert" aria-label="Close"><span><i class="mdi mdi-close"></i></span></button>
									<strong>Error!</strong> <?php echo $errMSG; ?>
									</div>

									<?php

									}
	
									else if(isset($successMSG)){

									?>
			
									<div class="alert alert-success alert-dismissible alert-alt solid fade show">
									<button type="button" class="close h-100" data-dismiss="alert" aria-label="Close"><span><i class="mdi mdi-close"></i></span></button>
									<strong>Success!</strong> <?php echo $successMSG; ?>.
									</div>

									<?php

									}

									?>
<div class="row">
<div class="col-lg-4 col-sm-6 col-12">
<div class="form-group">
<label for="fullName">Customer</label>
            <select class="form-control" id="customer" name="customer" required>
					<option value="">Select Customer</option>
					<?php
					$cus2 = "SELECT * FROM mobilecustomers WHERE ccredit > 0 AND status=1";
					$result = $conn->query($cus2);
					if ($result->num_rows > 0) {
						while ($row = $result->fetch_assoc()) {
							$coid = $row["coid"];
							echo $cname = $row["cname"];
							$cphone = $row["cphone"];
							$ccredit = $row["ccredit"];
							echo '<option value="' . $coid . '">' . $cname . ' | ' . $cphone . ' | Rs.' . $ccredit . '</option>';
						}
					}
					//$conn->close();
					?>
				</select>
</div>
</div>
<div class="col-lg-4 col-sm-6 col-12">
<div class="form-group">
<label for="fullName">Payment Method</label>
            <select class="form-control" id="cash_type" name="paymethod" required onchange="setPaymentMethod();">
					<option selected disabled value="">Select Payment Method</option>
							<option value="cash">Cash</option>
							
							<option value="cheques">Cheques</option>
			</select>
</div>
</div>

			<div class="col-lg-4 col-sm-6 col-12" id="cheque_nowrap" style="display: none;">
					<div class="form-group">
						<label style="font-weight:bold;color:#000000;">Cheque Number</label>
						<input type="text" class="form-control" name="cheque_number" id="cheque_number" value="" placeholder="Cheque Number">
					</div>
			</div>
<div class="col-lg-4 col-sm-6 col-12" id="cheque_datewrap" style="display: none;">

<div class="form-group">
						<label style="font-weight:bold;color:#000000;">Cheque Date</label>
						<input type="date" class="form-control" name="cheque_date" id="cheque_date" value="" placeholder="Cheque Date">
					</div>


</div>
<div class="col-lg-4 col-sm-6 col-12">
<div class="form-group">
<label for="fullName">Paid Amount</label>
<input type="number" name="amount" class="form-control" placeholder="Enter Amount" step="0.01" min="1" required>
</div>
</div>
<div class="col-lg-12">
<a href="customerlist.php" class="btn btn-cancel">Cancel</a>
<button type="submit" name="save" class="btn btn-submit me-2">Save</button>
</div>
</div>
</form>
</div>
</div>

</div>
</div>
</div>

	<script src="assets/js/jquery-3.6.0.min.js"></script>

	<script src="assets/js/feather.min.js"></script>

	<script src="assets/js/jquery.slimscroll.min.js"></script>

	<script src="assets/js/jquery.dataTables.min.js"></script>
	<script src="assets/js/dataTables.bootstrap4.min.js"></script>

	<script src="assets/js/bootstrap.bundle.min.js"></script>

	<script src="assets/plugins/select2/js/select2.min.js"></script>

	<script src="assets/js/moment.min.js"></script>
	<script src="assets/js/bootstrap-datetimepicker.min.js"></script>

	<script src="assets/plugins/sweetalert/sweetalert2.all.min.js"></script>
	<script src="assets/plugins/sweetalert/sweetalerts.min.js"></script>
	<script src="assets/alertify/alertify.js"></script>
		<script src="assets/alertify/alertify.min.js"></script>

	<script src="assets/js/script.js"></script>
	<!-- <script src="process.js"></script> -->
	<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>
	<script>
			function setPaymentMethod() {
		var cash_type = document.getElementById("cash_type").value;
		
		if (cash_type == 'cheques') {

			document.getElementById("cheque_datewrap").style.display = "block";
			document.getElementById("cheque_nowrap").style.display = "block";
			
		} else if (cash_type == 'card') {
			document.getElementById("cardnoshow").style.display = "block";
			document.getElementById("creditAmount").style.display = "none";
			document.getElementById("totalcreditAmount").style.display = "none";
			document.getElementById("cheque_datewrap").style.display = "none";

		} else if (cash_type == 'credit') {
			document.getElementById("cheque_datewrap").style.display = "none";
			document.getElementById("cardnoshow").style.display = "none";
			document.getElementById("creditAmount").style.display = "block";
			document.getElementById("totalcreditAmount").style.display = "block";

			var customer = document.getElementById("billcustomer").value;
		}else{	
			document.getElementById("cheque_datewrap").style.display = "none";
			document.getElementById("cheque_nowrap").style.display = "none";
		}
	
	
	}
	</script>
</body>
</html>