<?php  include "head.php" ; ?>
<?php
    session_start();
    date_default_timezone_set('Asia/Kolkata');
    if(isset($_SESSION['user_id']) && isset($_SESSION['user_name']) && $_SESSION['user_cate'] === "vendor"){
        $user_id = $_SESSION['user_id'] ;
        $username = $_SESSION['user_name'] ;
        $email = $_SESSION['user_email'] ;
        $phone = $_SESSION['user_phone'] ;
        $ticket = $_SESSION['last_ticket'] ;
        $company = $_SESSION['company'] ;
        $vendor_image = $_SESSION['vendor_image'] ;
    }else{
        header("Location:vendor_login.php") ;
    }
?>


<body>
	<!-- Main Wrapper -->

	<div class="main-wrapper">

		<!-- Header -->

		<?php include "header.php" ; ?>

		<!-- /Header -->
	<body>

		
			<!-- Breadcrumb -->
			<?php include "breadcrumb.php" ; ?>
			<!-- /Breadcrumb -->
			
			<!-- Page Content -->
			<div class="content">
				<div class="container-fluid">
                    <div class="row">
						<div class="col-md-5 col-lg-4 col-xl-3 theiaStickySidebar">
							
							<!-- Profile Sidebar -->
							<?php include "vendor_sidebar.php" ; ?>
							<!-- /Profile Sidebar -->
							
						</div>
						
					
						<div class="col-md-7 col-lg-8 col-xl-9">
							<div class="row">
								<div class="col-md-12">
									<div class="card dash-card">
										<div class="card-body">
											<div class="row">
												<div class="col-md-12 col-lg-4">
													<div class="dash-widget dct-border-rht">
														<div class="circle-bar circle-bar1">
															<div class="circle-graph1" data-percent="75">
																<img src="assets/img/icon-01.png" class="img-fluid" alt="patient">
															</div>
														</div>
														<div class="dash-widget-info">
															<h6>Cost</h6>
															<h3>
															<?php
                                    					        $total_amt = 0;
                                    							$up_sql = $config->query("SELECT * FROM event_post WHERE vendor_id = '$user_id' AND e_status = 2 ") ;
                                        					        while($up = $up_sql->fetch_assoc()){
																		$evt_id = $up['event_id'];
																		$app_sql = $config->query("SELECT *,sum(comm_amt) as total_comm_amt,sum(entry_fees) as total_entry_fees ,count(user_id) as tot FROM apply_request WHERE event_id = '$evt_id' ");
                                            					        $app = $app_sql->fetch_assoc();
																		if($up['entry']=='multi' || $up['entry']=='paid'){

																			$total_amt += ($app['total_entry_fees']-$app['total_comm_amt']);
																		}else{
                                            					        $total_amt = $total_amt + $up['entry_fees'];
																		}
                                        					        }
                                        					        echo $total_amt;
                                    				        ?>
                                    				        </h3>
														</div>
													</div>
												</div>
												
												<div class="col-md-12 col-lg-4">
													<div class="dash-widget dct-border-rht">
														<div class="circle-bar circle-bar2">
															<div class="circle-graph2" data-percent="65">
																<img src="assets/img/icon-02.png" class="img-fluid" alt="Patient">
															</div>
														</div>
														<div class="dash-widget-info">
															<h6>Commission</h6>
															<h3>
															<?php
                                    					        $total_com = 0;
                                    							$up_sql = $config->query("SELECT * FROM event_post WHERE vendor_id = '$user_id' AND e_status = 2 ") ;
                                        					        while($up = $up_sql->fetch_assoc()){
                                        					            $evt_id = $up['event_id'];
                                        					            $pack_id = $up['pack_id'];
                                            					        $pack_sql = $config->query("SELECT * FROM package_master WHERE pack_id = '$pack_id' AND pack_status = 1 ");
                                            					        $pack = $pack_sql->fetch_assoc();
																		$app_sql = $config->query("SELECT *,sum(comm_amt) as total_comm_amt,sum(entry_fees) as total_entry_fees ,count(user_id) as tot FROM apply_request WHERE event_id = '$evt_id' ");
                                            					        $app = $app_sql->fetch_assoc();
                                            					       
																		if($up['entry']=='multi' || $up['entry']=='paid'){

																			$total_com += ($app['total_comm_amt']);
																		}else{
																			$comm = ($app['tot'] * $up['entry_fees']) * ($pack['pack_commission']/100);
																			$total_com = $total_com + $comm;
																		}
                                            					        echo '<script>console.log("'.$total_com.'")</script>';
                                        					        }
                                        					        echo $total_com;
                                    				        ?>
															</h3>
														</div>
													</div>
												</div>
												
												<div class="col-md-12 col-lg-4">
													<div class="dash-widget ">
														<div class="circle-bar circle-bar3">
															<div class="circle-graph3" data-percent="50">
																<img src="assets/img/icon-03.png" class="img-fluid" alt="Patient">
															</div>
														</div>
														<div class="dash-widget-info">
															<h6>Request Amount</h6>
															<h3>
															<?php
                                    					        $total_req = 0;
																$total_comm_amt=0;
                                    							$up_sql = $config->query("SELECT * FROM event_post WHERE vendor_id = '$user_id' AND e_status = 2 ") ;
                                        					        while($up = $up_sql->fetch_assoc()){
                                        					            $evt_id = $up['event_id'];
                                        					            $pack_id = $up['pack_id'];
                                        					            $comm = $up['pack_comm'];
																		$v_id=$up['vendor_id'];
                                            					        $pack_sql = $config->query("SELECT * FROM package_master WHERE pack_id = '$pack_id' AND pack_status = 1 ");
                                            					        $pack = $pack_sql->fetch_assoc();
                                            					        $app_sql = $config->query("SELECT *,sum(comm_amt) as total_comm_amt,sum(entry_fees) as total_entry_fees ,count(user_id) as tot FROM apply_request WHERE event_id = '$evt_id' ");
                                            					        $app = $app_sql->fetch_assoc();
                                            					        if($up['entry']=='multi' || $up['entry']=='paid'){
																							
																			$total_req += ($app['total_entry_fees']-$app['total_comm_amt']);
																			$total_comm_amt+=$app['total_comm_amt'];
																		}
																		else{
                                            					       // $comm = ($app['tot'] * $up['entry_fees']) * ($pack['pack_commission']/100);
                                            					        $amt =  $up['entry_fees'] - ($app['tot'] * $comm);
                                            					        $total_req += $amt ;
																		}
                                            					        echo '<script>console.log("'.$total.'")</script>';
																
                                        					        }
                                        					        $balsql = $config->query("SELECT sum(amount) as total_bal FROM v_payout_history WHERE vendor_id = '$user_id' ") ;
																	$v_payout_request = $config->query("SELECT sum(payout_request_amount) as total_request FROM v_payout_request WHERE payout_customer_id = '$user_id' and payout_status!=0 ") ;
                                        					        if($balsql->num_rows > 0){
																	
																		$v_payout_request=$v_payout_request->fetch_assoc();
                                            					        $balrow = $balsql->fetch_assoc();
                                            					        $total_bal1 = $balrow['total_bal'];
																	    // print_r($v_payout_request['total_request']);exit;
                                            					        $payout_bal = ($total_req - $v_payout_request['total_request']); 
																		
																		$total_bal =  $payout_bal;
                                            					        echo round($payout_bal,2).' / '.number_format($total_bal1);
                                        					        }else{
                                        					            $payout_bal = $total_req ;
                                        					            echo $payout_bal;
                                        					        }
																
                                    				        ?>
                                    				        </h3>
														</div>
													</div>
												</div>
											</div>
										</div>
									</div>
								</div>
							</div>
	                        <div class="row">
								<div class="col-md-12">
								    <div class="d-flex justify-content-between align-items-start">
								        <h4 class="mb-4">Wallet Amount</h4>
									    <!--<a class="btn btn-sm bg-info-light" data-target="#payRequest" data-toggle="modal"><i class="fas fa-wallet"></i> Pay Request</a>-->
								    </div>
									<div class="appointment-tab">
										
										<div class="tab-content">
										
											<!-- Upcoming Appointment Tab -->
											<div class="tab-pane show active" id="upcoming-appointments">
												<div class="card card-table mb-0">
													<div class="card-body">
														<div class="table-responsive">
															<table class="table table-hover table-center mb-0">
																<thead>
																	<tr>
																		<th>S.No</th>
																		<th>Category</th>
																		<th>Cost</th>
																		<!--<th>View</th>-->
																		<th>Request</th>
																		<th>Commission</th>
																		<th>Status</th>
																		<th>Amount</th>
																	</tr>
																</thead>
																<tbody>
																    <tr>
																	    <?php
																	        $sno = 1;
            																$sql = "SELECT * FROM event_post WHERE vendor_id = '$user_id' AND e_status = 2 " ;
                        											        $res = $config->query($sql);
                        											        if($res->num_rows > 0){
                        											        while($list = $res->fetch_assoc()){
                        											            $status = $list['e_status'];
                        											            $lock = $list['l_status'];
                        											            $cate_id = $list['sub_cate_id'];
                        											            $evt_id = $list['event_id'];
                        											            $pack_id = $list['pack_id'];
																	    ?>
																	    <td><?= $sno;?></td>
																		<?php
                        											        $sub_sql = "SELECT * FROM sub_category WHERE Sub_Category_Id = '$cate_id' AND Sub_Category_status = 1 " ;
                        											        $sub_res = $config->query($sub_sql);
                        											        $sub = $sub_res->fetch_assoc();
                    											        ?>
                    											        <td><?= $sub['Sub_Category_Name'] ?></td>
																		<td>
																		    <span class="d-block text-info text-capitalize">
																		        <?php
    																		        if($list['entry'] == "free" ) echo $list['entry'];
    																		        else if($list['entry'] == "multi" || $list['entry'] == "paid"){
    																		            // $m_entry = explode(',',$list['m_entry_fees']);
    																		            echo $list['entry'].'<br>';
																						$sql_list1 = $config->query("SELECT sum(comm_amt) as total_comm_amt,COUNT(apply_id) as tot,SUM(entry_fees) as amt FROM apply_request WHERE event_id = '$evt_id' ");
																						$res_list1 = $sql_list1->fetch_assoc();
																						// print_r($res_list1);exit;
																						// print_r($res_list1['amt']);exit;
																						$amount = $res_list1['amt']-$res_list1['total_comm_amt'];
    																		            // foreach($m_entry as $m_val){
    																		                echo '('.round($amount,2).') ';
    																		            // }
    																		        } 
    																		            
																		        ?>
																		    </span>
																		</td>
																		<td>
																		    <a href="applied.php?event=<?= $evt_id ?>" class="btn btn-sm bg-primary-light" >
																		        <?php
                                											        $sql_list = $config->query("SELECT sum(comm_amt) as total_comm_amt,  COUNT(apply_id) as tot,SUM(entry_fees) as amt FROM apply_request WHERE event_id = '$evt_id' ");
                                											        $res_list = $sql_list->fetch_assoc();
                                											        echo $res_list['tot'];
																				    if($list['comm_type']=='Amount'){
                                											        $amount = ($res_list['amt'] - ($res_list['tot'] * $list['pack_comm']));
																					}else{
																						$amount = $res_list1['amt']-$res_list1['total_comm_amt'];
																					}
                                											        $amt = ($list['entry'] != "free") ? $amount : 0 ;
                            											        ?>     
																		    </a>
																		</td>
																		<td><?php
																		 if($list['entry'] != "free"){
																			if($list['comm_type']=='Amount'){
																				echo 'Rs.'.$list['pack_comm'];
																			}else{
																				echo '%'.$list['pack_comm'];
																			}
																			
																		}else{
																			echo ' - ';
																		} ?>
																		</td>
																		<td>
                        											        <?php 
                                        								        if($status == 1){
                                        								            $cur_status = "Pending";
                                        								            $btn_color = "info" ;
                                        								        }else if($status == 2){
                                        								            $cur_status = "Active";
                                        								            $btn_color = "success" ;
                                        								        }else if($status == 0){
                                        								            $cur_status = "Rejected";
                                        								            $btn_color = "danger" ;
                                        								        }
                                        								    ?>
											            
																		    <a href="#" class="btn btn-sm bg-<?= $btn_color; ?>-light" > <?= $cur_status; ?></a>
																		</td>
											                            <td><?= $amt ?></td>
											                            <td><?= ($lock == 1) ? '<i class="fas fa-lock-open"></i>' : '<i class="fas fa-lock"></i>' ;?></td>
											                            
																	</tr>
																	<?php $sno++; } } ?>
																</tbody>
															</table>		
														</div>
													</div>
												</div>
											</div>
											<!-- /Upcoming Appointment Tab -->
									   
											
										</div>
									</div>
								</div>
							</div>
	                        <div class="row">
								<div class="col-md-12">
								    <div class="d-flex justify-content-between align-items-start">
								        <h4 class="mb-4">Request Amount</h4>
									    <a class="btn btn-sm bg-info-light" data-target="#payRequest" data-toggle="modal"><i class="fas fa-wallet"></i> Pay Request</a>
								    </div>
									<div class="appointment-tab">
										
										<div class="tab-content">
										
											<!-- Upcoming Appointment Tab -->
											<div class="tab-pane show active" id="upcoming-appointments">
												<div class="card card-table mb-0">
													<div class="card-body">
														<div class="table-responsive">
															<table class="table table-hover table-center mb-0">
																<thead>
																	<tr>
																		<th>S.No</th>
																		<th>Date</th>
																		<th>Amount</th>
																		<th>Status</th>
																		<!--<th>D.Status</th>-->
																		<th>Transaction Id</th>
																		<th>Transac. Date</th>
																		<th>View</th>
																		<!--<th>Request</th>-->
																		<!--<th>Commission</th>-->
																	</tr>
																</thead>
																<tbody>
																    <?php
																        $sno = 1;
        																$sql = "SELECT * FROM v_payout_request WHERE payout_customer_id = '$user_id' ORDER BY payout_request_id DESC " ;
                    											        $res = $config->query($sql);
                    											        if($res->num_rows > 0){
                    											        while($v_pay = $res->fetch_assoc()){
                    											            $status = $v_pay['payout_status'];
                    											            $cate_id = $list['sub_cate_id'];
                    											            $evt_id = $list['event_id'];
                    											            $pack_id = $list['pack_id'];
																    ?>
																    <tr>
																	    <td><?= $sno;?></td>
																		<?php
                        											        $sub_sql = "SELECT * FROM sub_category WHERE Sub_Category_Id = '$cate_id' AND Sub_Category_status = 1 " ;
                        											        $sub_res = $config->query($sub_sql);
                        											        $sub = $sub_res->fetch_assoc();
                    											        ?>
                    											        <td><?= date('d-m-Y',strtotime($v_pay['payout_request_on'])) ?></td>
                    											        <td><?= $v_pay['payout_request_amount'] ?></td>
																		<td>
                        											        <?php 
																			    
                                        								        if($status == 1){
                                        								            $cur_status = "Pending";
                                        								            $btn_color = "warning" ;
                                        								        }else if($status == 2){
                                        								            $cur_status = "Processing";
                                        								            $btn_color = "info" ;
                                        								        }else if($status == 3){
                                        								            $cur_status = "Done";
                                        								            $btn_color = "success" ;
                                        								        }else if($status == 0){
                                        								            $cur_status = "Rejected";
                                        								            $btn_color = "danger" ;
                                        								        }
                                        								    ?>
											            
																		    <a href="#" class="btn btn-sm bg-<?= $btn_color; ?>-light" > <?= $cur_status; ?></a>
																		</td>
																	
                    											        <td><?= empty($v_pay['transac_id']) ? '' : $v_pay['transac_id'] ; ?></td>
																		<td>
																		    <?php 
                        											            if(!empty($v_pay['payout_attachment'])){ 
                        											            echo date('d-m-Y',strtotime($v_pay['payout_on']));
                        											            }
                    											            ?>
                    											        <td>
                    											            <?php 
                    											            if(!empty($v_pay['payout_attachment'])){ ?>
                    											            <a class="btn btn-primary-light" href="assets/img/payout/<?= $v_pay['payout_attachment'] ?>"><i class="fas fa-eye"></i> View</a>
                    											            <?php } ?>
                    											        </td>
                    											        
																	</tr>
																	<?php $sno++; } }else{ ?>
																    <tr>
																	    <td colspan="8" class="text-center">No Data Available.</td>
																	</tr>
																	<?php } ?>
																</tbody>
															</table>		
														</div>
													</div>
												</div>
											</div>
											<!-- /Upcoming Appointment Tab -->
									   
											
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>


				</div>

			</div>		
			<!-- /Page Content -->
    	<!-- Footer -->

		<?php include "footer.php" ; ?>

		<!-- /Footer -->
	</div>
	    <!-- Appointment Details Modal -->
		<div class="modal fade custom-modal" id="payRequest">
			<div class="modal-dialog modal-dialog-centered">
				<div class="modal-content">
					<div class="modal-header">
						<h5 class="modal-title">Payment Request</h5>
						<button type="button" class="close" data-dismiss="modal" aria-label="Close">
							<span aria-hidden="true">&times;</span>
						</button>
					</div>
					<div class="modal-body">
						<form method="post" action="wallet_request_post.php">
							<!--<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" style="display: flex;align-items: baseline;justify-content: flex-start;column-gap: 20px;">
								<label for="pay_amount">Amount</label>
								<input type="number" class="pay-box" id="pay_amount" name="pay_amount" value="<?= $payout_bal; ?>" readonly>
								<input type="hidden" class="pay-box" id="cus_id" name="cus_id" value="<?= $user_id; ?>" >
							</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>-->
							<input type="hidden" value="0" name="pay_request_value" class="pay_request_value">
							<div class="form-group d-flex align-items-end" style="column-gap:10px;">
								<button class="btn btn-proceed btn-lg btn-block  pay_request_active" type="submit" name="pay_request"><i class="fas fa-check-circle"></i> PROCEED</button>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
		<!-- /Appointment Details Modal -->
	<!-- /Main Wrapper -->
    <style>
        .pay-box {
            width: 150px;
            padding: 10px 10px 10px 20px;
            border-radius: 7px;
            border: 1px solid #dbc3c3;
        }
        .pay-box:focus-visible {
            outline: inherit;
        }
        .btn-proceed {
            background-color: #20c0f3;
            border: 1px solid #20c0f3;
            color: #fff;
        }
        .btn-proceed:hover {
            color: #fff;
            text-decoration: none;
        }
    </style>
    <script>
        let payinput =document.querySelector('.pay-box');
        
    </script>
    <script>
		<?php  if(isset($_SESSION['status']) && !empty($_SESSION['status'])) { ?>
		    swal({
              title: "<?= $_SESSION['status'] ?>",
              text: "",
              icon: "<?= $_SESSION['alert'] ?>",
            });
        <?php unset($_SESSION['status']) ; } ?>
    </script>
	<!-- jQuery -->

	<script src="assets/js/jquery.min.js"></script>

	<!-- Bootstrap Core JS -->

	<script src="assets/js/popper.min.js"></script>

	<script src="assets/js/bootstrap.min.js"></script>

	<!-- Sticky Sidebar JS -->
    <script src="assets/plugins/theia-sticky-sidebar/ResizeSensor.js"></script>
    <script src="assets/plugins/theia-sticky-sidebar/theia-sticky-sidebar.js"></script>
	
	<!-- Circle Progress JS -->
	<script src="assets/js/circle-progress.min.js"></script>
		
	<!-- Slick JS -->

	<script src="assets/js/slick.js"></script>

	<!-- Custom JS -->

	<script src="assets/js/script.js"></script>
	<script>
	  $(document).ready(function () {
			   $('.pay_request_active').click(function (e) { 
				  $('.pay_request_value').val(1);
			   });
			});
</script>
</body>


</html>