<?php
  session_start();
  require '../database/connect.php';
  if (isset($_SESSION['user_id'])) {
      echo "<script>window.location.href = '../dashboard/index.php'</script>";
  }
?>

<?php
    if (isset($_POST['reset'])) {
        $email = $_POST['email'];

        $sql = "SELECT * FROM users WHERE email = '$email' AND id != 1";
        $result = $connection->query($sql);
        while ($row = $result->fetch_assoc()) {
           $full_name = $row['full_name'];
           $db_email = $row['email'];
           $user_id = $row['user_id'];
        }

        // Extract the first name
        $fname = explode(' ', $full_name)[0];

        $text = '121234567890abcdefghijklmnopqrstuvwxyz';
  			$text_suff = str_shuffle($text);
  			$reset_pass = substr($text_suff, 0, 6);

        if (empty($email)) {
          $error = '<div class="alert alert-danger d-flex align-items-center" role="alert">
            Please enter your email address to get reset link
          </div>';
        }else if ($db_email != $email) {
          $error = '<div class="alert alert-danger d-flex align-items-center" role="alert">
            User not found. Please check your email.
          </div>';
        }else if ($db_email == $email) {
          $sql_update = "UPDATE users SET password = '$reset_pass' WHERE user_id = '$user_id'";
          $update_result = $connection->query($sql_update);
          if ($update_result === TRUE) {
            // send mail
            $to = "$email";
            $subject = "Password Reset";
            $message = "
            <div style='background: #000; padding: 10px;'>
            <br>
            <center><img src='$website_url/images/$logo_img' width='200px;'></center>
            <br>
            <div style='width: 90%; font-family: sans-serif; padding: 10px 4%; background: ; margin: 5px auto; border-radius: 0px; color:#fff;'>
            <center>
              <h3>Mail From $website_email</h3>
              <h4>Password Reset</h4>
            </center>
            <hr>
            <p>
              <b>Hi $fname</b>, <br>
              You are seeing this email because you requested to reset your password.
              We have generated a new password for you. Your new password is $reset_pass. Don't share this with anyone.
            <br><br>

              <br><br>
              Kindly click the button below to login
            <br><br>
              <div>
                <a href='$website_url/root/signin.php' style='padding:15px 3%; border-radius:5px; background:#080224; color:#fff; text-decoration:none'>Sign In</a>
              </div>
            <br><br>
            Note: The details of this emails should not be disclosed to anyone.
            </p>
            <hr>
            <br>

            </div>
            <br>
            <center><p style='font-size: 13px; color:#ccc'>$website_email<sup>TM</sup> </p></center>
            <br>
            </div>";

            // Always set content-type when sending HTML email
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

            // More headers
            $headers .= "From: $website_name <$website_email>" . "\r\n";
             $headers .= "Reply-To: $website_email \r\n";
             $headers .= "Return-Path: $website_email\r\n";
            // $headers .= "CC: $website_email\r\n";

            if (mail($to,$subject,$message,$headers,"-f $website_email")){
              $error = '<div class="alert alert-success d-flex align-items-center" role="alert">
                Reset link sent, Check email address
              </div>';
            }else {
              $error = '<div class="alert alert-danger d-flex align-items-center" role="alert">
                Sorry!!! Something went wrong, please contact suppport or try again later
              </div>';
            }
          }
        }
    }
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title><?php echo $website_name ?> </title>
    <!-- Favicon -->
    <link rel="shortcut icon" href="assets/images/favicon.ico">
    <link rel="stylesheet" href="assets/css/libs.min.css">
    <link rel="stylesheet" href="assets/css/style.css?v=1.0.0">
</head>

<body class="" data-bs-spy="scroll" data-bs-target="#elements-section" data-bs-offset="0" tabindex="0">
    <!-- loader Start -->
    <div id="loading">
        <div class="loader simple-loader">
            <div class="loader-body"></div>
        </div>
    </div>
    <!-- loader END -->
    <div style="background-image: url('images/advantages-image-5.png')">
        <div class="wrapper">
            <section class="vh-100 bg-image">
                <div class="container h-100">
                    <div class="row justify-content-center h-100 align-items-center">
                        <div class="col-md-6 mt-5">
                            <div class="card">
                                <div class="card-body">
                                    <div class="auth-form">
                                        <h2 class="text-center mb-4">Forgot Password</h2>
                                        <form action="" method="post">
                                          <?php echo $error; ?>
                                            <div class="form-floating mb-3">
                                                <input type="email" name="email" class="form-control" id="floatingInput" placeholder="Enter Email">
                                                <label for="floatingInput">Enter Email</label>
                                            </div>
                                            <span>If your email is registered with us you'll receive a password reset link</span>
                                            <div class="text-center">
                                                <button type="submit" name="reset" class="btn btn-primary">Request Link</button>
                                            </div>

                                        </form>
                                        <div class="new-account mt-3 text-center">
                                            <p>Don't have an account? <a class="" href="register.php">Click
                                                    here to sign up</a></p>
                                        </div>
                                    </div>
                                    <a href="index.php"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-left" viewbox="0 0 16 16">
  <path fill-rule="evenodd" d="M6 12.5a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-9a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v2a.5.5 0 0 1-1 0v-2A1.5 1.5 0 0 1 6.5 2h8A1.5 1.5 0 0 1 16 3.5v9a1.5 1.5 0 0 1-1.5 1.5h-8A1.5 1.5 0 0 1 5 12.5v-2a.5.5 0 0 1 1 0v2z"></path>
  <path fill-rule="evenodd" d="M.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L1.707 7.5H10.5a.5.5 0 0 1 0 1H1.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"></path>
</svg> return to home</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </div>


    <!-- Backend Bundle JavaScript -->
    <script src="assets/js/libs.min.js"></script>
    <!-- widgetchart JavaScript -->
    <script src="assets/js/charts/widgetcharts.js"></script>
    <!-- fslightbox JavaScript -->
    <script src="assets/js/fslightbox.js"></script>
    <!-- app JavaScript -->
    <script src="assets/js/app.js"></script>
    <!-- apexchart JavaScript -->
    <script src="assets/js/charts/apexcharts.js"></script>
</body>

</html>
