<?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>Paid Amount </th>
													<th>Transaction ID</th>
													<th>Transaction On</th>
													<th>Attachment</th>
												    <th>Status</th>
												</tr>
											</thead>
											<tbody>
											    <?php
											        $sno = 1;
											        $sql = "SELECT * FROM v_payout_request WHERE payout_status = 3 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><?= $row['transac_id'] ?></td>
											        <td><?= date('d-m-Y',strtotime($row['payout_on'])) ?></td>
											        <td><a class="btn btn-primary" href="../../assets/img/payout/<?= $row['payout_attachment'] ?>" target="_blank"><i class="fas fa-eye"></i> View</a></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-success text-white" href="javascript:void(0);"><?= $cur_stauts; ?></a>
											        </td>
											    </tr>
											    
											    <!-- Modal -->
											    <?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>