<?php include 'include/header.php';?>
<?php include 'include/navbar.php';?>



 <style>
    body {
      background: #f7f7f7;
    }
    .register-box {
      max-width: 500px;
      margin: 60px auto;
      padding: 30px;
      background: #fff;
      border-radius: 10px;
      box-shadow: 0 0 10px rgba(0,0,0,0.1);
    }
    .register-box h2 {
      margin-bottom: 25px;
    }
    .form-control:focus {
      box-shadow: none;
      border-color: #1A6789;
    }
    .btn-warning {
      width: 100%;
    }
  </style>
</head>
<body>

<div class="register-box">
  <h2 class="text-center text-dark">Create Your Account</h2>




<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // ✅ Check reCAPTCHA
    if (empty($_POST['g-recaptcha-response'])) {
        echo "<p class='p-2 bg-danger text-white rounded text-center'>Please complete the reCAPTCHA.</p>";
        exit;
    }

    $recaptcha_response = $_POST['g-recaptcha-response'];
    $verify_url = 'https://www.google.com/recaptcha/api/siteverify';
    $response = file_get_contents($verify_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
    $response_data = json_decode($response);

    if (!$response_data->success) {
        echo "<p class='p-2 bg-danger text-white rounded text-center'>reCAPTCHA failed. Try again.</p>";
        exit;
    }

    // ✅ Continue with registration if reCAPTCHA is valid
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $password = password_hash($_POST['password'], PASSWORD_BCRYPT);
    $confirm_password = $_POST['confirm_password'];

    if ($_POST['password'] !== $confirm_password) {
        echo "<p class='p-2 bg-dark text-white rounded text-center'>Passwords do not match.</p>";
        exit;
    }

    $verification_code = md5(rand());
    $query = "INSERT INTO users (name, email, password, verification_code) 
              VALUES ('$name', '$email', '$password', '$verification_code')";

 
    if (mysqli_query($conn, $query)) {
        // Send verification email
        $subject = "Verify your email -  $website_name  ";
        $message = '
<html>
<head>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f8f9fa;
      margin: 0;
      padding: 0;
    }
    .email-wrapper {
      max-width: 600px;
      margin: auto;
      background: #ffffff;
      border-radius: 8px;
      overflow: hidden;
      box-shadow: 0 0 10px rgba(0,0,0,0.1);
      border:2px solid #C86A19;
    }
    .header {
      background-color: #C86A19;
      padding: 5px;
      text-align: center;
      color: white;
    }
    .header img {
      max-height: 130px;
    }
    .banner {
      width: 100%;
      height: auto;
    }
    .content {
      padding: 30px;
      color: #333;
    }
    .btn {
       padding: 14px 25px; background-color: rgb(200, 106, 25); color: white; border-radius: 5px; text-decoration: none; font-weight: 600; display: inline-block; margin-top: 15px;
       color:white !important;
    }
    .footer {
      padding: 20px;
      text-align: center;
      font-size: 12px;
      color: #999;
      background-color: #f1f1f1;
    }
  </style>
</head>
<body>

<div class="email-wrapper">
  <div class="header">
 <img width="100px" src="'. $domain .'/img/logo.png" alt="'. $website_name .' Logo" />
  </div>


  <div class="content">
    <h3>Hello ' . htmlspecialchars($name) . ',</h3>
    <p>Thank you for registering at <strong>'. $website_name .'</strong>. To complete your registration, please verify your email address by clicking the button below.</p>

    <a href="'. $domain .'verify.php?code=' . urlencode($verification_code) . '" class="btn">Verify My Account</a>

    <p>If you did not create this account, please ignore this email.</p>
  </div>

  <div class="footer">
    &copy; ' . date("Y") . ' '. $website_name .'. All rights reserved.
  </div>
</div>

</body>
</html>';

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

        if (mail($email, $subject, $message, $headers)) {
            echo "<p class='p-2 bg-success text-white rounded text-center'>Registration successful! Check your email to verify your account.</p>";
        } else {
            echo "<p class='p-2 bg-dark text-white rounded text-center'>Failed to send verification email.</p>";
        }
    }  else {
        echo "<p class='p-2 bg-danger text-white rounded text-center'>Registration failed: " . mysqli_error($conn)."<p>";
    }
}
?>











  <form action="" method="POST">
    <div class="mb-3">
      <label for="name" class="form-label">Full Name</label>
      <input type="text" name="name" id="name" class="form-control" required placeholder="Enter full name">
    </div>

    <div class="mb-3">
      <label for="email" class="form-label">Email Address</label>
      <input type="email" name="email" id="email" class="form-control" required placeholder="Enter email">
    </div>

    <div class="mb-3">
      <label for="password" class="form-label">Password</label>
      <input type="password" name="password" id="password" class="form-control" required placeholder="Create password">
    </div>

    <div class="mb-3">
      <label for="confirm_password" class="form-label">Confirm Password</label>
      <input type="password" name="confirm_password" id="confirm_password" class="form-control" required placeholder="Confirm password">
    </div>
    <div class="mb-3">
  <div class="g-recaptcha" data-sitekey="<?php echo $recaptcha_site;?>"></div>
</div>


    <button type="submit" class="btn my-btn btn-block">Register</button>
  </form>

  <p class="mt-3 text-center">
    Already have an account? <a href="login.php" class="my-color">Login</a>
  </p>
</div>











<?php include 'include/footer.php';?>


    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  </body>
</html>