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

if(isset($_POST['submit']) && !empty($_POST['username'])){
    $U = mysqli_real_escape_string($ahk_conn, $_POST['username']);
    
    if(strtolower($U) == "admin"){
        echo '<script>alert("You are not authorized to reset admin password.");</script>'; 
        exit;
    }
    
    $Q = mysqli_query($ahk_conn, "SELECT * FROM users WHERE phone='$U' OR email='$U'");
    
    if(mysqli_num_rows($Q) == 1){
        $D = mysqli_fetch_assoc($Q);
        
        if($D['status'] == 1){
            $NP = "Ab8sh" . rand(000000,999999);
            $HP = password_hash($NP, PASSWORD_DEFAULT);
            date_default_timezone_set("Asia/Kolkata");
            $TS = date('Y-m-d h:i:s');
            
            $IQ = mysqli_query($ahk_conn, "UPDATE users SET password='$HP', date='$TS' WHERE phone='$U' OR email='$U'");
            
            if($IQ){
                $WN = ahkweb('webname');
                $SE = "noreply@" . $_SERVER['SERVER_NAME'];
                $TO = $D['email'];
                $SB = "Password Reset Successful";
                $SV = $_SERVER['SERVER_NAME'];
                $PP = "https://cdn.pixabay.com/animation/2022/07/31/05/09/05-09-53-216_512.gif";
                $MSG = urlencode("Dear ".$D['name'].",\n\nYour password is *$NP*. Login with this password.");

                // Send WhatsApp only if otp_option is "yes"
                if(isset($webdata['otp_option']) && $webdata['otp_option'] == "yes"){
                    @file_get_contents("$ENDPOINT?api_key=$tng_wa_apikey&sender=$tng_wa_sender&number=91".$D['phone']."&message=$MSG&footer=".strtoupper($SV));
                }

                // Email
                $BODY = "<html><head><style>
                .header{text-align:center;margin-bottom:20px;}.profile-photo{width:100px;height:100px;border-radius:50%;display:block;margin:0 auto;}
                .footer{background-color:red;color:white;font-size:12px;text-align:center;padding:10px;}.footer a{color:white;text-decoration:none;}
                </style></head><body>
                <div class='header'><img src='$PP' class='profile-photo'/><h3>$WN</h3>
                <p>Your password reset is successful.</p><p>New Password: <strong>$NP</strong></p></div>
                <div class='footer'><p>$WN - <a href='https://$SV'>Click here to login</a></p></div>
                </body></html>";

                $HDR = "From: $WN <$SE>\r\nReply-To: $SE\r\nContent-Type: text/html; charset=UTF-8\r\n";
                @mail($TO, $SB, $BODY, $HDR);

                echo '<script>alert("Password reset successfully. New password sent via email'.(isset($webdata['otp_option']) && $webdata['otp_option'] == "yes" ? ' & WhatsApp' : '').'.");</script>';
            } else {
                echo '<script>alert("Failed to update password.");</script>';
            }
        } else {
            echo '<script>alert("Account not active. Contact support.");</script>';
        }
    } else {
        echo '<script>alert("Email or phone not registered.");</script>';
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" href="./template/ahkweb/assets/images/favicon-32x32.png" type="image/png" />
    <link href="./template/ahkweb/assets/plugins/simplebar/css/simplebar.css" rel="stylesheet" />
    <link href="./template/ahkweb/assets/plugins/perfect-scrollbar/css/perfect-scrollbar.css" rel="stylesheet" />
    <link href="./template/ahkweb/assets/plugins/metismenu/css/metisMenu.min.css" rel="stylesheet" />
    <link href="./template/ahkweb/assets/css/pace.min.css" rel="stylesheet" />
    <script src="./template/ahkweb/assets/js/pace.min.js"></script>
    <link href="./template/ahkweb/assets/css/bootstrap.min.css" rel="stylesheet">
    <link href="./template/ahkweb/assets/css/bootstrap-extended.css" rel="stylesheet">
    <link href="./template/ahkweb/assets/css/newlogin-extended.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link href="./template/ahkweb/assets/css/app.css" rel="stylesheet">
    <link href="./template/ahkweb/assets/css/icons.css" rel="stylesheet">
    <title><?php ahkweb('webname'); ?></title>
  
</head>

<body>
   <!-- Header -->
<nav class="navbar navbar-expand-lg navbar-dark">
    <div class="container">
        <a class="navbar-brand" href="index.php">
           <?= $webdata['webname'] ?? '' ?>
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav ms-auto">
                <li class="nav-item">
                    <a class="nav-link active" href="index.php">Home</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="register.php">Register</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="Forgot-password.php">Forgot Password</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Contact</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

   <!-- Login Content -->
    <div class="wrapper py-5">
        <div class="section-authentication-signin d-flex align-items-center justify-content-center my-5 my-lg-0">
            <div class="container-fluid">
                <div class="row row-cols-1 row-cols-lg-2 row-cols-xl-3">
                    <div class="col mx-auto">
                        <div class="card">
                            <div class="card-body">
                                <div class="p-4 rounded">
                                    <div class="text-center">
                                        <h3 class="welcome-text">Forgot Password</h3>
                                        <p class="auth-subtitle">Access your account to continue your journey</p>
                                    </div>
                                    <div class="form-body">
                                        <form method="POST" action="" class="row g-4">
                                            <div class="col-12">
                                                <label for="inputEmailAddress" class="form-label">Email or Phone</label>
                                                <input name="username" type="text" class="form-control" id="inputEmailAddress" placeholder="Email or Phone">
                                            </div>
                                            <div class="col-md-6">
                                                <div class="form-check form-switch">
                                                    <input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" checked>
                                                    <label class="form-check-label" for="flexSwitchCheckChecked">Remember Me</label>
                                                </div>
                                            </div>
                                            <div class="col-md-6 text-end">
                                                <a href="forgot-password.php"></a>
                                            </div>
                                            <div class="col-12">
                                                <div class="d-grid">
                                                    <button type="submit" name="submit" class="btn btn-primary">
                                                        <i class="bx bxs-lock-open"></i>Submit</button>
                                                </div>
                                            </div>
                                            <div class="col-12 text-center">
                                                <p class="mb-0">Don't have an account yet? <a href="login.php">Sign in here</a></p>
                                            </div>
                                          </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <!--footer class="footer mt-5">
        <div class="container">
            <div class="row">
                <div class="col-lg-4 mb-4 mb-lg-0">
                    <h5><?php echo strtoupper($_SERVER['SERVER_NAME']); ?></h5>
                    <p>Transforming India into a digitally empowered society and knowledge economy.</p>
                    <div class="social-icons mt-3">
                        <a href="#"><i class="bx bxl-facebook"></i></a>
                        <a href="#"><i class="bx bxl-twitter"></i></a>
                        <a href="#"><i class="bx bxl-instagram"></i></a>
                        <a href="#"><i class="bx bxl-linkedin"></i></a>
                    </div>
                </div>
                <div class="col-lg-2 col-md-4 mb-4 mb-md-0">
                    <h5>Quick Links</h5>
                    <ul class="list-unstyled">
                        <li><a href="index.php">Home</a></li>
                        <li><a href="#">About Us</a></li>
                        <li><a href="#">Services</a></li>
                        <li><a href="#">Contact</a></li>
                    </ul>
                </div>
                <div class="col-lg-2 col-md-4 mb-4 mb-md-0">
                    <h5>Account</h5>
                    <ul class="list-unstyled">
                        <li><a href="login.php">Login</a></li>
                        <li><a href="register.php">Register</a></li>
                        <li><a href="Forgot-password.php">Forgot Password</a></li>
                        <li><a href="#">Privacy Policy</a></li>
                    </ul>
                </div>
                <div class="col-lg-4 col-md-4">
                    <h5>Contact Info</h5>
                    <ul class="list-unstyled">
                        <li><i class="bx bx-map me-2"></i> <?php ahkweb('address'); ?></li>
                        <li><i class="bx bx-phone me-2"></i> +91 <?php ahkweb('phone'); ?></li>
                        <li><i class="bx bx-envelope me-2"></i> <?php ahkweb('email'); ?></li>
                    </ul>
                </div>
            </div>
            <hr class="my-4">
            <div class="row align-items-center">
                <div class="col-md-6">
                   <p class="mb-0">
    © 2023 <?php echo strtoupper($_SERVER['SERVER_NAME']); ?>. All rights reserved.
</p>

                </div>
                <div class="col-md-6 text-md-end">
                    <a href="#">Terms of Service</a>
                    <span class="mx-2">|</span>
                    <a href="#">Privacy Policy</a>
                </div>
            </div>
        </div-->
    </footer>

    <!-- Bootstrap JS -->
    <script src="./template/ahkweb/assets/js/bootstrap.bundle.min.js"></script>
    <!--plugins-->
    <script src="./template/ahkweb/assets/js/jquery.min.js"></script>
    <script src="./template/ahkweb/assets/plugins/simplebar/js/simplebar.min.js"></script>
    <script src="./template/ahkweb/assets/plugins/metismenu/js/metisMenu.min.js"></script>
    <script src="./template/ahkweb/assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
    <!--Password show & hide js -->
    <script>
        $(document).ready(function () {
            $("#show_hide_password a").on('click', function (event) {
                event.preventDefault();
                if ($('#show_hide_password input').attr("type") == "text") {
                    $('#show_hide_password input').attr('type', 'password');
                    $('#show_hide_password i').addClass("bx-hide");
                    $('#show_hide_password i').removeClass("bx-show");
                } else if ($('#show_hide_password input').attr("type") == "password") {
                    $('#show_hide_password input').attr('type', 'text');
                    $('#show_hide_password i').removeClass("bx-hide");
                    $('#show_hide_password i').addClass("bx-show");
                }
            });
        });
    </script>
    <!--app JS-->
    <script src="./template/ahkweb/assets/js/app.js"></script>
</body>
</html>