<?php include('../config/setup.php'); ?>
<?php 
session_start();
    date_default_timezone_set('Asia/Kolkata');
    // event approve update query starts
    if(isset($_POST['approve'])){
        $event_id = $_POST['event_id'];
        $com_type = $_POST['com_type'];
        $com_value = $_POST['com_value'];

        $sql = "UPDATE event_post SET comm_type = '$com_type', pack_comm = '$com_value', e_status = 2 WHERE event_id = '$event_id' " ;
        if($config->query($sql) === TRUE){
            $pay_sql = "UPDATE payment_request SET pay_status = 2 WHERE pay_event_id = '$event_id' " ;
            if($config->query($pay_sql) === TRUE){
                $_SESSION['status'] = "Approved Successfully";
    		    $_SESSION['alert'] = "success";
            }
        } else {
            $_SESSION['status'] = "Approval Failed";
		    $_SESSION['alert'] = "error";
        }
    }
    // event approve update query ends
    
    // event reject update query starts
    if(isset($_POST['reject'])){
        $event_id = $_POST['event_id'];
        $pay_sql = $config->query("SELECT * FROM payment_request WHERE pay_event_id = '$event_id' ") ;
        $pay_res = $pay_sql->fetch_assoc() ;
        $vendor_id = $pay_res['pay_vendor_id'] ;
        $wallet = $pay_res['wallet'] ;
        $amount = $pay_res['amount'] ;
        $wallet = $wallet + $amount ;
        $amount = 0 ;
        $ewal_sql = $config->query("UPDATE ewallet_master SET total_amount = '$wallet' WHERE vendor_id = '$vendor_id' ") ;
        $sql = "UPDATE event_post SET e_status = 0 WHERE event_id = '$event_id' " ;
        if($config->query($sql) === TRUE){
            
            $pay_sub = "UPDATE payment_request SET amount = '$amount' ,wallet = '$wallet', pay_status = 0 WHERE pay_event_id = '$event_id' " ;
            if($config->query($pay_sub) === TRUE){
                $_SESSION['status'] = "Request Rejected";
		        $_SESSION['alert'] = "success";
            }
        } else {
            $_SESSION['status'] = "Failed";
		    $_SESSION['alert'] = "error";
        }
    }
    // event 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">Event Request</h4>
						<div class="d-flex justify-content-between" style="column-gap:10px;">
						    <?php
						        $req_sql = $config->query("SELECT count(event_id) as total FROM event_post WHERE e_status = 1 ") ;
						        $req = $req_sql->fetch_assoc();
						        $requested = $req['total'];
						    ?>
						    <a href="listing.php" class="btn btn-info text-white">Request <span class="badge text-white"><?= $requested; ?></span></a>
						    <?php
						        $app_sql = $config->query("SELECT count(event_id) as total FROM event_post WHERE e_status = 2 ") ;
						        $app = $app_sql->fetch_assoc();
						        $approved = $app['total'];
						    ?>
						    <a href="listing_approve.php" class="btn btn-success text-white">Approval <span class="badge text-white"><?= $approved; ?></span></a>
						    <?php
						        $rej_sql = $config->query("SELECT count(event_id) as total FROM event_post WHERE e_status = 0 ") ;
						        $rej = $rej_sql->fetch_assoc();
						        $rejected = $rej['total'];
						    ?>
						    <a href="listing_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>Company Name</th>
													<th>Category</th>
													<th>Cost</th>
													<th>Last Date</th>
													<th>Event Date</th>
													<th>View</th>
													<th>Status</th>
												</tr>
											</thead>
											<tbody>
											    <?php
											        $sno = 1;
											        $sql = "SELECT * FROM event_post WHERE e_status = 1 ORDER BY event_id DESC" ;
											        $res = $config->query($sql);
											        while($row = $res->fetch_assoc()){
											            $cus_id = $row['vendor_id'];
											            $sub_id = $row['sub_cate_id'];
											            $status = $row['e_status'];
											            $event_id = $row['event_id'];
											    ?>
											    <tr>
											        <td><?= $sno; ?></td>
											        <?php
    											        $cus_sql = "SELECT * FROM v_profile WHERE v_id = '$cus_id' " ;
    											        $cus_res = $config->query($cus_sql);
    											        $cus = $cus_res->fetch_assoc();
											        ?>
											        <td><?= $cus['company'] ?></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();

                                                        $sql="SELECT SUM(entry_price) as entryfees FROM entry_master WHERE event_id='$event_id' ";

                                                        $entry=$config->query($sql);
                                                        $entry=$entry->fetch_assoc();
                                                        
											        ?>
											        <td><?= $sub['Sub_Category_Name'] ?></td>
											        <td><?= $entry['entryfees'] ?></td>
											        <td><?= date('d-m-Y',strtotime($row['l_date'])) ?></td>
											        <td><?= date('d-m-Y',strtotime($row['e_date'])) ?></td>
											        <td><a class="btn btn-primary text-white" data-target="#view<?= $sno; ?>" data-toggle="modal"><i class="fas fa-eye"></i> View</a></td>
											        <td>
											        <?php 
                								        if($status == 1){
                								            $cur_stauts = "Requested";
                								            $btn_color = "secondary" ;
                								        }else if($status == 2){
                								            $cur_stauts = "Approved";
                								            $btn_color = "success" ;
                								        }else if($status == 0){
                								            $cur_stauts = "Rejected";
                								            $btn_color = "danger" ;
                								        }
                								    ?>
											            <a class="btn btn-<?= $btn_color; ?> text-white" data-target="#event<?= $sno; ?>" data-toggle="modal"><?= $cur_stauts; ?></a>
											        </td>
											    </tr>
											    <!-- Modal -->
                                    			<div class="modal fade" id="view<?= $sno; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                    				<div class="modal-dialog modal-lg" role="document">
                                    					<div class="modal-content">
                                    						<div class="modal-header">
                                    							<h5 class="modal-title" id="exampleModalLabel">Event Details</h5>
                                    							<button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    								<span aria-hidden="true">&times;</span>
                                    							</button>
                                    						</div>
                                    						<div class="modal-body">
                                    						    <div class="row px-3">
                                    						        <div class="col-md-6">
                                    									<h3>Event Name</h3>
                                    									<p><?= $row['e_title']; ?></p>
                                    									<?php if($row['sub_title'] != ''){ ?>
                                    									<h3>Sub Title</h3>
                                    									<p><?= $row['sub_title']; ?></p>
                                    									<?php } ?>
                                    									<h3>Category</h3>
                                    									<p><?= $sub['Sub_Category_Name'] ?></p>
                                    									<div class="row">
                                    									    <div class="col-md-4">
                                        									    <h3>Type</h3>
                                        									    <p><?= $row['e_type']; ?></p>
                                    									    </div>
                                    									    <div class="col-md-4">
                                        									    <h3>Enrty</h3>
                                        									    <p class="text-capitalize"><?= $row['entry']; ?></p>
                                    									    </div>
                                    									    <div class="col-md-6">
                                        									    <h3>Enrty Type</h3>
                                        									    <?php 
                                        									        $entry_sql = $config->query("SELECT * FROM entry_master WHERE event_id = '$event_id' ");
                                        									        foreach($entry_sql as $entry_res){
                                        									    ?>
                                        									    <p class="text-primary"><?= $entry_res['entry_type'] ?></p>
                                        									    <?php } ?>
                                    									    </div>
                                    									    <div class="col-md-6">
                                        									    <h3>Enrty Fees</h3>
                                        									    <?php 
                                        									        foreach($entry_sql as $entry_res){
                                        									    ?>
                                        									    <p class="text-primary"><?= $entry_res['entry_price'] ?></p>
                                        									    <?php } ?>
                                    									    </div>
                                    									</div>
                                    						        </div>
                                    						        <div class="col-md-6">
                                    						            <h3>Image</h3>
                                    						            <img class="img-fluid" src="../../assets/img/event/<?= $row['e_image']; ?>">
                                    						        </div>
                                    						        <?php if($row['sub_title'] != ''){ ?>
                                    						        <div class="col-md-12">
                                    									<h3>Description</h3>
                            									        <p class="text-justify"><?= $row['e_desc']; ?></p>
                                    						        </div>
                                    						        <?php } ?>
                                    						        <div class="col-md-6">
                                    									<h3>Address</h3>
                                    									<p><?= $row['address']; ?></p>
                                    									<h3>Area</h3>
                                    									<p><?= $row['area']; ?></p>
                                    									<h3>City</h3>
                                    									<p><?= $row['city']; ?></p>
                                    						        </div>
                                    						        <div class="col-md-6">
                                    									<h3>Event Start Date : <span class="badge bg-success text-white"><?= date('d-m-Y',strtotime($row['e_date'])); ?></span></h3>
                                    									<h3>Event End Date : <span class="badge bg-info text-white"><?= date('d-m-Y',strtotime($row['end_date'])); ?></span></h3>
                                    									<h3>Last Enrollment Date : <span class="badge bg-danger text-white"><?= date('d-m-Y',strtotime($row['l_date'])); ?></span></h3>
                                    									<?php if($row['e_broucher'] != ''){ ?>
                                    									<h3>Broucher
                                        									<a class="btn btn-warning" href="../../assets/img/broucher/<?= $row['e_broucher']; ?>" target="_blank">
                                        									    <i class="fas fa-eye"></i> View
                                        									</a>
                                    									</h3>
                                    									<?php } ?>
                                    						        </div>
                                    						    </div>

                                    						</div>
                                    						<div class="modal-footer">
                                        						<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                    						</div>
                                    					</div>
                                    				</div>
                                    			</div>

											    <!-- Modal -->
                                    			<div class="modal fade" id="event<?= $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="e_status" name="e_status" value="<?= $cur_stauts; ?>" readonly >
                                    									<input type="hidden"  id="event_id" name="event_id" value="<?= $row['event_id']; ?>" >
                                    								</div>
                                    								<div class="form-group">
                                    									<label for="order_status">Commission Type</label>
                                    									<select name="com_type" id="com_type" class="form-control" >
                                    									    <option value="">--Select--</option>
                                    									    <option value="Amount">Amount</option>
                                    									    <option value="Percentage">Percentage</option>
                                    									</select>
                                    								</div>
                                    								<div class="form-group">
                                    									<label for="order_status">Commission</label>
                                    									<select name="com_value" id="com_value" class="form-control" >
                                    									    <option value="">--Select--</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>
	<script>
		$('#com_type').change(()=>{
		    let com_type = $('#com_type').val();
		    $.ajax({
                url: "func.php",
                method: "POST",
                data: { com_type: com_type },
                success: function(data){
                    $("#com_value").html(data)
                }
            })
		});
	</script>
</body>

</html>