<?php include("header.php");
 include("side.php");
 include("../db.php");

$product_amount = 300;
?>
<?php
//Clicked on send buton
if(isset($_POST['send'])){
	$userid = mysqli_real_escape_string($conn,$_POST['userid']);
	$amount = mysqli_real_escape_string($conn,$_POST['amount']);
	$id = mysqli_real_escape_string($conn,$_POST['id']);
	
	$no_of_pin = $amount/$product_amount;
	//Insert pin
	$i=1;
	while($i<=$no_of_pin){
		$new_pin = pin_generate();
		mysqli_query($conn,"insert into pin_list (`userid`,`pin`) values('$userid','$new_pin')");
		$i++;	
	}
	
	//updae pin request status
	mysqli_query($conn,"update pin_request set status='close' where id='$id' limit 1");
	
	echo '<script>alert("Pin send successfully.");window.location.assign("PV_transfer_list.php?userid='.$userid.'&amount='.$amount.'");</script>';	
}

//Pin generate
function pin_generate(){
	global $conn;
	$generated_pin = rand(100000,999999);
	
	$query = mysqli_query($conn,"select * from pin_list where pin = '$generated_pin'");
	if(mysqli_num_rows($query)>0){
		pin_generate();
	}
	else{
		return $generated_pin;
	}
	
}
?>
            <div id="layoutSidenav_content">
                <main>
                    <div class="container-fluid">
                         <h2 class="mt-30 page-title">PIN Request</h2>
                        <ol class="breadcrumb ">
                            <li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
                             <li class="breadcrumb-item active">PIN Request list</li>
                        </ol>
                        <div class="row justify-content-between">
							<!-- <div class="col-lg-12">
								<a href="add_page.html" class="add-btn hover-btn">Add New Page</a>
							</div>
							 -->
							
							<div class="col-lg-12 col-md-12">
								<div class="card card-static-2 mb-30">
									<div class="card-title-2">
										<h4>PIN Request</h4>
									</div>
									<div class="card-body-table">
										<div class="table-responsive">
											<table class="table ucp-table table-hover" id="table_id">
												<thead>
													<tr>
                                	<th>S.n.</th>
                                    <th>Userid</th>
                                    <th>Amount</th>
                                    <th>Date</th>
                                    <th>Send</th>
                                    <th>Cancel</th>
                                </tr>
                            </thead>
                                <?php
									$query = mysqli_query($conn,"select * from pin_request where status='open' ORDER BY id DESC");
									if(mysqli_num_rows($query)>0){
										$i=1;
										while($row=mysqli_fetch_array($query)){
											$id = $row['id'];
											$email = $row['email'];
											$amount = $row['amount'];
											$date = $row['date'];
										?>
										<tbody>
                                        	<tr>
                                            	<td><?php echo $i; ?></td>
                                                <td><?php echo $email; ?></td>
                                                <td><?php echo $amount; ?></td>
                                                <td><?php echo $date; ?></td>
                                                <form method="post">
                                                	<input type="hidden" name="userid" value="<?php echo $email ?>">
                                                    <input type="hidden" name="amount" value="<?php echo $amount ?>">
                                                    <input type="hidden" name="id" value="<?php echo $id ?>">
                                                	<td><input type="submit" name="send" value="Send" class="btn btn-primary"></td>
                                                </form>
                                                <td>Cancel</td>
                                            </tr>
                                        <?php
											$i++;
										}
									}
									else{
									?>
                                    	<tr>
                                        	<td colspan="6" align="center">You have no pin request.</td>
                                        </tr>
                                    <?php
									}
								?>
												</tbody>
											</table>
										</div>
									</div>
								</div>
							</div>
                        </div>
                    </div>
                </main>
               <?php include("footer.php"); ?>