<?php include('../config/setup.php'); ?>
<?php 
session_start();
    date_default_timezone_set('Asia/Kolkata');
    // order_master approve update query starts
    if(isset($_POST['approve'])){
        $pay_id = $_POST['pay_id'];
        $amount = $_POST['amount'];
        $vendor_id = $_POST['vendor_id'];
        $transac_id = $_POST['transac_id'];
        $payout_on = $_POST['payout_on'];
        $trans_img = $_FILES['trans_img']['name'];
       
        // $paid_date = date('Y-m-d h-i-s');
        // $total = $add_on + $order_price ;
        // $subsql = "SELECT * FROM ewallet_master WHERE vendor_id = '$vendor_id' " ;
        // $result = $config->query($subsql);
        // $subrow = $result->fetch_assoc();
        // $wallet = $subrow['total_amount'] + $total;
        
        $sql = "UPDATE v_payout_request SET payout_status = 3, transac_id = '$transac_id', payout_on = '$payout_on' ,payout_attachment = '$trans_img' WHERE payout_request_id = '$pay_id' " ;
        if($config->query($sql) === TRUE){
            move_uploaded_file($_FILES['trans_img']['tmp_name'],'../../assets/img/payout/'.$trans_img);
            $_SESSION['status'] = "Success";
		    $_SESSION['alert'] = "success";
        }else {
            $_SESSION['status'] = "Failed";
		    $_SESSION['alert'] = "error";
        }
    }
    // order_master approve update query ends
    
    // order_master reject update query starts
    if(isset($_POST['reject'])){
        $pay_id = $_POST['pay_id'];

        $sql = "UPDATE v_payout_request SET payout_status = 0 WHERE payout_request_id = '$pay_id' " ;
        if($config->query($sql) === TRUE){
            $_SESSION['status'] = "Request Rejected";
		    $_SESSION['alert'] = "success";
        } else {
            $_SESSION['status'] = "Failed";
		    $_SESSION['alert'] = "error";
        }
    }
    // order_master reject update query ends


?>

