<?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'] === "user"){
        $user_id = $_SESSION['user_id'] ;
        $username = $_SESSION['user_name'] ;
        // $ticket = $_SESSION['last_ticket'] ;
        // $company = $_SESSION['company'] ;
        $user_image = $_SESSION['user_image'] ;
    }else{
        header("Location:login.php") ;
    }
?>

<?php

    if(isset($_POST["swap"])){
        $swap_user = "user";
        $swap_user_id = $user_id;
        $swap_to = $_POST['swap_to'];
        $swap_amount = $_POST['swap_amount'];
        $sql_wal = $config->query("SELECT * FROM ewallet_master WHERE user_id = '$user_id' ") ;
        $sql_row = $sql_wal->fetch_assoc();
        if($sql_row['total_amount'] >= $swap_amount){
            $total = $sql_row['total_amount'] - $swap_amount ;
            $sql_uwal = $config->query("SELECT * FROM ewallet_master WHERE user_id = '$swap_to' ") ;
            $sql_urow = $sql_uwal->fetch_assoc();
            $total1 = $sql_urow['total_amount'] + $swap_amount ;
    
            $sql = "INSERT INTO swap_master(swap_user, swap_user_id, swap_to, swap_amount, swap_status)
                    VALUES('$swap_user', '$swap_user_id', '$swap_to', '$swap_amount', 1);" ;
            $sql .= "UPDATE ewallet_master SET total_amount = '$total' WHERE user_id = '$user_id' ;" ;
            $sql .= "UPDATE ewallet_master SET total_amount = '$total1' WHERE user_id = '$swap_to' " ;
            if($config->multi_query($sql) === TRUE){
                $_SESSION['status'] = "Transaction Success" ;
                $_SESSION['alert'] = "success";
                header("Location:user-swap.php");
            }else{
                $_SESSION['status'] = "Transaction Failed" ;
                $_SESSION['alert'] = "error";
            }
        }else{
            $_SESSION['status'] = "Wallet Balance Low" ;
            $_SESSION['alert'] = "warning";
        }
    }

?>

<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">
									<div class="card dash-card">
										<div class="card-body" id="check">
    										<form method="post" enctype="multipart/form-data">
                							<!-- Basic Information -->
                								<div class="row form-row">
            										<div class="col-md-6">
            											<div class="form-group">
            												<label>Mobile Number <span class="text-danger">*</span></label>
                											<div class="input-group mb-3">
                                                                <input type="number" class="form-control" name="phone" id="phone" maxlength="10" >
                                                                <div class="input-group-append">
                                                                  <span type="button" class="input-group-text btn btn-primary submit-btn" id="search-phone" >Submit</span>
                                                                </div>
                                                            </div>
            												<span class="text-danger py-2"></span>
            											</div>
            										</div>
            									</div>
                    							<!-- /Basic Information -->
                							</form>
                                        </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 History</h4>
								    </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>Name</th>
																		<th>Mobile 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 swap_master WHERE swap_user_id = '$user_id' AND swap_user = 'user' ORDER BY swap_id DESC " ;
                    											        $res = $config->query($sql);
                    											        if($res->num_rows > 0){
                    											        while($swap = $res->fetch_assoc()){
                    											            $status = $swap['swap_status'];
                    											            $swap_id = $swap['swap_to'];
																    ?>
																    <tr>
																	    <td><?= $sno;?></td>
																		<?php
                        											        $sub_sql = "SELECT * FROM master_login WHERE Master_id = '$swap_id' " ;
                        											        $sub_res = $config->query($sub_sql);
                        											        $sub = $sub_res->fetch_assoc();
                    											        ?>
                    											        <td><?= $sub['Master_Name'] ?></td>
                    											        <td><?= $sub['Master_phone'] ?></td>
                    											        <td><?= date('d-m-Y',strtotime($swap['swap_on'])) ?></td>
                    											        <td><?= $swap['swap_amount'] ?></td>
																		<td>
                        											        <?php 
                                        								        if($status == 1){
                                        								            $cur_status = "Success";
                                        								            $btn_color = "success" ;
                                        								        }else{
                                        								            $cur_status = "Failed";
                                        								            $btn_color = "danger" ;
                                        								        }
                                        								    ?>
											            
																		    <a href="#" class="btn btn-sm bg-<?= $btn_color; ?>-light" > <?= $cur_status; ?></a>
																		</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>

	<!-- /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 user_num = document.getElementById('search-phone');
        user_num.addEventListener('click',()=>{
            let val = document.getElementById('phone');
            let mobile = val.value ;
            $.ajax({
                type:"POST",
                url:"check-number.php",
                data:{mobile:mobile},
                success: function(data){
                    $('#check').html(data);
                },
            });
        });
        
    </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>

</body>

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

</html>