<?php
    session_start();
    if(isset($_SESSION['user_id']) && isset($_SESSION['user_name']) && $_SESSION['user_cate'] === "user"){
        $user_id = $_SESSION['user_id'] ;
        $username = $_SESSION['user_name'] ;
        $email = $_SESSION['user_email'] ;
        $phone = $_SESSION['user_phone'] ;
        // $ticket = $_SESSION['last_ticket'] ;
        // $company = $_SESSION['company'] ;
        $user_image = $_SESSION['user_image'] ;
    }else{
        header("Location:login.php") ;
    }
?>
<?php 
    if(isset($_POST['event_id']) || isset($_GET['event_id'])){
        $event_id = $_POST['event_id'];
    }
    else{
        header("Location:dashboard.php") ;
    }
?> 
<?php include "head.php" ; ?>
<?php
    $sql = $config->query("SELECT * FROM event_post WHERE e_status = 2 AND event_id = '$event_id' ") ;
    if($sql){
        $row = $sql->fetch_assoc();
        $title = $row['e_title'];
        $v_id = $row['vendor_id'];
        $event_id = $row['event_id'];
        $entry = $row['entry'];
		$comm_type = $row['comm_type'];
        $comm = $row['pack_comm'];
        $entry_type = explode(',',$row['entry_type']);
        $m_entry = explode(',',$row['m_entry_fees']);
        $multi_entry = array();
        if($entry == 'free'){
			$sql = "SELECT * FROM `entry_master` WHERE event_id='$event_id'";
			$multi_entry=$config->query($sql);
            $entry_fees = $row['entry_fees'];
        }else if($entry == 'paid'){
			$sql = "SELECT * FROM `entry_master` WHERE event_id='$event_id'";
			$multi_entry=$config->query($sql);
            $entry_fees = $row['entry_fees']+$comm;
        }else if($entry == 'multi'){
			$sql = "SELECT * FROM `entry_master` WHERE event_id='$event_id'";
			$multi_entry=$config->query($sql);
			// $data=$data->fetch_all();
			// print_r($data);exit;
            // $count = count($entry_type);
            // for($i = 0;$i < $count;$i++ ){
            //     array_push($multi_entry,array('type'=>$entry_type[$i],'amt'=>($m_entry[$i]+$comm)));
            // }
        }
    }else{
        
    }
?>
<style>
    .event-box img {
        height: 300px;
        width: 100%;
    }
    .event-title {
        font-weight: 500;
        color: #4b94e5;
        /*width: 40%;*/
    }
    .event-para {
        /*width: 60%;*/
        margin-left: 10px;
    }
