<?php
session_start();
require_once "SystemController/DbConnection.php";
require 'SystemController/CsrfProtection.php';
$csrf = new CsrfProtection();


$db_handle = new Dbconnection();


$msg = "";
$email = "";

if(isset($_POST['email'])){
    
if (!isset($_POST['csrf_token']) || !$csrf->validateToken($_POST['csrf_token'])) {
        die("CSRF token validation failed!");
    }

$token = bin2hex(random_bytes(50));

$email = $db_handle->sanitize($_POST['email']);


$user = $db_handle->runBaseQuery("SELECT * FROM users WHERE email = '$email'"); 
  
  
  if($user)
  {
   
    $msg = "<div class='alert alert-danger mx-auto'><i class='fa fa-info-circle'></i> Email already exists. Please try another one!</div>";
          
        
        
  }else{

$code = mt_rand(1000,9999);
$time = time();
$date = date("m-d-Y h:i:sa");

$message = '<!DOCTYPE html>
<html>
  <body style="font-family: Arial, sans-serif; color: #333;">
    <table width="100%" style="max-width: 600px; margin: auto; border-collapse: collapse;">
      <tr>
        <td style="padding: 20px;">
          <h2 style="color: #4CAF50;">Activation Code</h2>
          <p>Hello,</p>
          <p>You requested for a verification code. Copy the code below to verify your account.</p>
          
         
          
          <p style="font-size: 24px; font-weight: bold; color: #4CAF50;">'.$code.'</p>
          <p>This code will expire in 15 minutes.</p>
          <p>If you didn’t request this, you can safely ignore this email.</p>
          <p>Thanks,<br>Ovoh\'s Initiative Team</p>

          <hr style="margin-top: 30px;">
          <p style="font-size: 12px; color: #888;">
            Ovohs Initiative Inc.,  123 Business Rd, Suite 100, Cityville, ST 12345 
            <br>
            This is an automated email—please do not reply.
          </p>
        </td>
      </tr>
    </table>
  </body>
</html>';

$subject = "Your verification code is ".$code;

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: OvohInitiative <no-reply@ovohinitiative.com>' . "\r\n";


$db_handle->send_mail($email,$message,$subject);

$insert = $db_handle->insertRecord("INSERT INTO potential_signups (email,code,time,date) VALUES (?,?,?,?)","ssis",array($email,$code,$time,$date));

if($insert){

        $_SESSION['email'] = $email;
    header("location: confirm-otp.php");
}
    $msg = "<div class='alert alert-danger mx-auto'>
            
    <strong><i class='fa fa-info-circle'></i> Passwords do not match, please check and try again </strong>
  </div>";
  }
  
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
   
    <meta name="viewport" content="width=device-width,initial-scale=1">
    
    <link rel="icon" href="assets/img/favicon.png" type="image/x-icon">
        <link rel="icon" href="assets/img/favicon.ico" sizes="any">
        <link rel="icon" href="assets/img/favicon.svg" type="image/svg+xml">
        <link rel="apple-touch-icon" href="assets/img/favicon.png">
    
    <title>Ovohs' Initiative </title>
    <link rel="stylesheet" href="assets/css/bootstrap.min.css">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="css/fontawesome/all.min.css">
    <!-- FontAwesome CSS -->
    
    <!-- FontAwesome CSS -->
    <link rel="stylesheet" href="style.css"><!-- Custom CSS -->
     <link rel="stylesheet" href="responsive.css">
   
        <style>
      :root {
          /* Color */
          --bs-primary: #000000;
          --bs-secondary: #c0a146;
      }
  </style>
</head>
<body>
    
    <main>
            <section>
        <div class="row align-items-center g-0 w-100 h-100vh position-relative">
            <div class="col-md-5 d-md-block d-none">
                <div class="login-left-content">
                    <a href="/">
                         <img src="assets/img/logo.png" class="logo-img" alt="">
                    </a>
                </div>
            </div>
            <div class="col-md-7 overflow-hidden bg-white">
                <div class="login-right-content login-forgot-padding row">
                    <div class="pb-0 px-0">
                        <div class="text-primary d-flex align-items-center justify-content-between">
                            <div>
                                <h2 class="fw-bold text-color title-text mb-2" style="color:#BA0C2F">Create New Acccount</h2>
                                <p class="text-color">Join us and unlock the full potential of our platform with a new account</p>
                            </div>
                            <!-- FOR SMALL DEVICE TOP CATEGORIES -->
                                                    </div>
                        <form class="mt-3" method="POST" action="">
                            <?php echo $msg; ?>
                            <?php echo $csrf->csrfInput(); ?>                             <div class="form-group">
                                <label for="email" class="form-label">Email<span
                                        class="text-danger"> * </span></label>
                                <input type="email" class="form-control" name="email"
                                    placeholder="Email" id="email" value="<?php echo $email; ?>" required>

                            </div>
                            
                            
                            <button class="btn btn-danger w-100 mt-2 mb-3 padding"
                                type="submit">Continue</button>

                                                            <p class="fs-7 text-center mt-2">Already have an account?
                                    <a href="login"
                                        class=" fw-semibold text-decoration fw-600" style="color:#BA0C2F">Sign In to Your Account</a>
                                </p>
                                                    </form>
                                            </div>
                </div>
            </div>
        </div>
    </section>
        </main>
    <script src="assets/js/jquery.min.js"></script><!-- jQuery JS -->
    <script src="assets/js/bootstrap.bundle.min.js"></script><!-- Bootstrap JS -->
    
   
</body>
</html>