<?php
include 'connect.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$last_date="20261026235959";
date_default_timezone_set("Asia/Kolkata");
$today_date=date("YmdHis");

   $email = $_POST["email"];
	$pass = trim($_POST['pass']);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
   header("Location: $location?message=wf");
}
 else{  
session_start();
$_SESSION['email'] = $email;
    $checkEmailStmt = "SELECT * FROM userdata WHERE email = '$email'";
    $sql=mysqli_query($conn,$checkEmailStmt) or die("FETCH ERROR");
	$r=mysqli_num_rows($sql);
	if($r>0) {
		$info=mysqli_fetch_object($sql);
		$submit_status = $info->submit_status;
		$frz = $info->frz;
		if ($frz==1){
		if ((($submit_status != 'finish') && ($last_date>=$today_date))||($submit_status == 'finish')){
		$otp = rand(000000,999999);
	$qr_insert= "UPDATE userdata SET otp='".$otp."' WHERE email='".$email."'";
	$sql_insert=mysqli_query($conn,$qr_insert) or die("FETCH ERROR otp");
	header("Location: $location/phpmailer/mail.php");
		}
		else {
           header("Location: $location?message=dto");
        }
	}
	else 
	{
		header("Location: $location?message=aco");
	}
    }
 	else{
		 if ($last_date>=$today_date) {		 
         $stmt = "INSERT INTO userdata (email) VALUES ('$email')";
       $sql_in=mysqli_query($conn,$stmt) or die("FETCH ERROR 2ss");

        if ($sql_in== TRUE) {
           $otp = rand(000000,999999);
	$qr_insert= "UPDATE userdata SET otp='".$otp."' WHERE email='".$email."'";
	$sql_insert=mysqli_query($conn,$qr_insert) or die("FETCH ERROR otp");
	header("Location: $location/phpmailer/mail.php");
        } else {
           header("Location: $location?message=swr");
        }

	}  else {
			unset($_SESSION['email']);
           header("Location: $location?message=dto");		   
        }
			
    }/* unset($_SESSION['email']);		
		header("Location: $location?message=wemail"); */
}
}

/*$my_ip = array();
$my_ip[]="172.16.6.103";
$my_ip[]="172.16.8.36";
$my_ip[]="172.16.8.32";
$ipaddress = getenv("REMOTE_ADDR");

 foreach ($my_ip as $ip)
 /*$ips = "SELECT ip FROM ip_address";
$ips_q=mysqli_query($conn,$ips) or die("FETCH ERROR");

$ipaddress = getenv("REMOTE_ADDR");
 while ($ip = mysqli_fetch_assoc($ips_q)) {
//if($ip['ip'] ==  $ipaddress)
if($ip ==  $ipaddress)
	
	{*/
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <link rel="stylesheet" href="bootstrap.min.css">
  <link rel="stylesheet" href="login.css">
    <title><?php echo $subject; ?></title>
<link rel="icon" type="image/png" href="logo.png">
</head>


<body style="background-color: #F1F1F1;">
<div  class="container" style="margin-top:10px">

	
	
	<div class="row">
		
		<div class="col-xs-12 text-center"><img src="logo.png" class="img-rounded" alt="iim ranchi" width="80" height="80"><br>
	<font style="font-size:20px; color:#00a300;"><b>भारतीय प्रबन्धन संस्थान राँची<br>Indian Institute of Management Ranchi</b></font>
		</div>
		
		</div>	
	
	  <div class="row" style="box-shadow: 8px 8px; border:2px solid; border-color:#F4B989;">
		<div class="col-sm-2 text-center"></div>
		<div class="col-sm-8 text-center">
		<div class="text-center"><h3><?php echo $subject; ?></h3></div>
		
		<div class=""><div class="msg" id="msg" name="msg">
			<?php 
			if(!empty($_GET['message']))
				{
					
				if(($_GET['message'])=="wemail")
				{
					echo "<p style='color:red;'>Email does not exist or time is over.</p>";
				}
				else if(($_GET['message'])=="success")
				{
					echo "<p style='color:green;'>Registered Successfully. Please login to complete the process.</p>";
				}
				else if(($_GET['message'])=="swr")
				{
					echo "<p style='color:red;'>Something went wrong. Please try again</p>";
				}
				else if(($_GET['message'])=="wf")
				{
					echo "<p style='color:red;'>Please enter correct email id.</p>";
				}
				else if(($_GET['message'])=="so")
				{
					echo "<p style='color:red;'>Your session is over. Please login again</p>";
				}
				else if(($_GET['message'])=="dto")
				{
					echo "<p style='color:red;'>Date & Time is over.</p>";
				}
				else if(($_GET['message'])=="aco")
				{
					echo "<p style='color:red;'>Account is closed.</p>";
				}
				
			}
			?>
			</div></div>
		
		<div class="jumbotron"  style="">
				  
				  <div class="container p-5 d-flex flex-column align-items-center">
        <?php if ($message): ?>
            <div class="toast align-items-center text-white border-0" 
          role="alert" aria-live="assertive" aria-atomic="true"
                style="background-color: <?php echo $toastClass; ?>;">
                <div class="d-flex">
                    <div class="toast-body">
                        <?php echo $message; ?>
                    </div>
                    <button type="button" class="btn-close
                    btn-close-white me-2 m-auto" 
                          data-bs-dismiss="toast"
                        aria-label="Close"></button>
                </div>
            </div>
        <?php endif; ?>
       
    </div>
				  <div class="row register-form">
						<form method="post">
							<div class="form-group">
                
          Please enter your Email ID
                <input type="email" name="email" id="email"
                  class="form-control" placeholder="Enter your Email ID here" required>
				 
				  <br>
                <button type="submit" 
                  class="btn btn-success
                bg-success" style="font-weight: 600;" onclick="show_hide()">Register / Login</button><br><br>
                
							</div>
							
							</form>
			</div>
		</div>
		</div>
		<div class="col-sm-2 text-center"></div>
  </div>
</div>

<script>
        function show_hide(){
			var check_email_id = document.getElementById("email").value;
			if (check_email_id !== null && check_email_id !== ''){
			document.getElementById("msg").innerHTML="<p style='color:green;'>Please wait, an otp will be sent to your email id</p>";
			}
			else 
			{
				alert ("Type an Email Address");
			}
		}
    </script>
</body>

</html>

 