<?php
ob_start();
include 'dheader.php';
$success = "";
$danger = "";
if (isset($_POST['submitf'])) {
 	# code...
 	extract($_POST, EXTR_OVERWRITE);
 	$date = $_POST['date'];
	$ecus_date = explode('/', $date);
	$set_date = $ecus_date[2].'/'.$ecus_date[0].'/'.$ecus_date[1];

	$change_date = explode("/", $date);
	$show_date = $change_date[1]."/".$change_date[0]."/".$change_date[2];
	$php_date = $change_date[2]."/".$change_date[0]."/".$change_date[1];


	//$cs_id = $_GET['pay'];
	$action = 0;
	($type == 1) ? $action = 1 : $action = 2;
		$sql = $db->query("INSERT INTO payments (date, php_date, show_date, cs_id, type, amount, bank, description, action, debcre) 
			VALUES ('$date', '$php_date', '$show_date', '$pay', '$type', '$amount', '$bank', '".addslashes($description)."', 1, $debcre)");
		$checks=$db->query("select max(id) as lastid from payments");
		$fetchid=$checks->fetchArray();
	    $id = $fetchid['lastid']; //$db->insert_id;
	echo "
			<script>
				window.open('print-invoice3.php?invoice='+$id,'','width=595px'); 
			</script>
		";
 	if($sql){
 		$success = "<strong>Success!</strong> Payment added successfully.";
 		//echo '<meta http-equiv="refresh" content="0;url=print-invoice3.php?id=$ids">';
 		
    
 	}else{
 		$danger = "<strong>Warning!</strong> Something went wrong.";
 	} 
 }

if (isset($_GET["pay"])) {
 	$get_pay = preg_replace('/[^0-9]/', "", strtolower($_GET["pay"]));
 	$sql = $db->query("SELECT * FROM customer_setup WHERE id = '".$get_pay."'");
	$row = $sql->fetchArray();
	}    
?>
<div class="modal fade hidden-print" id="myModal" tabindex="-1" role="dialog" aria-hidden="true"></div>
	<div class="wrapper ">
		<?php include 'leftbar.php'; ?>
		<!-- left-bar -->
		<?php include 'topbar.php'; ?>
			<!-- top-bar -->
			<div class="main-content">

<div class="row" id="form">
	<div class="spinner" id="grid-loader" style="display:none">
	  <div class="rect1"></div>
	  <div class="rect2"></div>
	  <div class="rect3"></div>
	</div>
	
	<div class="col-md-12">
			

			<form action="" id="customer_form" class="form-horizontal" enctype="multipart/form-data" method="post" accept-charset="utf-8" novalidate="novalidate">

			<div class="panel panel-piluku">
				<div class="panel-heading">
		                <h3 class="panel-title">
		                    <i class="ion-edit"></i> 
		                    Customer Payment							<small>(Fields in red are required)</small>
		                </h3>
		                <a href="customerledger.php?name=c<?php echo $row['id']; ?>" title="Account Ledger" class="btn btn-success " style="float: right;">View Ledger</a>
		        </div>

			<div class="panel-body">
					<?php
                    if (isset($_POST['submitf'])) {
                    ?>
                    <div class="alert alert-<?php echo ($success) ? "success" : "danger"; ?>"> 
                            <?php echo ($success) ? $success : $danger; ?>                          
                    </div>
                 <?php } ?>

				<div class="row">
	<div class="col-md-12">

	<div class="form-group">
			<label for="date" class="required col-sm-3 col-md-3 col-lg-2 control-label ">Date :</label>			<div class="col-sm-9 col-md-9 col-lg-10">
				<input type="text" name="date" value="<?php echo date('m/d/Y'); ?>" class="form-control datepicker" id="date">
			</div>
		</div>

		<div class="form-group">
		    <span id="cus_balance" style="color:red; margin-left: 18px;"></span>
			<label for="first_name" class="required col-sm-3 col-md-3 col-lg-2 control-label ">Customer Name :</label> 			<div class="col-sm-9 col-md-9 col-lg-10">
				
				<select name="pay" class="ui search selection dropdown form-control" id="search-select">
				  <option value="">Customer Name</option>
				  <?php
					$listNames = $db->query("SELECT * FROM customer_setup");
					while($rowNames = $listNames->fetchArray()){
						$selected = "";
						if (isset($_GET["pay"])) {
						 	$cus_id = preg_replace('/[^0-9]/', "", strtolower($_GET["pay"]));
						 	if ($cus_id == $rowNames['id']) {
						 		$selected = "selected";
						 	}
						}    
				?>
					<option value="c<?php echo $rowNames['id']; ?>" <?php echo $selected; ?>><?php echo $rowNames['customer_name']; ?></option>
				<?php } ?>
				</select>
			</div>
		</div>

		<div class="form-group">
			<label for="type" class="col-sm-3 col-md-3 col-lg-2 control-label required">Payment Type :</label>			<div class="col-sm-9 col-md-9 col-lg-10">
			<select name="type" class="form-control" id="type">
				<option value="1">Cash</option>
				<option value="2">Check</option>
			</select>
			</div>
		</div>

		<div class="form-group">
			<label for="Debit/Credit" class="col-sm-3 col-md-3 col-lg-2 control-label required">Debit/Credit :</label>			<div class="col-sm-9 col-md-9 col-lg-10">
			<select name="debcre" class="form-control" id="debcre">
				<option value="1">Credit</option>
				<option value="2">Debit</option>
			</select>
			</div>
		</div>

		<div class="form-group">
			<label for="amount" class="col-sm-3 col-md-3 col-lg-2 control-label required">Amount :</label>			<div class="col-sm-9 col-md-9 col-lg-10">
			<input type="text" name="amount" value="" class="form-control" id="amount">
			</div>
		</div>
		<div class="form-group">	
			<label for="bank" class="col-sm-3 col-md-3 col-lg-2 control-label ">Bank :</label>			<div class="col-sm-9 col-md-9 col-lg-10">
			<input type="text" name="bank" list="bankNames"  class="form-control" id="bank">
			<a href="addbank.php" target="_blank"><input type="button"  value="Add Bank" class=" submit_button btn btn-success"></a>
				<datalist id="bankNames">
				<?php
	                $sn4d  = "SELECT * FROM ";
	                $sn4d .= "bank";
	                $sn4d_run = $db->query($sn4d);
	                while($sn4d_row = $sn4d_run->fetchArray())
	                {
	            ?>
	                <option value="<?php echo $sn4d_row["name"]; ?>">
	            <?php
	                }
	            ?>    
	            </datalist>
			</div>
		</div>
		


	<div class="form-group">	
<label for="description" class="col-sm-3 col-md-3 col-lg-2 control-label ">Description :</label>	<div class="col-sm-9 col-md-9 col-lg-10">
	<textarea name="description" cols="17" rows="5" id="description" class="form-control text-area"></textarea>
	</div>
</div>
 
	</div><!-- /col-md-12 -->
</div><!-- /row -->				
							
						<div class="form-actions pull-right">
														
							<input type="submit" name="submitf" value="Submit" id="submitf" class=" submit_button btn btn-primary" onclick="return confirm('Are you sure you want to save this payment?');">
						</div>
					</div>
				</div>
										</form>
			</div>
		</div><!-- /row -->


	

			
<?php include 'dfooter.php'; ?>
<script>
    $(document).ready(function(){	
	$('#search-select').on('change', function(){
		var name = $(this).val();
		$.ajax({
		  method: "POST",
		  url: "customerpre.php",
		  data: { name: name },
		  success: function( data ) {
			$('#cus_balance').text("Previous Balance: " +data);
		  }
		});
	});
});	
</script>