<?php include "head.php" ?>
	<div class="wrapper">
		<div class="main-header">
			<!-- Logo Header -->
			<?php include('logo.php'); ?>
			<!-- End Logo Header -->

			<!-- Navbar Header -->
			<?php include('topbar.php'); ?>
			<!-- End Navbar -->
		</div>
		<!-- Sidebar -->
		<?php include('sidebar.php'); ?>


		<div class="main-panel">
			<div class="content">
				<div class="page-inner">
					<div class="page-header d-flex justify-content-between">
						<h4 class="page-title">Payout Request</h4>
						<div class="d-flex justify-content-between" style="column-gap:10px;">
						    <?php
						        $req_sql = $config->query("SELECT count(payout_request_id) as total FROM v_payout_request WHERE payout_status = 1 ") ;
						        $req = $req_sql->fetch_assoc();
						        $requested = $req['total'];
						    ?>
						    <a href="payout_history.php" class="btn btn-info text-white">Request <span class="badge text-white"><?= $requested; ?></span></a>
						    <?php
						        $app_sql = $config->query("SELECT count(payout_request_id) as total FROM v_payout_request WHERE payout_status = 2 ") ;
						        $app = $app_sql->fetch_assoc();
						        $approved = $app['total'];
						    ?>
						    <a href="payout_processing.php" class="btn btn-success text-white">Processing <span class="badge text-white"><?= $approved; ?></span></a>
						    <?php
						        $rej_sql = $config->query("SELECT count(payout_request_id) as total FROM v_payout_request WHERE payout_status = 3 ") ;
						        $rej = $rej_sql->fetch_assoc();
						        $rejected = $rej['total'];
						    ?>
						    <a href="payout-done.php" class="btn btn-primary text-white">Done <span class="badge text-white"><?= $rejected; ?></span></a>
						</div>
					</div>
					<div class="row">
						<div class="col-md-12">
							<div class="card">
								<div class="card-body">
									<div class="table-responsive">
										<table id="basic-datatables" class="display table table-striped table-hover">
											<thead>
												<tr>
													<th>S.No</th>
													<th>Customer Name</th>
													<th>Contact No.</th>
													<th>Email Id</th>
													<!--<th>Payout Through</th>-->
													<th>Request Amount </th>
													<th>Request On</th>
													<!--<th>Transferred Attachment</th>-->
												    <th>Status</th>
													<th>Action</th>
												</tr>
											</thead>
											<tbody>
											    <?php
											        $sno = 1;
											        $sql = "SELECT * FROM v_payout_request WHERE payout_status = 2 ORDER BY payout_request_id DESC " ;
											        $res = $config->query($sql);
											        while($row = $res->fetch_assoc()){
											            $status = $row['payout_status'];
											    ?>
											    <tr>
											        <td><?= $sno ?></td>
											        <td><?= $row['payout_customer_name'] ?></td>
											        <td><?= $row['payout_customer_mobile'] ?></td>
											        <td><?= $row['payout_customer_email'] ?></td>
											        <td><?= $row['payout_request_amount'] ?></td>
											        <td><?= date('d-m-Y',strtotime($row['payout_request_on'])) ?></td>
											        <td>
											        <?php 
                								        if($status == 1){
                								            $cur_stauts = "Requested";
                								        }elseif($status == 2){
                								            $cur_stauts = "Processing";
                								        }elseif($status == 3){
                								            $cur_stauts = "done";
                								        }
                								    ?>
											            <a class="btn btn-warning text-white" href="javascript:void(0);"><?= $cur_stauts; ?></a>
											        </td>
											        <?php
    											     //   $sub_sql = "SELECT * FROM sub_category WHERE Sub_Category_id = '$sub_id' AND Sub_Category_Status = 1 " ;
    											     //   $sub_res = $config->query($sub_sql);
    											     //   $sub = $sub_res->fetch_assoc();
											        ?>
											        <td><a class="btn btn-primary text-white" data-target="#payout<?= $sno; ?>" data-toggle="modal"><i class="fas fa-pencil-alt"></i>Action</a></td>
											    </tr>
											    
											    <!-- Modal -->
                                    			<div class="modal fade" id="payout<?= $sno; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                    				<div class="modal-dialog" role="document">
                                    					<div class="modal-content">
                                    						<div class="modal-header bg-info">
                                    							<h5 class="modal-title" id="exampleModalLabel">Payment</h5>
                                    							<button type="button" class="close text-white" data-dismiss="modal" aria-label="Close">
                                    								<span aria-hidden="true">&times;</span>
                                    							</button>
                                    						</div>
                                    						<div class="modal-body">
                                    							<form method="post" enctype="multipart/form-data">
                                    								<div class="form-group">
                                    									<label for="payout_status">Current Status</label>
                                    									<input type="text" class="form-control" id="payout_status" name="payout_status" value="<?= $cur_stauts; ?>" readonly >
                                    									<input type="hidden"  id="pay_id" name="pay_id" value="<?= $row['payout_request_id']; ?>" >
                                    									<input type="hidden"  id="vendor_id" name="vendor_id" value="<?= $row['payout_customer_id']; ?>" >
                                    								</div>
                                    								<div class="form-group">
                                    									<label for="amount">Amount</label>
                                    									<input type="text" class="form-control" id="amount" name="amount" value="<?= $row['payout_request_amount']; ?>" readonly>
                                    								</div>
                                    								<div class="form-group">
                                    									<label for="transac_id">Transaction Id</label>
                                    									<input type="text" class="form-control" id="transac_id" name="transac_id"  >
                                    								</div>
                                    								<div class="form-group">
                                    									<label for="payout_on">Transaction Date</label>
                                    									<input type="datetime-local" class="form-control" id="payout_on" name="payout_on" >
                                    								</div>
                                    								<div class="form-group">
                                    									<label for="trans_img">Payout Attachment</label>
                                    									<input type="file" class="form-control" id="trans_img" name="trans_img" >
                                    								</div>
                                    								<!--<div class="form-group">-->
                                    								<!--	<label for="payment_mode">Payment Mode</label>-->
                                    								<!--	<select class="form-control" name="payment_mode" id="payment_mode">-->
                                    								<!--	    <option>-- Select --</option>-->
                                    								<!--	    <option value="1">Offline</option>-->
                                    								<!--	    <option value="2">Online</option>-->
                                    								<!--	</select>-->
                                    								<!--</div>-->
                                    								<div class="form-group d-flex" style="column-gap:10px;">
                                    									<button class="btn btn-success btn-lg btn-block" type="submit" name="approve"><i class="fas fa-check-circle"></i> Submit</button>
																		<button class="btn btn-danger btn-lg btn-block" type="submit" name="reject"><i class="fas fa-times-circle"></i> Cancel</button>                                    									
                                    								</div>
                                    							</form>
                                    						</div>
                                    					</div>
                                    				</div>
                                    			</div>
											    <?php $sno++; } ?>
											</tbody>
										</table>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			
			<!-- Modal -->


			
			<?php include('footer.php'); ?>
		</div>
		<!-- End Custom template -->
	</div>
	<!--   Core JS Files   -->
	<style>
	    .badge{
            border-radius: 50px;
            margin-left: auto;
            line-height: 1;
            padding: 6px 10px;
            vertical-align: middle;
            font-weight: 800;
            font-size: 15px;
            border: unset; 
        }
        .btn-block+.btn-block{
            margin-top: unset;
        }
	</style>
	<script src="../assets/js/core/jquery.3.2.1.min.js"></script>
	<script src="../assets/js/core/popper.min.js"></script>
	<script src="../assets/js/core/bootstrap.min.js"></script>
	<!-- jQuery UI -->
	<script src="../assets/js/plugin/jquery-ui-1.12.1.custom/jquery-ui.min.js"></script>
	<script src="../assets/js/plugin/jquery-ui-touch-punch/jquery.ui.touch-punch.min.js"></script>

	<!-- jQuery Scrollbar -->
	<script src="../assets/js/plugin/jquery-scrollbar/jquery.scrollbar.min.js"></script>
	<!-- Datatables -->
	<script src="../assets/js/plugin/datatables/datatables.min.js"></script>
	<!-- Atlantis JS -->
	<script src="../assets/js/atlantis.min.js"></script>
	<!-- Atlantis DEMO methods, don't include it in your project! -->
	<script src="../assets/js/setting-demo2.js"></script>
	<script>
		$(document).ready(function() {
			$('#basic-datatables').DataTable({});
		});
	</script>
</body>

</html>