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

$success = false;
$show_id = "";
$show_pass = "";

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

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

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

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

// Hash password
$pass = password_hash($password_input,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){

    // Save for popup
    $success = true;
    $show_id = $mobile_without_code;
    $show_pass = $password_input;

    // WhatsApp Message
    $msg = "ID: $show_id\nPassword: $show_pass";
    $msg = urlencode($msg);

    $api_key = "YOUR_API_KEY";
    $sender = "91XXXXXXXXXX";

    $url = "https://whatsapp.adds.us.cc/send-message?api_key=$api_key&sender=$sender&number=$mobile_with_code&message=$msg";

    function sendWA($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_exec($ch);
        curl_close($ch);
    }

    sendWA($url);
}
}
?>

<!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;
}
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;
}

/* Popup */
#popup {
    display:none;
    position:fixed;
    top:0;
    left:0;
    width:100%;
    height:100%;
    background:rgba(0,0,0,0.7);
}
.popup-box {
    background:#fff;
    color:#000;
    padding:20px;
    border-radius:10px;
    width:300px;
    text-align:center;
    position:absolute;
    top:50%;
    left:50%;
    transform:translate(-50%,-50%);
}
</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>
<input type="password" name="password" placeholder="Enter Password" required>

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

<!-- ✅ POPUP -->
<div id="popup">
<div class="popup-box">
<h3 style="color:green;">✅ Registration Successful</h3>

<p><b>User ID:</b> <?php echo $show_id; ?></p>
<p><b>Password:</b> <?php echo $show_pass; ?></p>

<button onclick="closePopup()" style="margin-top:10px; padding:10px; width:100%; background:green; color:white; border:none; border-radius:5px;">
OK
</button>
</div>
</div>

<script>
<?php if($success){ ?>
document.getElementById("popup").style.display="block";
<?php } ?>

function closePopup(){
    document.getElementById("popup").style.display="none";
    window.location="index.php";
}
</script>

</body>
</html>