<!DOCTYPE html>
<html lang="pt-BR">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Recuperar senha</title>
</head>
<body>
<?php
include('db.php');
require 'includes/functions.php';
include_once 'config.php';

if(isset($_POST["email"]) && (!empty($_POST["email"]))){
$email = $_POST["email"];
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email) {
   $error .="<p>Por favor, insira um E-mail válido</p>";
   }else{
   $sel_query = "SELECT * FROM `members` WHERE email='".$email."'";
   $results = mysqli_query($con,$sel_query);
   $row = mysqli_num_rows($results);
   var_dump($results);
   if ($row==""){
   $error .= "<p>Não foi encontrado nenhum usuário com o E-mail cadastrado!</p>";
   }
  }
   if($error!=""){
   echo "<div class='error'>".$error."</div>
   <br /><a href='javascript:history.go(-1)'>Go Back</a>";
   }else{
      $expFormat = mktime(
      date("H"), date("i"), date("s"), date("m") ,date("d")+1, date("Y")
      );
      $expDate = date("Y-m-d H:i:s",$expFormat);
      /* $key = md5(2418*2+$email); */
      $addKey = substr(md5(uniqid(rand(),1)),3,10);
      $key = $addKey;
      // Insert Temp Table
      mysqli_query($con,
      "INSERT INTO `password_reset_temp` (`email`, `key`, `expDate`)
      VALUES ('".$email."', '".$key."', '".$expDate."');");
      
      $m = new MailSender;
      $m->sendMail($email, 'recover', $key, 'Password');
      
      echo "<p class='msg-rec'>Foi enviado um e-mail para o endereço informado com as instruções para recuperar a sua senha. Caso não receba, verifique a sua caixa de SPAM</p>";
    }
}else{
?>

    <form method="post" action="" name="reset"><br /><br />
    <div class="form-container">
        <label><strong>Informe o seu e-mail:</strong></label><br /><br />
        <input type="email" class="form-control text-box" name="email" placeholder="exemplo@email.com" />
        <br /><br />
        <input type="submit" class="btn btn-lg btn-primary btn-block" value="Recuperar Senha"/>
    </div>
    </form>

<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<?php } ?>
<style>
  form{
      display: flex;
      justify-content: center;
      align-items: center;
  }
  
  .form-container{
      margin-top: 50px;
  }
  
  .btn {
    background-color: #98B827;
    color: white;
    font-size: 17px;
    padding: 15px;
    border-radius: 15px;
    font-weight: bold;
  }

  form label {
    font-family: sans-serif;
  }

  .text-box {
    border-radius: 8px;
    border: 1px solid #050505;
    padding: 8px;
  }
  
  .msg-rec {
      background-color: #519839;
      color: white;
      padding: 20px;
      font-family: sans-serif;
      font-size: 19px;
      font-weight: bold;
      border-radius: 15px;
  }
</style>
</body>
</html>
<!-- -->
