<?php 

session_start();
// error_reporting(0);

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


require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require 'PHPMailer/src/Exception.php';
// require '../../PHPMailer/src/Autoload.php';

 include("assets/constant/config.php");
 

 try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


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

  $otp=substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 8)),0,8);
  $text_email=$_POST['email'];

  $sql="SELECT * FROM login WHERE email='".$text_email."'";
// print_r($sql);
  $ans=$conn->prepare($sql);
  $ans->execute();
  $res=$ans->fetch();
if($res) {
  $realemail=$res['email'];
  $personename=$res['username'];
  $msg="yor password is:'".$otp."' ";
  $subject="remaind password";
// print_r($subject);
  $otp1=hash('sha256', $otp);
  function createSalt()
  {
    return'2123293dsj2hu2nikhiljdsd';
}
$salt=createSalt();
$otp_pass=hash('sha256', $salt.$otp1);

// print_r($otp_pass);
// if($text_email==$realemail){
  $sql1="UPDATE login SET password='$otp_pass' WHERE email='$text_email'";
  // print_r($sql1);
  $ans1=$conn->prepare($sql1);
  $ans1->execute();
   //  $mail = new PHPMailer;

 $stmt3 = $conn->prepare("SELECT * FROM emailsetting");

            $stmt3->execute();

            $result3 = $stmt3->fetchAll();

           

            foreach ($result3 as $row) {

                $smtp_server = $row['smtp_server'];

                $smtp_password = $row['smtp_password'];

                $smtp_enc = $row['smtp_type'];

                $smtp_username = $row['smtp_username'];

                $smtp_port = $row['stmp_port'];

              //  $email = $row['email'];

            }
    // print_r($mail);
             $mail = new PHPMailer(true);
        $mail->isSMTP();
        $mail->Host       = $smtp_server; // SMTP server
        $mail->SMTPAuth   = true;
        $mail->Username   = $smtp_username; // SMTP username
        $mail->Password   = $smtp_password; // SMTP password
        $mail->SMTPSecure = $smtp_enc;
        $mail->Port       = $smtp_port; // Port

        //Recipients
        $mail->setFrom($smtp_username, 'change pass');
        $mail->addAddress('');

        // Content
        $mail->isHTML(true);
        $mail->Subject = 'Password Reset';
        $res= $mail->Body    ="Hello, your password is:".$otp;
       //  print_r($res);
         // $mail->send();
         $mail->send();
// print_r($res);
         //echo 'Email sent successfully';
         header("location: index.php");
         
        //  if($mail->send()) {
        //         echo '<script>alert("Your new password has been sent to your email.");</script>';
        //     } else {
        //         echo '<script>alert("Unable to send email. Please try again later.");</script>';
        //     }
        // } else {
        //     echo '<script>alert("Invalid email address.");</script>';
        // }

}

}
}   catch(PDOException $e)
        {
        echo "Connection failed: " . $e->getMessage();
        }
?>
   <!--  <link rel="stylesheet" href="assets/popup_style.css">
    <div class="popup popup--icon -success js_success-popup popup--visible">
      <div class="popup__background"></div>
      <div class="popup__content">
        <h3 class="popup__content__title">
          Success
          </h1>
          <p>Password Sent Successfully.....Please check Your Email</p>
          <p>
            <a href="index.php"><button class="button button--success" data-for="js_success-popup">OK</button></a>
          </p>
      </div>
    </div>
  
    <link rel="stylesheet" href="assets/popup_style.css">
    <div class="popup popup--icon -error js_error-popup popup--visible">
      <div class="popup__background"></div>
      <div class="popup__content">
        <h3 class="popup__content__title">
          Error
          </h1>
          <p>Something Goes Wrong.....</p>
          <p>
            <a href="fpassword.php"><button class="button button--error" data-for="js_error-popup">Close</button></a>
          </p>
      </div>
    </div>
 


?> -->

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
  <title>Admin Dashboard</title>
  <meta content="" name="" />
  <meta content="" name="" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />

  <link rel="shortcut icon" href="assets/images/<?php echo $key1['photos']; ?>">

  <link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
  <link href="assets/css/icons.css" rel="stylesheet" type="text/css">
  <link href="assets/css/popup_style.css" rel="stylesheet" type="text/css">
  <link href="assets/css/style.css" rel="stylesheet" type="text/css">
  <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  <style>
    body {
      background-image: url('nature1.jpg');
    }
  </style>
</head>

<body>


  <!-- Begin page -->

  <div class="wrapper-page row align-items-center">
    <div class="col-md-6 ">
      <img src="../assets/img/about/ap1.jpg" class="vh-100" width="100%">
    </div>
    <div class="col-md-6 bg-white hv-100 d-flex align-items-center">
      <div class="col-md-8 mx-auto login-form">


        <div class="text-center mb-5">
          <a href="index.php" class="logo logo-admin"><img src="assets/images/<?php echo $photo1; ?>" alt="logo" width="300px"></a>
        </div>

        <div class="px-3 pb-3">
          <form class="form-horizontal m-t-20" action="#" method="POST">

            <div class="form-group position-relative mb-4">

              <label class="form-label">Email address</label>
              <input class="form-control" type="text" name="email" required="" placeholder="Email">
            </div>


            <div class="form-group text-center row m-t-20">
              <div class="col-12">
                <button class="btn btn-primary btn-block waves-effect waves-light py-2" type="submit" name="submit">Login</button>
              </div>
            </div>



          </form>

        </div>
      </div>
    </div>
  </div>



  <!-- jQuery  -->
  <script src="assets/js/jquery.min.js"></script>
  <script src="assets/js/popper.min.js"></script>
  <script src="assets/js/bootstrap.min.js"></script>
  <script src="assets/js/modernizr.min.js"></script>
  <script src="assets/js/detect.js"></script>
  <script src="assets/js/fastclick.js"></script>
  <script src="assets/js/jquery.blockUI.js"></script>
  <script src="assets/js/waves.js"></script>
  <script src="assets/js/jquery.nicescroll.js"></script>

  <!-- App js -->
  <script src="assets/js/app.js"></script>
  <script>
    // Hide show Password Field
    $(".toggle-password").click(function() {

      $(this).toggleClass("fa-eye fa-eye-slash");
      var input = $($(this).attr("toggle"));
      if (input.attr("type") == "password") {
        input.attr("type", "text");
      } else {
        input.attr("type", "password");
      }
    });
  </script>
</body>

</html>