<?php

include '../db_connect.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer.php';
require 'SMTP.php';
require 'Exception.php';

$email = $_POST['email'];
$number = $_POST['number'];
$otp   = $_POST['otp'];

$con = dbConnect();

$sql1 = "SELECT * FROM signup WHERE number = '$number' OR email = '$email'";
$result = mysqli_query($con, $sql1);
$rows = mysqli_num_rows($result);

if ($rows <= 0) {

    $htmlBody = "
<!DOCTYPE html>
<html lang='en'>
<head>
  <meta charset='UTF-8'>
  <meta name='viewport' content='width=device-width, initial-scale=1.0'>
  <title>Verification Code</title>
</head>

<body style='margin:0;padding:0;background:#f6f7fb;font-family:Arial,Helvetica,sans-serif;'>

  <table width='100%' cellpadding='0' cellspacing='0'>
    <tr>
      <td align='center' style='padding:20px 0;'>

        <table width='600' cellpadding='0' cellspacing='0' style='background:#ffffff;border-radius:10px;overflow:hidden;box-shadow:0 4px 14px rgba(0,0,0,0.08);'>

          <!-- Header -->
          <tr>
            <td style='background:#c60056;text-align:center;padding:25px;'>
              <img src='https://votecep.com/voteapp/sendotp/logos.png' alt='Bpay Social' style='height:70px;margin-bottom:10px;' />
              <h2 style='color:white;margin:0;font-size:22px;font-weight:600;'>Bpay Social</h2>
            </td>
          </tr>

          <!-- Content -->
          <tr>
            <td style='padding:35px;'>
              
              <h2 style='color:#333;margin:0;font-size:24px;font-weight:600;text-align:center;'>Your Verification Code</h2>
              <p style='color:#666;font-size:15px;text-align:center;margin-top:10px;'>
                Use the following OTP to continue your verification:
              </p>

              <div style='text-align:center;margin:25px 0;'>
                <span style='display:inline-block;background:#c6005633;color:#c60056;font-size:36px;font-weight:700;padding:15px 40px;border-radius:10px;letter-spacing:5px;'>
                  $otp
                </span>
              </div>

              <p style='color:#777;font-size:14px;text-align:center;'>
                This OTP will expire in <strong>2 minutes</strong>. Please do not share this code with anyone for your account security.
              </p>

              <hr style='border:0;border-top:1px solid #eee;margin:30px 0;'>

              <p style='color:#aaa;font-size:12px;text-align:center;line-height:1.6;'>
                This is an automated message. If you did not request this verification code, you may simply ignore this email.
              </p>
            </td>
          </tr>

          <!-- Footer -->
          <tr>
            <td style='background:#fafafa;padding:20px;text-align:center;border-top:1px solid #eee;'>
              <p style='margin:0;color:#999;font-size:12px;'>
                &copy; ".date('Y')." Bpay Social. All rights reserved.
              </p>
            </td>
          </tr>

        </table>
      </td>
    </tr>
  </table>

</body>
</html>
";


    $mail = new PHPMailer(true);

    try {
        $mail->isSMTP();
        $mail->Host       = 'mail.bpluspro.xyz';
        $mail->SMTPAuth   = true;
        $mail->Username   = 'noreply@bpluspro.xyz';
        $mail->Password   = 'D&rVsTt!*Nel}tGA';
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
        $mail->Port       = 465;

        $mail->CharSet    = 'UTF-8';
        $mail->Encoding   = 'base64';

        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer'       => false,
                'verify_peer_name'  => false,
                'allow_self_signed' => true
            )
        );

        $mail->setFrom('noreply@bpluspro.xyz', 'Bpay Social');
        $mail->addAddress($email);

        $mail->isHTML(true);
        $mail->Subject = 'Your Bpay Social OTP Code';
        $mail->Body    = $htmlBody;

        // Plain text alternative (very important for inbox deliverability)
        $mail->AltBody = "Your OTP code is: $otp\nThis code is valid for 2 minutes. Do not share it with anyone.";

        $mail->send();
        echo 'success';

    } catch (Exception $e) {
        echo 'error: ' . $mail->ErrorInfo;
    }

} else {
    echo 'User already available! Change phone number or email and try again';
}

mysqli_close($con);
?>
