<?php include('../config/setup.php'); ?>
<?php 
session_start();
    // order_master reject update query starts
    if(isset($_POST['reject'])){
        $buy_id = $_POST['buy_id'];

        $sql = "UPDATE buypack_master SET buy_status = 0 WHERE buy_id = '$buy_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">User Order Approve</h4>
						<div class="d-flex justify-content-between" style="column-gap:10px;">
						    <?php
						        $req_sql = $config->query("SELECT count(buy_id) as total FROM buypack_master WHERE buy_status = 1 ") ;
						        $req = $req_sql->fetch_assoc();
						        $requested = $req['total'];
						    ?>
						    <a href="user_request.php" class="btn btn-info text-white">Request <span class="badge text-white"><?= $requested; ?></span></a>
						    <?php
						        $app_sql = $config->query("SELECT count(buy_id) as total FROM buypack_master WHERE buy_status = 2 ") ;
						        $app = $app_sql->fetch_assoc();
						        $approved = $app['total'];
						    ?>
						    <a href="user_approve.php" class="btn btn-success text-white">Approval <span class="badge text-white"><?= $approved; ?></span></a>
						    <?php
						        $rej_sql = $config->query("SELECT count(buy_id) as total FROM buypack_master WHERE buy_status = 0 ") ;
						        $rej = $rej_sql->fetch_assoc();
						        $rejected = $rej['total'];
						    ?>
						    <a href="user_reject.php" class="btn btn-danger text-white">Rejected <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>Order Id</th>
													<th>Transaction</th>
													<th>User Name</th>
													<th>Mobile Number</th>
													<th>Amount</th>
													<th>Commission</th>
													<th>Created On</th>
													<th>Status</th>
												</tr>
											</thead>
											<tbody>
											    <?php
											        $sno = 1;
											        $sql = "SELECT * FROM buypack_master WHERE buy_status = 2 ORDER BY buy_id DESC " ;
											        $res = $config->query($sql);
											        while($row = $res->fetch_assoc()){
											            $cus_id = $row['user_id'];
											            $status = $row['buy_status'];
											            $pay_id = $row['buy_id'];
											    ?>
											    <tr>
											        <td><?= $sno ?></td>
											        <td><?= $row['buy_id'] ?></td>
											        <td><?= $row['transaction_id'] ?></td>
										            <?php
    											        $cus_sql = "SELECT * FROM master_login WHERE Master_Id = '$cus_id' AND Master_Type = 3 " ;
    											        $cus_res = $config->query($cus_sql);
    											        $cus = $cus_res->fetch_assoc();
											        ?>
											        <td><?= $cus['Master_Name'] ?></td>
											        <td><?= $cus['Master_phone'] ?></td>
											        <td><?= $row['buy_amount'] ?></td>
											        <?php $sql2 = "SELECT SUM(comm_amt) as commAmt FROM apply_request WHERE pay_id = $pay_id " ;
											        $res2 = $config->query($sql2); 
											           $res2=$res2->fetch_assoc();
											           // print_r($res2);
											           ?>

											        <td><?=$res2['commAmt']?></td>
											        <td><?=date('d-M-Y H:i a',strtotime($row['buy_on']))?></td>
											        <td>
											        <?php 
                								        if($status == 1){
                								            $cur_stauts = "Requested";
                								        }elseif($status == 2){
                								            $cur_stauts = "Approved";
                								        }else{
                								            $cur_stauts = "Rejected";
                								        }
                								    ?>
											            <a class="btn btn-secondary text-white" data-target="#buy<?= $sno; ?>" data-toggle="modal"><?= $cur_stauts; ?></a>
											        </td>
											    </tr>
											    
											    <!-- Modal -->
                                    			<div class="modal fade" id="buy<?= $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">Status Approval</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">
                                    								<div class="form-group">
                                    									<label for="buy_status">Current Status</label>
                                    									<input type="text" class="form-control" id="buy_status" name="buy_status" value="<?= $cur_stauts; ?>" readonly >
                                    									<input type="hidden"  id="buy_id" name="buy_id" value="<?= $row['buy_id']; ?>" >
                                    									<input type="hidden"  id="user_id" name="user_id" value="<?= $cus_id; ?>" >
                                    								</div>
                                    								<div class="form-group d-flex" style="column-gap:10px;">
																		<button class="btn btn-danger btn-lg btn-block" type="submit" name="reject"><i class="fas fa-trash-alt"></i> Delete</button>                                    									
                                    									<button class="btn btn-muted btn-lg btn-block" type="button" data-dismiss="modal"> Cancel</button>
                                    								</div>
                                    							</form>
                                    							
                                    						</div>
                                    					</div>
                                    				</div>
                                    			</div>
											    <?php $sno++; } ?>
											</tbody>
										</table>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>




			<?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>





