<?php include("sass/icon_for_datepicker_and_datatable.php");?>
        
        <?php
$errMsg=""; 
if(isset($_POST['submit'])) // To check 'submmited or not' by if(isset($_POST['submit'])) 
{
	$loan_bank_name_id = $_POST['loan_bank_name_id'];
	$paid = $_POST['paid'];
	$date = $_POST['date'];
	
		if(!empty($date)) {
		$date = date("Y-m-d", strtotime($date));
		}
		else {
		$date = "";
		}
	
	if(empty($paid)) $errMsg="write the quantity";
	if(empty($errMsg))
	{
		
		
		mysqli_query($connection,"INSERT INTO loan_list (loan_bank_id, paid, date) VALUES ('$loan_bank_name_id','$paid','$date')")
		or die(mysqli_error());
		
		mysqli_query($connection,"INSERT INTO transaction (date, ledger, credit, loan_bank_name_id, payment_type) VALUES ('$date','paid loan','$paid','$loan_bank_name_id','cash')")
		or die(mysqli_error()); 
		
		$check=mysqli_query($connection,"select * from loan_bank_name where loan_bank_name_id='$loan_bank_name_id' and total_loan >= '$paid'");
		$checkrows=mysqli_num_rows($check);
		if($checkrows>0){
		
		mysqli_query($connection,"UPDATE loan_bank_name SET total_loan = total_loan - $paid WHERE loan_bank_name_id = '$loan_bank_name_id'")
				or die(mysqli_error());
		}
		
		else{ 
		
		$result_bank_name = mysqli_query($connection,"SELECT * FROM loan_bank_name WHERE loan_bank_name_id  = '$loan_bank_name_id'");
		$test_bank_name = mysqli_fetch_array($result_bank_name);
				
				$total_loan=$test_bank_name['total_loan'];
				
				$rest = $paid - $total_loan ;
				
				$minus = $paid - $rest ;
				
		mysqli_query($connection,"UPDATE loan_bank_name SET total_loan = total_loan - $minus WHERE loan_bank_name_id = '$loan_bank_name_id'")
				or die(mysqli_error());		
		
		
		 }
		
		
		mysqli_query($connection,"UPDATE cash SET amount = amount - $paid WHERE cash_id = 1")
				or die(mysqli_error());
		
		
		$errMsg="Loan has been inserted successfully";
	
}
}
?>

        
        
        <div class="wrapper row-offcanvas row-offcanvas-left">
            <!-- Left side column. contains the logo and sidebar -->
            <?php include("sass/sidebar.php");?>

            <!-- Right side column. Contains the navbar and content of the page -->
               <aside class="right-side">
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1>
                       <?php echo $company_name ; ?>
                        <small>Control panel</small>
                    </h1>
                    <ol class="breadcrumb">
                        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
                        <li class="active">Bank Setup</li>
                    </ol>
                </section>
                 <section class="content">
                 <!---Main Content--->
                 
                     <div class="row">
                     
                         <div class="col-md-8 col-md-offset-2">
                         
                       
                        <?php
							if (!empty($errMsg))
							{echo "<div class='alert alert-warning alert-dismissible' role='alert'>$errMsg";
							echo "<button type='button' class='close' data-dismiss='alert'><span aria-hidden='false'>&times;</span><span class='sr-only'>		Close</span></button>
                        </div>";}
                        ?>
                        
                         <div class="box box-primary">
                                <div class="box-header">
                                    <h3 class="box-title">Loan Pay</h3>
                                </div><!-- /.box-header -->
                                <!-- form start -->
                                <form role="form"  action="<?php echo "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" method="post">
                                    <div class="box-body">
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Bank Name</label>
                                            <select name="loan_bank_name_id" class="form-control">
												<?php  
                                                $query_bank = "select * from loan_bank_name";
                                                $result_bank = mysqli_query($connection,$query_bank);
                                                
                                               
                                                while($row_bank=mysqli_fetch_array($result_bank))
                                                {
                                                
                                                echo "<option value='" . $row_bank['loan_bank_name_id'] . "'>" . $row_bank['loan_bank_name'] . "</option>";}?> 
                                            
                                            </select>
                                        </div>
                                       
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Amount</label>
                                            <input type="text" class="form-control" name="paid" placeholder="write the quantity">
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Date</label>
                                             <input type="text" class="form-control datepicker" autocomplete="off" name="date" value="<?php echo date("d-m-Y");?>">
                                        </div>
                                        
                                    </div><!-- /.box-body -->

                                    <div class="box-footer">
                                        <button type="submit" name="submit" class="btn btn-primary">Submit</button>
                                    </div>
                                </form>
                            </div>
                         
                         </div>
                         
                         
                         <div class="col-md-12">
                         
                         
                         <div class="box">
                                <div class="box-header">
                                    <h3 class="box-title">Bank list with details</h3>
                                </div><!-- /.box-header -->
                                <div class="box-body table-responsive">
                                    <table id="example1" class="table table-bordered table-striped">
                                        <thead>
                                            <tr>
                                                <th>Bank Name</th>
                                                <th>Loan Amount</th>
                                                <th>Date</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            
												<?php
                                                
                                                $query = "SELECT * FROM loan_list where withdraw = '0'";
                                                $result = mysqli_query($connection,$query);
                                                
                                                $i=0;
                                                while($row=mysqli_fetch_array($result))
                                                {
                                                $i++;
												$loan_list_id = $row['loan_list_id'];
                                                $loan_bank_id = $row['loan_bank_id'];
												
												$result_bank_name = mysqli_query($connection,"SELECT loan_bank_name FROM loan_bank_name WHERE loan_bank_name_id  = '$loan_bank_id'");
		$test_bank_name = mysqli_fetch_array($result_bank_name);
				
				$loan_bank_name=$test_bank_name['loan_bank_name'];
												
												
                                                $paid = $row['paid'];
                                                $date = $row['date'];
												
                                                echo "<tr>";
                                                echo "<td>$loan_bank_name</td>";
                                                echo "<td>$paid</td>";
                                                echo "<td>$date</td>";
                                                echo "</tr>";
                                                }
                                                
                                                ?>
                                         
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <th>Bank Name</th>
                                                <th>Loan Amount</th>
                                                <th>Date</th>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div><!-- /.box-body -->
                            </div>
                         
                         </div>
                     
                     </div>
                 
                 
                 
                 
                 </section>
                </aside>
                <!-- /.right-side -->
        </div><!-- ./wrapper -->

        <!-- add new calendar event modal -->
 <script>
function EditProduct(id) {
    window.open('edit_bank.php?id='+id,'logos','width=500,height=500');}
</script>
  <!-- jQuery 2.0.2 -->
     <?php include("sass/footer_for_datepicker_and_datatable.php");?>
    </body>
</html>