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

if (isset($_POST['reset_pin'])) {
    $email = $_POST['email'];
    $new_pin = md5($_POST['new_pin']);

    // Check if the email exists
    $sql = "SELECT email FROM users WHERE email=:email";
    $query = $dbh->prepare($sql);
    $query->bindParam(':email', $email, PDO::PARAM_STR);
    $query->execute();
    $results = $query->fetchAll(PDO::FETCH_OBJ);

    if ($query->rowCount() > 0) {
        // Update the user's PIN
        $sql = "UPDATE users SET password=:new_pin WHERE email=:email";
        $query = $dbh->prepare($sql);
        $query->bindParam(':new_pin', $new_pin, PDO::PARAM_STR);
        $query->bindParam(':email', $email, PDO::PARAM_STR);
        $query->execute();

        echo "<script>alert('PIN has been reset successfully');</script>";
        echo "<script type='text/javascript'> document.location = 'index.php'; </script>";
    } else {
        echo "<script>alert('Email not found');</script>";
    }
}
?>

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

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Reset PIN</title>
  <link rel="shortcut icon" type="image/png" href="assets/image/" />
  <link rel="stylesheet" href="assets/css/styles.min.css" />
  <link href="assets/bensen/stylesheet.css" rel="stylesheet">
  <script src="assets/libs/jquery/dist/jquery.min.js"></script>
</head>

<body>
  <!--  Body Wrapper -->
  <div class="page-wrapper" id="main-wrapper" data-layout="vertical" data-navbarbg="skin6" data-sidebartype="full" data-sidebar-position="fixed" data-header-position="fixed">
    <div class="position-relative overflow-hidden radial-gradient min-vh-100 d-flex align-items-center justify-content-center">
      <div class="d-flex align-items-center justify-content-center w-100">
        <div class="row justify-content-center w-100">
          <div class="col-md-8 col-lg-6 col-xxl-3">
            <div class="card mb-0">
              <div class="card-body">
                <a href="#" class="text-nowrap logo-img text-center d-block py-3 w-100">
                  <img src="assets/images/logos/logo.png" width="180" alt="">
                </a>
                <form method="POST">
                  <div class="mb-3">
                    <label for="exampleInputEmail1" class="form-label">ইমেইল</label>
                    <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="email">
                  </div>
                  <div class="mb-3">
                    <label for="newPin" class="form-label">নতুন পিন</label>
                    <input type="password" class="form-control" id="newPin" name="new_pin">
                  </div>
                  <button type="submit" name="reset_pin" class="btn btn-primary w-100 py-8 fs-4 mb-4 rounded-2">Reset PIN</button>
                  <div class="d-flex align-items-center justify-content-center">
                    <p class="fs-4 mb-0 fw-bold">পাসওয়ার্ড মনে আছে?</p>
                    <a class="text-primary fw-bold ms-2" href="index.php">লগইন করুন</a>
                  </div>
                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <script src="assets/libs/jquery/dist/jquery.min.js"></script>
  <script src="assets/libs/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>
