<?php
session_start();
error_reporting(0);
include('includes/config.php');

if (isset($_POST['change'])) {
    $email = $_POST['email'];
    $mobile = $_POST['mobile'];
    $newpassword = md5($_POST['newpassword']);

    $sql = "SELECT EmailId FROM tblstudents WHERE EmailId=:email and MobileNumber=:mobile";
    $query = $dbh->prepare($sql);
    $query->bindParam(':email', $email, PDO::PARAM_STR);
    $query->bindParam(':mobile', $mobile, PDO::PARAM_STR);
    $query->execute();

    if ($query->rowCount() > 0) {
        $update = "UPDATE tblstudents SET Password=:newpassword WHERE EmailId=:email AND MobileNumber=:mobile";
        $chngpwd1 = $dbh->prepare($update);
        $chngpwd1->bindParam(':email', $email, PDO::PARAM_STR);
        $chngpwd1->bindParam(':mobile', $mobile, PDO::PARAM_STR);
        $chngpwd1->bindParam(':newpassword', $newpassword, PDO::PARAM_STR);
        $chngpwd1->execute();
        echo "<script>alert('Your password was successfully changed');</script>";
    } else {
        echo "<script>alert('Invalid email or mobile number');</script>";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Password Recovery | Library</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" type="image/x-icon" href="<?php echo $_SESSION['logo']; ?>">

  <!-- Bootstrap 5 -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <!-- Font Awesome -->
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">

  <style>
    body {
      /* background: #ffffff; */
        background: #fffcffff;
      font-family: 'Segoe UI', sans-serif;
    }
    .login-wrapper {
      min-height: 100vh;
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 20px;
    }
    .login-box {
      background: #fff;
      border-radius: 15px;
      /* box-shadow: 0 8px 20px rgba(0,0,0,0.1); */
          box-shadow: 0 8px 20px #b63f78ff;
      overflow: hidden;
      width: 100%;
      max-width: 800px;
      display: flex;
      flex-direction: row;
    }
    .promo-side {
      flex: 1;
      overflow: hidden;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .promo-side img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      display: block;
    }
    .form-side {
      padding: 40px 30px;
      flex: 1;
    }
    .form-side h3 {
      font-weight: bold;
      color: #e95ea1;
      margin-bottom: 25px;
      text-align: center;
    }
    .form-text a {
      color: #0d6efd;
      text-decoration: none;
    }
    .form-text a:hover {
      text-decoration: underline;
    }
    .btn-login {
      width: 100%;
      border-radius: 25px;
    }
    @media (max-width: 768px) {
      .login-box {
        flex-direction: column;
        max-width: 90%;
      }
      .promo-side {
        padding: 20px;
      }
      .form-side {
        padding: 25px;
      }
    }
  </style>

  <script>
    function valid() {
      const np = document.chngpwd.newpassword.value;
      const cp = document.chngpwd.confirmpassword.value;
      if (np !== cp) {
        alert("New Password and Confirm Password do not match!");
        document.chngpwd.confirmpassword.focus();
        return false;
      }
      return true;
    }
  </script>
</head>

<body>
<div class="container-fluid login-wrapper">
  <div class="login-box">
    <!-- Left Side Image -->
    <div class="promo-side p-0">
      <img src="assets/img/lms_img7.AVIF" alt="Library Image">
    </div>

    <!-- Right Side Password Reset Form -->
    <div class="form-side">
      <h3><i class="fas fa-unlock-alt me-2"></i>Password Recovery</h3>
      <form method="post" name="chngpwd" onsubmit="return valid();">
        <div class="mb-3">
          <label for="email" class="form-label">Registered Email ID</label>
          <input type="email" class="form-control" name="email" required autocomplete="off">
        </div>

        <div class="mb-3">
          <label for="mobile" class="form-label">Registered Mobile Number</label>
          <input type="text" class="form-control" name="mobile" required autocomplete="off">
        </div>

        <div class="mb-3">
          <label for="newpassword" class="form-label">New Password</label>
          <input type="password" class="form-control" name="newpassword" required autocomplete="off">
        </div>

        <div class="mb-3">
          <label for="confirmpassword" class="form-label">Confirm Password</label>
          <input type="password" class="form-control" name="confirmpassword" required autocomplete="off">
        </div>

        <button type="submit" name="change" class="btn btn-primary btn-login mt-3">
          Change Password <i class="fas fa-key ms-2"></i>
        </button>

        <div class="text-center mt-3">
          <a href="index.php">Back to Login</a>
        </div>
      </form>
    </div>
  </div>
</div>

<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
