<?php 
include 'dheader.php';
$success = "";
$danger = "";
if (isset($_POST['submitf'])) {
 	# code...
 	extract($_POST, EXTR_OVERWRITE);
 	//$countQuery = $db->query("SELECT count(*) totalCount FROM supplier_setup WHERE supplier_name = '$fullname'");
 	//$rowcount = $countQuery->fetchArray();
 	//$totalCount = $rowcount['totalCount'];
 	//if ($totalCount < 1) {
 		# code...
 		$date = $_POST['date'];
 		$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];

		$ecus_date = explode('/', $date);
		$set_date = $ecus_date[2].'/'.$ecus_date[0].'/'.$ecus_date[1];

		$get_pay = preg_replace('/[^0-9]/', "", strtolower($_GET['edit']));
 		$sql = $db->query("SELECT * FROM supplier_setup WHERE id = '".$get_pay."'");
		$row = $sql->fetchArray();

		$current_balance = $row['current_balance'];
		$opening_balance = $row['open_balance'];
		
		$current_balance -= $opening_balance;
		$current_balance += $balance;

 		$sql = $db->query("UPDATE supplier_setup SET supplier_name = '$fullname', date = '$date', email = '$email', phone = '$phone_number', address = '$address', city = '$city', ntn = '$ntn', gst = '$gst', comments = '$comments', open_balance = '$balance', company_name = '$company_name', account_no = '$account_number', current_balance = '$current_balance', active = 1 WHERE id = '".$get_pay."'");

 		$sql = $db->query("UPDATE customerinvoice SET date = '$date', php_date = '$php_date', show_date = '$show_date', subtotal = '$balance', remaining = '$balance' WHERE payment_type = 'Opening Balance' AND name = '".$_GET['edit']."'");

 		$success = "<strong>Success!</strong> Supplier updated successfully."; 
 	}else{
 		$danger = "<strong>Warning!</strong> This Supplier name already exist.";
 	} 
 //}
 	$set_pay = preg_replace('/[^0-9]/', "", strtolower($_GET['edit']));
 	$sql = $db->query("SELECT * FROM supplier_setup WHERE id = '".$set_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> 
		                    Supplier Information							<small>(Fields in red are required)</small>
		                </h3>
		                <a href="supplierledger.php?name=s<?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="col-sm-3 col-md-3 col-lg-2 control-label required">Date :</label>			<div class="col-sm-9 col-md-9 col-lg-10">
			<input type="text" name="date" value="<?php echo $row['date']; ?>" class="form-control datepicker" id="date">
			</div>
		</div>
		
		<div class="form-group">
			<label for="first_name" class="required col-sm-3 col-md-3 col-lg-2 control-label ">Supplier Name :</label>			<div class="col-sm-9 col-md-9 col-lg-10">
				<input type="text" name="fullname" value="<?php echo $row['supplier_name']; ?>" class="form-control" id="fullname">
			</div>
		</div>

		<div class="form-group">
			<label for="email" class="col-sm-3 col-md-3 col-lg-2 control-label not_required">E-Mail :</label>			<div class="col-sm-9 col-md-9 col-lg-10">
			<input type="text" name="email" value="<?php echo $row['email']; ?>" class="form-control" id="email">
			</div>
		</div>
		<div class="form-group">	
			<label for="phone_number" class="col-sm-3 col-md-3 col-lg-2 control-label ">Phone Number :</label>			<div class="col-sm-9 col-md-9 col-lg-10">
			<input type="text" name="phone_number" value="<?php echo $row['phone']; ?>" class="form-control" id="phone_number">
			</div>
		</div>
		

<div class="form-group">	
<label for="address_1" class="col-sm-3 col-md-3 col-lg-2 control-label ">Address :</label>	<div class="col-sm-9 col-md-9 col-lg-10">
	<input type="text" name="address" value="<?php echo $row['address']; ?>" class="form-control" id="address">
	</div>
</div>

			<div class="form-group">	
<label for="city" class="col-sm-3 col-md-3 col-lg-2 control-label ">City :</label>	<div class="col-sm-9 col-md-9 col-lg-10">
	<input type="text" name="city" value="<?php echo $row['city']; ?>" class="form-control " id="city">
	</div>
</div>

			<div class="form-group">	
<label for="ntn" class="col-sm-3 col-md-3 col-lg-2 control-label ">NTN :</label>	<div class="col-sm-9 col-md-9 col-lg-10">
	<input type="text" name="ntn" value="<?php echo $row['ntn']; ?>" class="form-control " id="ntn">
	</div>
</div>

			<div class="form-group">	
<label for="gst" class="col-sm-3 col-md-3 col-lg-2 control-label ">GST :</label>	<div class="col-sm-9 col-md-9 col-lg-10">
	<input type="text" name="gst" value="<?php echo $row['gst']; ?>" class="form-control " id="gst">
	</div>
</div>

	<div class="form-group">	
<label for="comments" class="col-sm-3 col-md-3 col-lg-2 control-label ">Comments :</label>	<div class="col-sm-9 col-md-9 col-lg-10">
	<textarea name="comments" cols="17" rows="5" id="comments" class="form-control text-area"><?php echo $row['comments']; ?></textarea>
	</div>
</div>
 
	</div><!-- /col-md-12 -->
</div><!-- /row -->				
								<div class="form-group">	
					<label for="balance" class="col-sm-3 col-md-3 col-lg-2 control-label ">Opening balance :</label>					<div class="col-sm-9 col-md-9 col-lg-10">
						<input type="text" name="balance" value="<?php echo $row['open_balance']; ?>" id="balance" class="form-control balance">
						</div>
					</div>

				<div class="form-group">	
					<label for="company_name" class="col-sm-3 col-md-3 col-lg-2 control-label ">Company Name :</label>					<div class="col-sm-9 col-md-9 col-lg-10">
						<input type="text" name="company_name" value="<?php echo $row['company_name']; ?>" id="company_name" class="company_names form-control">
						</div>
					</div>

					<div class="form-group">	
						<label for="account_number" class="col-sm-3 col-md-3 col-lg-2 control-label ">Account # :</label>						<div class="col-sm-9 col-md-9 col-lg-10">
							<input type="text" name="account_number" value="<?php echo $row['account_no']; ?>" id="account_number" class="company_names form-control">
							</div>
						</div>

						<div class="form-actions pull-right">
														
							<input type="submit" name="submitf" value="Submit" id="submitf" class=" submit_button btn btn-primary">
						</div>
					</div>
				</div>
										</form>
			</div>
		</div><!-- /row -->


	

			
<?php include 'dfooter.php'; ?>