<?php
    include('includes/header.php'); 
    echo "<title>Password Reset |  $_SESSION[newName] </title>";
    echo "<link rel='stylesheet' href='css/login.css'>";
?>
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-4"></div>
            <div class="col-md-4 wrapper-login">
                <img src="<?php echo $_SESSION['newImageLink']; ?>" width="200px">
                <div class="wrapper-box">
                    <h1>Password Reset</h1>
                    <form action="reset.php" method="POST">
                        <div class="form-box">
                            <label for="n">Email Address</label>
                            <label class="text-muted">The Email Address Used In registration</label>
                            <input type="email" id="n" name="email" placeholder="name@example.com">
                            <br/><br/>
                            <input type="submit" name="validate" value="Send Reset Link">

                            <p style="margin-top:10px;">Repeat <a href="login.php">Login.</a></p>

                            <p style="margin-top:45px;">© Copyright 2026 <a href="<?php echo $_SESSION['newExternalLink']; ?>"><?php echo $_SESSION['newName']; ?></a> All Rights Reserved.</p>
                        </div>
                    </form>
                </div>
            </div>
            <div class="col-md-4"></div>
        </div>
    </div>
    



    <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/main.js"></script>
<?php include('includes/footer.php'); ?>


<?php 

    if(isset($_POST['validate'])){
        
        extract($_POST);
        
        $tblquery = "SELECT * FROM users WHERE email = :email and status = :status";
        $tblvalue = array(
            ':email'=> htmlspecialchars($email),
            ':status'=> 'Active'
        );
        $select = $collect->tbl_select($tblquery, $tblvalue);
        if($select){

            function random_strings($length_of_string){
              
                $str_result = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
              
                return substr(str_shuffle($str_result), 0, $length_of_string);
            }
             
            $password =  random_strings(5);

            $tblquery = "UPDATE users SET password = :password WHERE email = :email";
            $tblvalue = array(
                ':password' => $password,   
                ':email' =>    htmlspecialchars($_POST['email'])
            );
            $update = $collect->tbl_update($tblquery, $tblvalue);
            
            if($update){
                $to = $_POST['email'];
                $subject = 'Recovery Password';
                $from =  $_SESSION['sendingEmail'];
                 
                // To send HTML mail, the Content-type header must be set
                $headers  = 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 
                // Create email headers
                $headers .= 'From: '.$from."\r\n".
                    'Reply-To: '.$from."\r\n" .
                    'X-Mailer: PHP/' . phpversion();
                 
                // Compose a simple HTML email message
                
                $message = '<html><body>';
                $message .= '<p>Your new password is <span style="color:blue;">' . $password . '.</span> Login your account and make sure you change your password in account setting for security purpose, So your new genarated password won\'t get to the wrong hands.</p><br>';
                $message .= '<p>You\'re receiving this message because an account using this email requested for a new password which was created and sent to this email.<br><span style="color:red;">NOTE: Do not share your email with anyone. Thanks</p>';
                $message .= '</body></html>';
        
                // Send Mail
                $sent = mail($to, $subject, $message, $headers);
                
                if($sent){
                    echo "
                        <div class='container-fluid invest-popup-notification'>
                            <div class='main-box'>
                                <div class='head'>
                                    <div class='row no-p'>
                                        <div class='col-xs-11'>
                                            <h4>Message</h4>
                                        </div>
                                        <div class='col-xs-1 close' onclick='popupInvestNotification()'>
                                            <h4><span><i class='fa fa-times' aria-hidden='true'></i></span></h4>
                                        </div>
                                    </div>
                                </div>
                                <div class='bottom'>
                                    <div class='msg'>
                                       A new password has been sent to your email.
                                    </div>
                                    <a class='btn' onclick='popupInvestNotification()'>Ok</a>
                                </div>
                            </div>
                        </div>
                    ";
                }
            }

        }else{
            echo "
                <div class='container-fluid invest-popup-notification'>
                    <div class='main-box'>
                        <div class='head'>
                            <div class='row no-p'>
                                <div class='col-xs-11'>
                                    <h4>Message</h4>
                                </div>
                                <div class='col-xs-1 close' onclick='popupInvestNotification()'>
                                    <h4><span><i class='fa fa-times' aria-hidden='true'></i></span></h4>
                                </div>
                            </div>
                        </div>
                        <div class='bottom'>
                            <div class='msg'>
                                This account do not exit.
                            </div>
                            <a class='btn' onclick='popupInvestNotification()'>Ok</a>
                        </div>
                    </div>
                </div>
            ";
        }
    }

?>