<?php include('../config/setup.php'); ?>
<?php 
session_start();
    date_default_timezone_set('Asia/Kolkata');
    // order_master approve update query starts
    if(isset($_POST['approve'])){
        $order_id = $_POST['order_id'];
        $order_price = $_POST['order_price'];
        $vendor_id = $_POST['vendor_id'];
        $add_on = $_POST['add_on'];
        $payment_mode = "offline";
        $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;
        
        $sub_sql = "UPDATE ewallet_master SET total_amount = '$wallet'  WHERE vendor_id = '$vendor_id' " ;
        if($config->query($sub_sql) === TRUE){
            $sql = "UPDATE order_master SET order_status = 2 ,payment_mode = '$payment_mode' , add_on = '$add_on',paid_status = 1 ,paid_date = '$paid_date' WHERE order_id = '$order_id' " ;
            if($config->query($sql) === TRUE){
                $_SESSION['status'] = "Approved Successfully";
    		    $_SESSION['alert'] = "success";
            }else {
                $_SESSION['status'] = "Approval Failed";
    		    $_SESSION['alert'] = "error";
            }
        }

    }
    // order_master approve update query ends
    
    // order_master reject update query starts
    if(isset($_POST['reject'])){
        $order_id = $_POST['order_id'];

        $sql = "UPDATE order_master SET order_status = 0 WHERE order_id = '$order_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">Order Request</h4>
						<div class="d-flex justify-content-between" style="column-gap:10px;">
						    <?php
						        $req_sql = $config->query("SELECT count(order_id) as total FROM order_master WHERE order_status = 1 ") ;
						        $req = $req_sql->fetch_assoc();
						        $requested = $req['total'];
						    ?>
						    <a href="order_request.php" class="btn btn-info text-white">Request <span class="badge text-white"><?= $requested; ?></span></a>
						    <?php
						        $app_sql = $config->query("SELECT count(order_id) as total FROM order_master WHERE order_status = 2 ") ;
						        $app = $app_sql->fetch_assoc();
						        $approved = $app['total'];
						    ?>
						    <a href="order_approve.php" class="btn btn-success text-white">Approval <span class="badge text-white"><?= $approved; ?></span></a>
						    <?php
						        $rej_sql = $config->query("SELECT count(order_id) as total FROM order_master WHERE order_status = 0 ") ;
						        $rej = $rej_sql->fetch_assoc();
						        $rejected = $rej['total'];
						    ?>
						    <a href="order_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>Customer Name</th>
													<th>Mobile Number</th>
													<th>Status</th>
													<th>Sub Category</th>
													<th>Duration</th>
													<th>Cost</th>
													<th>%</th>
													<th>Package</th>
												</tr>
											</thead>
											<tbody>
											    <?php
											        $sno = 1;
											        $sql = "SELECT * FROM order_master WHERE order_status = 1 ORDER BY order_id DESC " ;
											        $res = $config->query($sql);
											        while($row = $res->fetch_assoc()){
											            $cus_id = $row['customer_id'];
											            $pack_id = $row['order_package'];
											            $sub_id = $row['order_sub_category'];
											            $status = $row['order_status'];
											    ?>
											    <tr>
											        <td><?= $sno ?></td>
											        <td><?= $row['ticket'] ?></td>
											        <td><?= $row['transaction_id'] ?></td>
										            <?php
    											        $cus_sql = "SELECT * FROM master_login WHERE Master_Id = '$cus_id' AND Master_Type = 2 " ;
    											        $cus_res = $config->query($cus_sql);
    											        $cus = $cus_res->fetch_assoc();
											        ?>
											        <td><?= $cus['Master_Name'] ?></td>
											        <td><?= $cus['Master_phone'] ?></td>
											        <?php
    											        $pack_sql = "SELECT * FROM package_master WHERE pack_id = '$pack_id' AND pack_status = 1 " ;
    											        $pack_res = $config->query($pack_sql);
    											        $pack = $pack_res->fetch_assoc();
											        ?>
											        <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="#order<?= $sno; ?>" data-toggle="modal"><?= $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><?= $sub['Sub_Category_Name'] ?></td>
											        <td><?= $pack['pack_duration'] ?></td>
											        <td><?= $pack['pack_cost'] ?></td>
											        <td><?= $pack['pack_commission'] ?></td>
											        <td><?= $pack['pack_name'] ?></td>
											    </tr>
											    
											    <!-- Modal -->
                                    			<div class="modal fade" id="order<?= $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="order_status">Current Status</label>
                                    									<input type="text" class="form-control" id="order_status" name="order_status" value="<?= $cur_stauts; ?>" readonly >
                                    									<input type="hidden"  id="order_id" name="order_id" value="<?= $row['order_id']; ?>" >
                                    									<input type="hidden"  id="vendor_id" name="vendor_id" value="<?= $cus_id; ?>" >
                                    								</div>
                                    								<div class="form-group">
                                    									<label for="order_status">Amount</label>
                                    									<input type="text" class="form-control" id="order_price" name="order_price" value="<?= $row['order_price']; ?>" readonly>
                                    								</div>
                                    								<div class="form-group">
                                    									<label for="add_on">Add On</label>
                                    									<input type="number" class="form-control" id="add_on" name="add_on" >
                                    								</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> Approve</button>
																		<button class="btn btn-danger btn-lg btn-block" type="submit" name="reject"><i class="fas fa-times-circle"></i> Reject</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>