</style>
<body>
	<!-- Main Wrapper -->

	<div class="main-wrapper">

		<!-- Header -->

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

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

		
			<!-- Breadcrumb -->
			<?php include "breadcrumbuser.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 "sidebar.php" ; ?>
							<!-- /Profile Sidebar -->
							
						</div>
						
						<div class="col-md-7 col-lg-8 col-xl-9">
							<div class="row">
								<div class="col-md-12 card shadow event-box">
                                    <div class="p-3 h4"><?= $title ?></div>
                                    <div class="card-body row p-0">
                                        <div class="table-responsive">
    										<table class="table table-hover table-center mb-0">
    											<thead>
    												<tr>
    													<th>S.No</th>
    													<th>Name</th>
    													<th>Age </th>
    													<th>Category</th>
    													<th>Amount</th>
    												</tr>
    											</thead>
    											<tbody>
    											    <?php
    											        $sno = 1;
     											        $ply_sql = "SELECT * FROM players WHERE user_id = '$user_id' " ;
    											        $ply_res = $config->query($ply_sql);
       											        while($ply = $ply_res->fetch_assoc()){
       											            $chk_id = $ply['id'];
       											            $dob = new DateTime(date('d-m-Y',strtotime($ply['dob'])));
                                                            $now = new DateTime();  // current date
                                                            $diff = $now->diff($dob);
        											        $age = $diff->y;
      										        ?>
      										        <?php if($entry == 'multi'){ ?>
      										        <?php
      										            $chk_sql = $config->query("SELECT * FROM apply_request WHERE user_id = '$user_id' AND player_id = '$chk_id' AND event_id = '$event_id' ");
      										            if($chk_sql->num_rows > 0){
    											            $chk_res = $chk_sql->fetch_assoc();
      										        ?>
      										            <tr>
        													<td><input type="checkbox" class="form-check selectPlayer" checked disabled></td>
        													<td><?= $ply['name'] ?></td>
        													<td><?= $age ?></td>
        													<td><?= $chk_res['entry_type'] ?></td>
        													<td><?= $chk_res['entry_fees'] - $chk_res['comm_amt'] ?></td>
        												</tr>
      										        <?php }else{ ?>
          										        <tr>
        													<td><input type="checkbox" class="form-check selectPlayer" name="player" value="<?= $ply['id'] ?>" id="check-player<?= $ply['id']?>" onclick="selectPr(<?= $ply['id'] ?>)" disabled></td>
        													<td><?= $ply['name'] ?></td>
        													<td><?= $age ?></td>
        													<td>
        													    <?php if($entry == 'multi'){ ?>
    													        <select class="form-control" name="cate" id="cate<?= $ply['id'] ?>" onchange="showCheckbox(<?= $ply['id'] ?>)" style="border: none;border-bottom: 1px solid #b8c1a7;border-radius: unset !important;" >
    													            <option value="">-- Select Category --</option>
    													        <?php foreach($multi_entry as $val){ ?>
    													            <option mulit_id="<?= $val['id'] ?>" value="<?= $val['entry_price'] ?>"><?= $val['entry_type'] ?></option>
    													        <?php } ?>
    													        </select>
    													        <?php } ?>
        													</td>
        													<td><span id="amount<?= $ply['id'] ?>"></span><span style="font-size:13px; color:red;" id="comm<?= $ply['id'] ?>"></span></td>
        												</tr>
    												<?php }?>
      										        <?php }else{ ?>
      										        <?php
      										            $chk_sql = $config->query("SELECT * FROM apply_request WHERE user_id = '$user_id' AND player_id = '$chk_id' AND event_id = '$event_id' ");
      										            if($chk_sql->num_rows > 0){
    											            $chk_res = $chk_sql->fetch_assoc();
      										        ?>
    												    <tr>
        													<td><input type="checkbox" class="form-check selectPlayer" checked disabled></td>
        													<td><?= $ply['name'] ?></td>
        													<td><?= $age ?></td>
        													<td><?= $chk_res['entry_type'] ?></td>
        													<td><?= $chk_res['entry_fees'] -$chk_res['comm_amt'] ?></td>
        												</tr>
    												<?php }else{ ?>
    												<tr>
    													<td><input disabled type="checkbox" class="form-check selectPlayer" id="check-player_f_<?= $ply['id']?>"  name="player" value="<?= $ply['id'] ?>" onclick="selectPr(<?= $ply['id'] ?>,<?= $entry_fees ?>)"></td>
    													<td><?= $ply['name'] ?></td>
    													<td><?= $age ?></td>
    													<td>
													        <?php if($entry == 'paid'){ ?>
																<select class="form-control" name="cate" id="cate_p<?= $ply['id'] ?>" onchange="showCheckbox_p(<?= $ply['id'] ?>)" style="border: none;border-bottom: 1px solid #b8c1a7;border-radius: unset !important;" >
    													            <option value="">-- Select Category --</option>
    													        <?php foreach($multi_entry as $val){ ?>
    													            <option mulit_id="<?=$val['id'] ?>" value="<?= $val['entry_price'] ?>"><?= $val['entry_type'] ?></option>
    													        <?php } ?>
																</select>
													        <?php }else if($entry == 'free'){ ?>
																<select class="form-control" name="cate" id="cate_f<?= $ply['id'] ?>" onchange="showCheckbox_f(<?= $ply['id'] ?>)" style="border: none;border-bottom: 1px solid #b8c1a7;border-radius: unset !important;" >
    													            <option value="">-- Select Category --</option>
    													        <?php foreach($multi_entry as $val){ ?>
    													            <option mulit_id="<?= $val['id'] ?>" value="<?= $val['entry_price'] ?>"><?= $val['entry_type'] ?></option>
    													        <?php } ?>
																</select>
													        <?php } ?>
    													</td>
    													<td id="amount<?= $ply['id'] ?>"></td>
    												</tr>
    												<?php } ?>
    												<?php } ?>
    												<?php $sno++; } ?>
    											</tbody>
    										</table>		
    									</div>
                                    </div>
								</div>
								<div class="col-md-12 card shadow event-box">
                                    <div class="p-3 h4 text-center">Total Amount</div>
                                    <div class="card-body row flex-column align-items-center justify-content-center">
									    <div>Amount <span id="total_comm_amt"></span></div>
										<div>Entry charges +<span id="total_comm"></span></div>
                                        <div id="" class="text-center my-3" style="font-size: 24px;font-weight: 500;">Pay Amount <span id="total_amount"></div>
                                        <!--<form action="payment.php" method="POST">-->
                                        <?php if($entry == 'free'){ ?>
                                            <button type="button" class="btn btn-info" id="free_apply" disabled>Apply</button>
                                        <?php }else{ ?>
                                            <button type="button" class="btn btn-info" data-toggle="modal" data-target="#apply_tour" id="apply" disabled>Apply</button>
                                        <?php } ?>
                                        <!--</form>-->
                                    </div>
								</div>
							</div>
						</div>
					</div>
				</div>

			</div>	
			<?php if($entry == 'paid'){ ?>
			<input type="hidden" name="paid_entry" id="paid_entry" value="<?= $entry_fees ?>"> 
			<?php } ?>
			<!-- /Page Content -->
   	<!-- Footer -->

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

		<!-- /Footer -->
	</div>

	<!-- /Main Wrapper -->
	<div class="modal fade custom-modal" id="apply_tour">
		<div class="modal-dialog modal-dialog-centered">
			<div class="modal-content">
				<div class="modal-header flex-column" style="padding-bottom:0;">
					<h5 class="modal-title text-capitalize">Pay Now</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="d-flex justify-content-center align-items-center my-3" style="column-gap: 10px;">
				        <img class="img-fluid" src="assets/img/rupee.png" style="width: 50px;">
				        <p style="font-weight: 600;font-size: 35px;margin-bottom: 0;" id="pay_amount"></p>
				    </div>
				    <div class="d-flex justify-content-center align-items-center" style="gap: 15px;">
    				    <!--<form action="payment.php" method="POST">-->
    				        <input type="hidden" name="wallet_amount" id="wallet_amount" value="<?= $row['total_amount'] ?>">
                            <button type="button" class="btn btn-primary" id="wallet-apply"><i class="fas fa-wallet"></i> Wallet</button>
                        <!--</form>-->
    				    <form action="payment.php" method="POST">
    				        <input type="hidden" name="pay_event" value="<?= $event_id ?>">
                            <button type="submit" class="btn btn-success" name="apply"><i class="fas fa-money-check"></i> Pay Online</button>
                        </form>
				    </div>
				</div>
			</div>
		</div>
	</div>
	<!--Topup Wallet-->
	<div class="modal fade custom-modal" id="topup_wallet">
		<div class="modal-dialog modal-dialog-centered">
			<div class="modal-content">
				<div class="flex-column" style="padding-bottom:0;">
					<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="d-flex align-items-center flex-column my-3">
				        <img class="img-fluid" src="assets/img/exclamation.png" style="width: 50px;">
				        <p style="font-weight: 600;font-size: 18px;margin-bottom: 0;">Your Wallet Balance Is Low.</p>
				    </div>
                    <div class="d-flex justify-content-center" style="gap: 15px;">
                        <button type="button" class="btn btn-primary" data-target="#buypack" data-toggle="modal" onclick="mclose()"><i class="fas fa-wallet"></i>TopUp Wallet</button>
    				    <form action="payment.php" method="POST">
    				        <input type="hidden" name="pay_event" value="<?= $event_id ?>">
                            <button type="submit" class="btn btn-success" name="apply"><i class="fas fa-money-check"></i> Pay Online</button>
                        </form>
                    </div>
				</div>
			</div>
		</div>
	</div>
	<!-- Warning modal -->
	<div class="modal fade custom-modal" id="emptyPlayers">
		<div class="modal-dialog modal-dialog-centered">
			<div class="modal-content">
				<div class="flex-column" style="padding-bottom:0;">
					<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="d-flex align-items-center flex-column my-3">
				        <img class="img-fluid mb-3" src="assets/img/exclamation.png" style="width: 50px;">
				        <p style="font-weight: 600;font-size: 18px;margin-bottom: 0;">Fields Are Empty. Select Players.</p>
				    </div>
				</div>
			</div>
		</div>
	</div>
	<!-- Appointment Details Modal -->
	<div class="modal fade custom-modal" id="buypack">
		<div class="modal-dialog modal-dialog-centered">
			<div class="modal-content">
				<div class="modal-header">
					<h5 class="modal-title">Topup Wallet</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 action="payment.php" method="POST">
					    <div class="row">
					        <div class="col-md-12 mb-3">
					            <label class="form-label">Email</label>
					            <input type="email" class="form-control" name="email" value="<?= $email ?>" readonly>
					        </div>
					        <div class="col-md-12 mb-3">
					            <label class="form-label">Phone</label>
					            <input type="number" class="form-control" name="phone" value="<?= $phone ?>" readonly>
					        </div>
					        <div class="col-md-12 mb-3">
					            <label class="form-label" >Topup Amount</label>
					            <input type="number" class="form-control" name="amount" min="" max="" step="0.01" required >
					            <input type="hidden" name="purpose" value="Package Purchase" >
					            <input type="hidden" name="name" value="<?= $username ?>" >
					            <input type="hidden" name="bid" value="<?= $user_id ?>" >
					            <input type="hidden" name="type" value="1" >
					        </div>
					        <div class="col-md-12 d-flex justify-content-end">
					            <input type="submit" name="pay" class="btn btn-info" value="Pay">
					        </div>
					    </div>
					</form>
				</div>
			</div>
		</div>
	</div>
	<!-- jQuery -->
	<script src="assets/js/jquery.min.js"></script>

	<script>
		$(document).ready(function () {
			<?php unset($_SESSION['pid']); ?>
		});
    	function showCheckbox(cid){
    	    let type = $('#cate'+cid).val();
    	    if(type!=''){
    	        $('#check-player'+cid).removeAttr('disabled');
				var id=$('#cate'+cid).find('option:selected').attr('mulit_id');
				$.ajax({
					type: "POST",
					url: "ajax/ajaxcall.php",
					data: {mulit_id:id,"comm_type":'<?=$comm_type?>',"comm_amount":<?=$comm?>},
					dataType: "json",
					success: function (response) {
						$('#amount'+cid).html(response.total_amount);
						$('#comm'+cid).html(response.comm_amt);
						selectPr(cid);
					}
				});
    	    }else{
    	        $('#check-player'+cid).attr('disabled','true');
				$('#amount'+cid).html('');
				$('#check-player'+cid).prop('checked', false);
			    selectPr(cid);
    	    }
			$('#check-player'+cid).prop('checked', false);
    	}
    	<?php if($entry == 'paid'){ ?>
			function showCheckbox_p(cid){
    	    let type = $('#cate_p'+cid).val();
    	    if(type!=''){
    	        $('#check-player_f_'+cid).removeAttr('disabled');
				var id=$('#cate_p'+cid).find('option:selected').attr('mulit_id');
			
				$.ajax({
					type: "POST",
					url: "ajax/ajaxcall.php",
					data: {mulit_id_p:id,"comm_type":'<?=$comm_type?>',"comm_amount":<?=$comm?>},
					dataType: "json",
					success: function (response) {
						$('#amount'+cid).html(response.total_amount);
						$('#comm_p'+cid).html(response.comm_amt);
						selectPr(cid,0);
					}
				});
    	    }else{
    	        $('#check-player_f_'+cid).attr('disabled','true');
				$('#amount'+cid).html('');
				$('#check-player_f_'+cid).prop('checked', false);
			    selectPr(cid);
    	    }
			$('#check-player_p'+cid).prop('checked', false);
    	}
	    function selectPr(pid,efees){
            let selectPlayer = $('.selectPlayer:checked').val();
			let checked = $('#check-player_f_'+pid+':checked').val();
            let selectCate = $('#cate_p'+pid+' option:selected').text();
            let efee_p = $('#cate_p'+pid).val();
            $.ajax({
                method: 'POST',
                url: 'ajax/ajaxcall.php',
                data: {
					checked:checked,
                    selectPlayer: pid,
                    selectCate: selectCate,
                    paid_entry:efee_p,
					comm_type:'<?=$comm_type?>',
					comm_amount:<?=$comm?>,
                    type:"paid"
                },
                dataType: "JSON",
                success: function(data) {
                    $('#amount'+data.player).text(data.amount)
                    if(data.total != ''){
                         $('#apply').removeAttr('disabled')
                         $('#apply').attr('type','submit')
                    }else{
                        $('#apply').attr('disabled','true')
                        $('#apply').attr('type','button')
                    }
                    $('#total_amount').text(data.total)
                    $('#pay_amount').text(data.total)
					$('#total_comm').text(data.total_comm)
					$('#total_comm_amt').text((data.total-data.total_comm))
                     
                }
            })
	    }
    	<?php }else if($entry == 'multi'){ ?>
	    function selectPr(pid){
            let checked = $('#check-player'+pid+':checked').val();
            let selectCate = $('#cate'+pid+' option:selected').text();
            let efees = $('#cate'+pid).val();
            $.ajax({
                method: 'POST',
                url: 'ajax/ajaxcall.php',
                data: {
					checked:checked,
                    selectPlayer: pid,
                    selectCate: selectCate,
                    paid_entry:efees,
                    type:"multi",
					comm_type:'<?=$comm_type?>',
					comm_amount:<?=$comm?>
                },
                dataType: "JSON",
                success: function(data) {
                    $('#amount'+data.player).text(data.amount)
                    if(data.total != ''){
                         $('#apply').removeAttr('disabled')
                         $('#apply').attr('type','submit')
                    }else{
                        $('#apply').attr('disabled','true')
                        $('#apply').attr('type','button')
                    }
                    $('#total_amount').text(data.total)
					$('#total_comm').text(data.total_comm)
					$('#total_comm_amt').text((data.total-data.total_comm))
                    $('#pay_amount').text(data.total)
                     
                }
            })
            // alert(selectCate);
	    }
    	<?php }else if($entry == 'free'){ ?>
			function showCheckbox_f(cid){
    	    let type = $('#cate_f'+cid).val();
    	    if(type!=''){
    	        $('#check-player_f_'+cid).removeAttr('disabled');
				var id=$('#cate_f'+cid).find('option:selected').attr('mulit_id');
			
				$.ajax({
					type: "POST",
					url: "ajax/ajaxcall.php",
					data: {mulit_id_f:id,"comm_type":'<?=$comm_type?>',"comm_amount":<?=$comm?>},
					dataType: "json",
					success: function (response) {
						$('#amount'+cid).html(response.total_amount);
						$('#comm_f'+cid).html(response.comm_amt);
						selectPr(cid,0);
					}
				});
    	    }else{
    	        $('#check-player_f_'+cid).attr('disabled','true');
				$('#amount'+cid).html('');
				$('#check-player_f_'+cid).prop('checked', false);
			    selectPr(cid);
    	    }
			$('#check-player_f_'+cid).prop('checked', false);
    	}
	    function selectPr(pid,efees){
            let selectPlayer = $('.selectPlayer:checked').val();
			let checked_f = $('#check-player_f_'+pid+':checked').val();
			let efeess = $('#cate_f'+pid).val();
			let selectCate = $('#cate_f'+pid+' option:selected').text();
            $.ajax({
                method: 'POST',
                url: 'ajax/ajaxcall.php',
                data: {
                    selectPlayer: pid,
                    paid_entry:efeess,
                    type:"free",
					checked:checked_f,
                    selectCate: selectCate,
					comm_type:'<?=$comm_type?>',
					comm_amount:<?=$comm?>
                },
                dataType: "JSON",
                success: function(data) {
					// if(data.apply!=""){
						
					// 	 $('#free_apply').removeAttr('disabled')
                    //      $('#free_apply').attr('type','submit')
					// }else{
					// 	$('#free_apply').attr('disabled','true')
                    //     $('#free_apply').attr('type','button')
					// }
                    $('#amount'+data.player).text(data.amount)
                    $('#free_apply').removeAttr('disabled')
                    // $('#apply').attr('type','submit')
					if(data.select!=0){
						$('#total_amount').text(data.total)
						$('#total_comm').text(data.total_comm)
					    $('#total_comm_amt').text((data.total-data.total_comm))
					}else{
						$('#total_amount').text("")
						$('#total_comm').text("")
				    	$('#total_comm_amt').text((""))
					}
                   
                    $('#pay_amount').text(data.total)
                     
                }
            })
            // alert(efees);
	    }
	    <?php } ?>
	    $('#wallet-apply').click(function(){
	         let wal = parseInt($('#wallet_amount').val());
	         let total_amount =parseInt($('#total_amount').text());

	         if(total_amount <=  wal){
	             window.location.href = "event-apply.php?apply=<?= $event_id ?>";
	         }else{
	             $('#topup_wallet').modal('show');
	             $('#apply_tour').modal('hide');
	         }
	    });
	    $('#free_apply').click(function(){
	         let chk = $('#total_amount').text();
	         if(chk!=''){
	             window.location.href = "event-apply.php?apply=<?= $event_id ?>";
	         }else{
	             $('#emptyPlayers').modal('show');
	         }
	    });
	    function mclose(){
	        $('#topup_wallet').modal('hide');
	    }
	</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>
		

</body>

<!-- doccure/  30 Nov 2019 04:11:53 GMT -->

</html>