<?php
include('includes/config.php');

if(isset($_POST['reg'])){

$name = $_POST['name'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];

// Clean mobile
$mobile = preg_replace('/[^0-9]/','',$mobile);

// Add 91 + User ID separate
if(strlen($mobile)==10){
    $mobile_without_code = $mobile;
    $mobile_with_code = "91".$mobile;
}else{
    echo "<script>alert('Invalid Mobile');</script>";
    exit;
}

// 🔥 AUTO PASSWORD GENERATE
function generatePassword($length = 6){
    $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789';
    return substr(str_shuffle($chars),0,$length);
}

// 🔥 PREFIX PASSWORD
$raw_pass = generatePassword(6);
$password = "JANTA".$raw_pass;

// Hash password
$pass = password_hash($password,PASSWORD_DEFAULT);

// Check user
$check = mysqli_query($conn,"SELECT * FROM usertable WHERE phone='$mobile_with_code' OR emailid='$email'");

if(mysqli_num_rows($check)>0){
    echo "<script>alert('User already exists');</script>";
    exit;
}

// Insert
$sql = "INSERT INTO usertable
(phone,emailid,name,password,userid,status,joineddate,usertype,walletamount)
VALUES
('$mobile_with_code','$email','$name','$pass','$mobile_without_code','verified',NOW(),'OPERATER','0')";

$res = mysqli_query($conn,$sql);

if($res){

    // 🔥 VIP MESSAGE
    $user_msg = "╔═══════🔥 *VIP ACCOUNT ACTIVATED* 🔥═══════╗\n\n";

    $user_msg .= "🌟 *WELCOME, $name* 🌟\n\n";

    $user_msg .= "┌───────────────────────┐\n";
    $user_msg .= "│ 🆔 USER ID : $mobile_without_code\n";
    $user_msg .= "│ 🔐 PASSWORD : $password\n";
    $user_msg .= "└───────────────────────┘\n\n";

    $user_msg .= "✨ *STATUS : SUCCESSFULLY REGISTERED* ✨\n";
    $user_msg .= "🚀 *YOU ARE NOW A VIP MEMBER*\n";
    $user_msg .= "💼 *ACCESS ALL SERVICES NOW*\n\n";

    $user_msg .= "═══════════════════════\n";
    $user_msg .= "📞 SUPPORT : 919142531828\n";
    $user_msg .= "💎 *THANK YOU FOR JOINING US*\n";
    $user_msg .= "═══════════════════════\n\n";

    $user_msg .= "🔰 *JANTA SERVICE OFFICIAL* 🔰";

    $user_msg = urlencode($user_msg);

    // API Details
    $api_key = "TcwZM2Mr1YNtrinUsyHq51ympYZEV9";
    $sender = "919142531828";

    // API URL
    $user_url = "https://whatsapp.adds.us.cc/send-message?api_key=$api_key&sender=$sender&number=$mobile_with_code&message=$user_msg";

    // CURL FUNCTION
    function sendWA($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_exec($ch);
        curl_close($ch);
    }

    // Send WhatsApp message
    sendWA($user_url);

    echo "<script>alert('Registered Successfully');window.location='index.php';</script>";

}else{
    echo "<script>alert('Database Error');</script>";
}

}
?>

<!DOCTYPE html>
<html>
<head>
<title>Register</title>

<style>
body {
    background: linear-gradient(135deg,#0c1b2a,#1e3c72);
    color:#fff;
    font-family: Arial;
    text-align:center;
}
.box {
    width:340px;
    margin:80px auto;
    padding:25px;
    background:#162b3c;
    border-radius:12px;
    box-shadow:0 0 20px rgba(0,255,255,0.3);
}
h2 {
    margin-bottom:20px;
}
input {
    width:100%;
    padding:12px;
    margin:8px 0;
    border:none;
    border-radius:5px;
}
button {
    padding:12px;
    width:100%;
    background:linear-gradient(45deg,#00c853,#64dd17);
    color:white;
    border:none;
    border-radius:5px;
    cursor:pointer;
    font-weight:bold;
}
button:hover {
    opacity:0.9;
}
</style>

</head>

<body>

<div class="box">
<h2>🚀 Register Now</h2>

<form method="POST">

<input type="hidden" name="reg" value="1">

<input type="text" name="name" placeholder="Enter Name" required>

<input type="text" name="mobile" placeholder="Enter Mobile" maxlength="10" required>

<input type="email" name="email" placeholder="Enter Email" required>

<button type="submit">🔥 Create Account</button>

</form>
</div>

</body>
</html>