<?php
session_start();
include('connection.php');
require_once "vendor/autoload.php";

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;




function send_password_reset($get_username, $get_email, $forgot_string)
{
    require 'vendor/phpmailer/src/Exception.php';
    require 'vendor/phpmailer/src/PHPMailer.php';
    require 'vendor/phpmailer/src/SMTP.php';


    $mail = new PHPMailer(true);

    try {
        $mail->isSMTP();
        $mail->Host = 'https://host.vtouchsolutions.in/admin'; 
        $mail->SMTPAuth   = true;            
        $mail->Username   = 'your email';
        $mail->Password   = 'your password';                    
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;   
        $mail->Port       = 465;                           

        $mail->setFrom('your email');
        $mail->addAddress($email);

        $mail->isHTML(true);
        $mail->Subject = 'Password Reset link form Aatmaninfo';
        $mail->Body    = "we got a request form you to reset Password! <br>Click the link bellow: <br>
        <a href='http://localhost:8000/updatePassword.php?email=$email&reset_token=$reset_token'>reset password</a>";

        $mail->send();
            return true;
    } catch (Exception $e) {
            return false;
    }
    // $mail->isSMTP();
    // $mail->SMTPAuth = true;

    // $mail->Host = 'https://host.vtouchsolutions.in/admin'; // Set the SMTP server to send through
    // $mail->Username = '6129vaishu@gmail.com'; // SMTP username
    // $mail->Password = '***'; // SMTP password

    // $mail->SMTPSecure = "tls"; // Enable implicit TLS encryption
    // $mail->Port = 587; // TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    // //Recipients
    // $mail->setFrom('6129vaishu@gmail.com', $get_username);
    // $mail->addAddress($get_email); // Add a recipient

    // $mail->isHTML(true); // Set email format to HTML
    // $mail->Subject = 'Reset Password Notification';

    // $email_template = "
    //     <h1>Hello</h1>
    //     <h3>You are receiving this email because we received a password reset request for your account.</h3>
    //     <br><br>
    //     <a href='http://localhost/admin/recover-password.php?forget_string=$forgot_string&email=$get_email'>Click here to reset your password</a>
    // ";

    // $mail->Body = $email_template;
    // // $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    // if (!$mail->send()) {
    //     echo "Mailer Error: " . $mail->ErrorInfo;
    // } else {
    //     echo "Message has been sent";
    // }
}

if (isset($_POST['send_reset_link'])) {
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $forgot_string = md5(rand());

    $check_email = "SELECT * FROM user_admin WHERE email='$email' LIMIT 1";
    $check_email_run = mysqli_query($conn, $check_email);
    if (mysqli_num_rows($check_email_run) > 0) {
        $row = mysqli_fetch_array($check_email_run);
        $get_username = $row['username'];
        $get_email = $row['email'];
        $update_forget_str = "UPDATE user_admin SET forgot_string='$forgot_string', forgot_date=CURDATE() WHERE email='$get_email' LIMIT 1";
        $update_forget_str_run = mysqli_query($conn, $update_forget_str);
        if ($update_forget_str_run) {
            send_password_reset($get_username, $get_email, $forgot_string);
            header('location: forget_password.php?error=We emailed you a password reset link');
        } else {
            header('location: forget_password.php?error=Something went wrong');
        }
    } else {
        header('location: forget_password.php?error=No email found!');
        exit();
    }
}
?>
