<?php
include('includes/config.php');
session_start();
$success = false;

if(isset($_POST['fsecret']) && $_POST['fsecret'] == "ahkwebsolutions_reset"){

  $username = mysqli_real_escape_string($conn,$_POST['username']);
  $_SESSION['tmpuser'] = $username;

  $res = mysqli_query($conn,"SELECT * FROM usertable WHERE phone='$username' OR emailid='$username' OR userid='$username'");

  if(mysqli_num_rows($res)==1){

    $userdata = mysqli_fetch_assoc($res);
    $otp = rand(10000,99999);

    mysqli_query($conn,"UPDATE usertable SET otp='$otp' WHERE phone='$username' OR emailid='$username' OR userid='$username'");

    $mob_no = $userdata['phone'];

    // ================= EMAIL =================
    include('gmail-otp/ak.php');

    $msg = "Hello ".$userdata['name']."<br>Your OTP is: <b>$otp</b>";

    $mail = new PHPMailer(); 
    $mail->IsSMTP(); 
    $mail->SMTPAuth = true; 
    $mail->SMTPSecure = 'tls'; 
    $mail->Host = "localhost";
    $mail->Port = 587; 
    $mail->IsHTML(true);
    $mail->CharSet = 'UTF-8';
    $mail->Username = $emailfrom;
    $mail->Password = $smtp_password;
    $mail->SetFrom($emailfrom,"Janata Service");
    $mail->Subject = "Password Reset OTP";
    $mail->Body =$msg;
    $mail->addAddress($userdata['emailid']);

    $mail->Send();

    // ================= WHATSAPP =================
    $wa_msg = "🔐 PASSWORD RESET OTP\n\n";
    $wa_msg .= "Hello ".$userdata['name']."\n\n";
    $wa_msg .= "Your OTP: $otp\n\n";
    $wa_msg .= "Do not share this OTP\n\n";
    $wa_msg .= "JANTA SERVICE";

    $wa_msg = urlencode($wa_msg);

    $api_key = "TcwZM2Mr1YNtrinUsyHq51ympYZEV9";
    $sender = "919142531828";

    $wa_url = "https://whatsapp.adds.us.cc/send-message?api_key=$api_key&sender=$sender&number=$mob_no&message=$wa_msg&footer=Sent%20via%20mpwa";

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

    sendWA($wa_url);

    $success = true;

  }else{
    echo "<script>alert('User not found');</script>";
  }
}

// ================= PASSWORD RESET =================
if(isset($_POST['preset'])){

  $username = $_POST['username'];
  $otp = $_POST['otp'];
  $password = $_POST['password'];
  $cpassword = $_POST['cpassword'];

  $check = mysqli_query($conn,"SELECT * FROM usertable WHERE phone='$username' OR emailid='$username' OR userid='$username'");

  if(mysqli_num_rows($check)==1){

    $data = mysqli_fetch_assoc($check);

    if($otp == $data['otp']){

      if($password == $cpassword){

        $hash = password_hash($password,PASSWORD_DEFAULT);
        mysqli_query($conn,"UPDATE usertable SET password='$hash', otp='' WHERE phone='$username' OR emailid='$username' OR userid='$username'");

        echo "<script>alert('Password Reset Success');window.location='login.php';</script>";

      }else{
        echo "<script>alert('Password Not Match');</script>";
      }

    }else{
      echo "<script>alert('Invalid OTP');</script>";
    }

  }
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Forgot Password</title>

<style>
body {
    background:#0c1b2a;
    color:#fff;
    text-align:center;
    font-family:Arial;
}
.box {
    width:350px;
    margin:80px auto;
    padding:25px;
    background:#162b3c;
    border-radius:10px;
}
input,button {
    width:100%;
    padding:12px;
    margin:8px 0;
}
button {
    background:green;
    color:#fff;
    border:none;
}
</style>

</head>

<body>

<div class="box">

<?php if(!$success){ ?>

<h2>Forgot Password</h2>

<form method="POST">
<input type="hidden" name="fsecret" value="ahkwebsolutions_reset">
<input type="text" name="username" placeholder="Mobile / Email / UserID" required>
<button type="submit">Send OTP</button>
</form>

<?php } else { ?>

<h2>Reset Password</h2>

<form method="POST">
<input type="hidden" name="preset" value="1">
<input type="hidden" name="username" value="<?php echo $_SESSION['tmpuser']; ?>">

<input type="text" name="otp" placeholder="Enter OTP" required>
<input type="password" name="password" placeholder="New Password" required>
<input type="password" name="cpassword" placeholder="Confirm Password" required>

<button type="submit">Reset Password</button>
</form>

<?php } ?>

</div>

</body>
</html>