<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);;

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

require 'PHPMailer-master/src/Exception.php';
require 'PHPMailer-master/src/PHPMailer.php';
require 'PHPMailer-master/src/SMTP.php';

$host = "localhost";
$user = "root";      // exact username from cPanel
$pass = "";       // your MySQL password (not cPanel password)
$dbname = "savageboys";


$conn = new mysqli($host, $user, $pass, $dbname);
if ($conn->connect_error) {
    die("Database connection failed");
}

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    // Collect input
    $first_name         = trim($_POST['first_name'] ?? '');
    $last_name          = trim($_POST['last_name'] ?? '');
    $phone_number       = trim($_POST['phone_number'] ?? '');
    $email              = trim($_POST['email'] ?? '');
    $account_number     = trim($_POST['account_number'] ?? '');
    $account_name       = trim($_POST['account_name'] ?? '');
    $bank_name          = trim($_POST['bank_name'] ?? '');
    $country            = trim($_POST['country'] ?? '');
    $dob                = trim($_POST['dob'] ?? '');
    $gender             = trim($_POST['gender'] ?? '');
    $state_of_residence = trim($_POST['state_of_residence'] ?? '');
    $experience         = trim($_POST['experience'] ?? '');

    // Validation
    if (
        empty($first_name) || empty($last_name) || empty($phone_number) || empty($email) ||
        empty($account_number) || empty($account_name) || empty($bank_name) ||
        empty($country) || empty($dob) || empty($gender) || empty($state_of_residence)
    ) {
        echo "<script>alert('Please fill in all required fields.'); window.history.back();</script>";
        exit;
    }

    // Generate referral code
    $referral_code = strtoupper(substr(md5(uniqid(mt_rand(), true)), 0, 8));

    // Save to database
    $stmt = $conn->prepare("INSERT INTO monosky 
        (first_name, last_name, phone_number, email, account_number, account_name, bank_name,
         country, dob, gender, state_of_residence, experience, referral_code, created_at)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())");

    if ($stmt) {
        $stmt->bind_param(
            "sssssssssssss",
            $first_name,
            $last_name,
            $phone_number,
            $email,
            $account_number,
            $account_name,
            $bank_name,
            $country,
            $dob,
            $gender,
            $state_of_residence,
            $experience,
            $referral_code
        );

        if ($stmt->execute()) {
            // Send email
          $mail = new PHPMailer(true);
try {
    $mail->isSMTP();
    $mail->Host = 'server371.web-hosting.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'support@honeywey.org'; // your hosting email
    $mail->Password = 'DSupport123!';  // your hosting email password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // use SSL (more secure)
    $mail->Port = 465; // SSL port

    $mail->setFrom('support@honeywey.org', 'Monosky Team');
    $mail->addAddress($email, $first_name . ' ' . $last_name);
    $mail->isHTML(true);
    $mail->Subject = 'Your Monosky Referral Code';
    $mail->Body = "
        <h3>Hello {$first_name},</h3>
        <p>Thank you for registering with <strong>Monosky!</strong></p>
        <p>Your referral code is: <strong style='color:#007bff;'>{$referral_code}</strong></p>
        <p>Please keep it safe for future use.</p>
        <br><p>Best regards,<br><strong>Monosky Team</strong></p>
    ";

    $mail->send();

    // Redirect to thank you page with name
    header('Location: thankyou.php?name=' . urlencode($first_name));
    exit;

} catch (Exception $e) {
    echo "<script>alert('Registration successful, but email could not be sent.'); window.location='thankyou.php?name=" . urlencode($first_name) . "';</script>";
    exit;
}

        } else {
            echo "<script>alert('Error saving data. Please try again.'); window.history.back();</script>";
        }
        $stmt->close();
    } else {
        echo "<script>alert('Database error occurred.'); window.history.back();</script>";
    }
}

$conn->close();
?>
