<?php 
require_once('includes/check-login.php');
require_once('includes/config.php'); 
require_once('email.php');
$tablename="save_order";	
$viewurl="online-payment.php";
$vendor_id=isset($_GET['vendor_id']) ? $_GET['vendor_id'] :'';
if($_SESSION['role_id']=='1'){
$result=$con->query("SELECT save_order.vendor_id,SUM(save_order.grand_total)-SUM(save_order.commission) as total,save_order.payment_mode
FROM save_order WHERE save_order.payment_mode='basispay' and save_order.job_status='2' and vendor_id='$vendor_id' GROUP BY save_order.vendor_id");
}else{
header("Location:".$base_url.$viewurl."");
}
if(!is_numeric($vendor_id)){
 header("Location:".$base_url.$viewurl."");   
}
$row=mysqli_fetch_array($result);
$grand_total=$row['total'];
$result_child=$con->query("SELECT SUM(vendor_payment.amount) as amount FROM vendor_payment WHERE vendor_payment.vendor_id='$vendor_id' GROUP BY vendor_payment.vendor_id");
$venrow=mysqli_fetch_array($result_child);
$amount=$venrow['amount'];
if(isset($_POST['submit'])){
    $type=$_POST['type'];
    $description=$_POST['description'];
    $amount=$_POST['amount'];
    $created=date("Y-m-d H:i:s");
    $getvendor=$con->query("SELECT * FROM user_details WHERE user_id='$vendor_id'");
    $getrow=mysqli_fetch_array($getvendor);
    if($type=="wallet"){
    $wallet=mysqli_query( $con,"insert into wallet SET user_id='".$vendor_id."',credit='".$amount."',description='$description',created='".$created."'");
    $html=' <h1>Hi '.$getrow['name'].',</h1> 
        <p>Your payment amount <b>'.$amount.'</b> has been transfered to your wallet.</p>';
     sendemail($getrow['email'],"Payment Transaction",$html);
    }else{
      $html=' <h1>Hi '.$getrow['name'].',</h1> 
        <p>Your payment amount <b>'.$amount.'</b> has been transfered to your bank as soon as possible.</p>';
    sendemail($getrow['email'],"Payment Transaction",$html);  
    }
    $qry=mysqli_query($con,"insert into vendor_payment SET vendor_id='".$vendor_id."',amount='".$amount."',type='".$type."',description='$description',created='".$created."'");
    if( $qry ) {				
			$_SESSION['vensucc_msg'] = 'Payment successfully transfered to vendor';
	}else{				
			$_SESSION['venerr_msg'] = 'Error occured';
	}
	header("Location:".$base_url.$viewurl."");
}
$last_result=$con->query("SELECT * FROM vendor_payment WHERE vendor_payment.vendor_id='$vendor_id' order by id desc limit 0,5");
require_once('includes/header.php'); 
require_once('includes/menu.php'); 
?>
<style>.text-primary {color: #337ab7 !important;font-weight: bold;font-size: 18px;}.text-success {color: #28a745!important;font-weight: bold;font-size: 18px;}.text-danger {color: #dc3545!important;font-weight: bold;font-size: 18px;}</style>
<!-- Datatable CSS -->
<link href='data_tables/datatables.min.css' rel='stylesheet' type='text/css' />
        <!-- Content Wrapper. Contains page content -->
        <div class="content-wrapper">
            <!-- Content Header (Page header) -->
            <section class="content-header">                                
                    <ol class="breadcrumb">
                        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>                    
                        <li class="active"><a href="job-request.php">Vendor ID  #<?php echo $vendor_id;?></a></li>           
                    </ol>                
            </section>
            <!-- Main content -->
            <section class="content">                
                <!-- Default box -->
                <div class="box box-form">
    <div class="box-body">
    <div class="row">
<div class="col-xs-12">
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand text-success" href="#"><b>Vendor ID # <?php echo $vendor_id;?></b></a>
    </div>
  </div>
</nav>
<hr>
<?php 
if(mysqli_num_rows($result)>0){?>
 <form method="post">
    <div class="form-group row">
      <div class="col-xs-3">
        <select class="form-control" name="type" required>
            <option value="" disabled selected>Choose Type</option>
            <option value="wallet">Wallet</option>
            <option value="bank">Bank</option>
        </select>
      </div>
      <div class="col-xs-3">
        <textarea class="form-control" name="description" required placeholder="Note"></textarea>
      </div>
      <div class="col-xs-3">
        <input type="number" class="form-control" name="amount" id="amount" value="" required>
      </div>
      <div class="col-xs-2">
        <input class="form-control btn btn-success" name="submit" type="submit" value="Submit">
      </div>
    </div>
  </form>
<?php } else {?>
<h3>Vendor Not Available!</h3>
<?php }?>
 <hr> 
</div>
</div>
    
    <div class="row">
    	<div class="col-md-12">
    		<div class="panel panel-default">
    			<div class="panel-heading">
    				<h3 class="panel-title"><strong>Last 5 Transactions summary</strong></h3>
    			</div>
    			<div class="panel-body">
    				<div class="table-responsive">
    					<table class="table table-condensed">
    						<thead>
                                <tr>
        							<td><strong>Date</strong></td>
        							<td class="text-center"><strong>Vendor ID</strong></td>
        							<td class="text-center"><strong>Type</strong></td>
        							<td class="text-right"><strong>Amount</strong></td>
                                </tr>
    						</thead>
    						<tbody>
    							<!-- foreach ($order->lineItems as $line) or some such thing here -->
    							<?php foreach($last_result as $row1) {?>
    							<tr>
    								<td><?php echo $row1['created'];?></td>
    								<td class="text-center"><?php echo $row1['vendor_id'];?></td>
    								<td class="text-center"><?php echo $row1['type'];?></td>
    								<td class="text-right"><?php echo $row1['amount'];?></td>
    							</tr>
    							<?php }?>
    						</tbody>
    					</table>
    				</div>
    					<?php $url = "online-payment.php";
  echo "<a href='$url' class='btn btn-primary'>Back</a>";?>
    			</div>
    		</div>
    	</div>
    </div>
</div>
                    <!-- /.box-footer-->                    
                </div>
                <!-- /.box -->

            </section>
            <!-- /.content -->                                        
        </div>
        <!-- /.content-wrapper -->        
<script src="js/common.js"></script>  
<script src="js/datatables.js"></script>  
<script>
$("#amount").bind('keyup mouseup', function () {
    var total=parseInt("<?php echo ($grand_total-$amount);?>");
    var amount=parseInt($(this).val());
    if(amount>total){
     $(this).val(total);   
    }
});
</script>
<?php require_once('includes/footer.php'); ?>