<?php
session_name('super_admin_session');
if (!is_dir(dirname(__DIR__) . '/session_data/super')) {
  mkdir(dirname(__DIR__) . '/session_data/super', 0700, true);
}
session_save_path(dirname(__DIR__) . '/session_data/super');
session_set_cookie_params(['path' => '/', 'httponly' => true, 'samesite' => 'Strict']);
session_start();
error_reporting(0);
include('includes/dbconnection.php');

use Dotenv\Dotenv;
require_once 'vendor/autoload.php';
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$logo = $_ENV['WEBSITE_LOGO'];
$school_name = $_ENV['SCHOOL_NAME'];
$prefix = $_ENV['Admission_prefix'];

if (isset($_POST['login'])) {
  $password1 = $_POST['password'];
  $password2 = $_POST['password1'];

  if ($password1 != $password2) {
    $_SESSION['fp_error'] = 'Password and Confirm Password do not match.';
  } else {
    $email = $_POST['email'];
    $mobile = $_POST['phone'];
    $newpassword = md5($_POST['password']);

    $sql = "SELECT email FROM tblusers WHERE email=:email AND mobile=: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) {
      $con = "UPDATE tblusers SET password=:newpassword WHERE email=:email AND mobile=:mobile";
      $chngpwd1 = $dbh->prepare($con);
      $chngpwd1->bindParam(':email', $email, PDO::PARAM_STR);
      $chngpwd1->bindParam(':mobile', $mobile, PDO::PARAM_STR);
      $chngpwd1->bindParam(':newpassword', $newpassword, PDO::PARAM_STR);
      $chngpwd1->execute();
      $_SESSION['fp_success'] = 'Password successfully changed. You can now log in.';
      header("Location: admin.php");
      exit();
    } else {
      $_SESSION['fp_error'] = 'Email address or mobile number is invalid.';
    }
  }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
  <?php @include("includes/head.php"); ?>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
  <style>
    *,
    *::before,
    *::after {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }

    body.login-page {
      font-family: 'Inter', sans-serif;
      background: #eef2ff;
      min-height: 100vh;
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 20px;
    }

    .login-wrapper {
      display: flex;
      width: 860px;
      max-width: 100%;
      height: auto;
      min-height: 500px;
      border-radius: 24px;
      overflow: hidden;
      box-shadow: 0 25px 60px rgba(79, 70, 229, 0.18);
    }

    /* LEFT PANEL */
    .panel-left {
      flex: 1;
      position: relative;
      background: linear-gradient(160deg, #3730a3 0%, #4f46e5 55%, #7c3aed 100%);
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      padding: 2rem;
      overflow: hidden;
    }

    .panel-left::before {
      content: '';
      position: absolute;
      width: 280px;
      height: 280px;
      border-radius: 50%;
      border: 2px solid rgba(255, 255, 255, 0.15);
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }

    .panel-left::after {
      content: '';
      position: absolute;
      width: 180px;
      height: 180px;
      border-radius: 50%;
      border: 2px solid rgba(255, 255, 255, 0.1);
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }

    .panel-left .school-logo {
      width: 72px;
      height: 72px;
      object-fit: contain;
      border-radius: 50%;
      background: rgba(255, 255, 255, 0.15);
      padding: 8px;
      margin-bottom: 1.25rem;
      position: relative;
      z-index: 2;
    }

    .panel-left .portal-label {
      color: rgba(255, 255, 255, 0.7);
      font-size: 0.7rem;
      font-weight: 600;
      letter-spacing: 0.15em;
      text-transform: uppercase;
      margin-bottom: 0.4rem;
      position: relative;
      z-index: 2;
    }

    .panel-left .portal-title {
      color: #fff;
      font-size: 1.5rem;
      font-weight: 700;
      text-align: center;
      line-height: 1.2;
      position: relative;
      z-index: 2;
    }

    .panel-left .portal-title span {
      display: block;
      font-size: 0.85rem;
      font-weight: 400;
      color: rgba(255, 255, 255, 0.65);
      margin-top: 0.4rem;
    }

    .blob {
      position: absolute;
      border-radius: 50%;
      opacity: 0.12;
    }

    .blob-1 {
      width: 120px;
      height: 120px;
      background: #a5b4fc;
      top: -30px;
      left: -30px;
    }

    .blob-2 {
      width: 80px;
      height: 80px;
      background: #c7d2fe;
      bottom: -20px;
      right: -20px;
    }

    /* RIGHT PANEL */
    .panel-right {
      width: 400px;
      background: #fff;
      display: flex;
      flex-direction: column;
      justify-content: center;
      padding: 2.25rem 2rem;
    }

    .form-title {
      font-size: 1.25rem;
      font-weight: 700;
      color: #1e1b4b;
      margin-bottom: 0.3rem;
    }

    .form-subtitle {
      font-size: 0.8rem;
      color: #64748b;
      margin-bottom: 1.4rem;
    }

    .field-label {
      display: block;
      font-size: 0.78rem;
      font-weight: 600;
      color: #374151;
      margin-bottom: 0.35rem;
    }

    .field-wrap {
      position: relative;
      margin-bottom: 0.9rem;
    }

    .field-wrap input {
      width: 100%;
      height: 42px;
      border: 1.5px solid #e2e8f0;
      border-radius: 10px;
      padding: 0 40px 0 14px;
      font-size: 0.875rem;
      color: #1e293b;
      background: #f8fafc;
      transition: border-color 0.2s, box-shadow 0.2s;
      outline: none;
    }

    .field-wrap input:focus {
      border-color: #4f46e5;
      background: #fff;
      box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
    }

    .field-wrap .fi {
      position: absolute;
      right: 13px;
      top: 50%;
      transform: translateY(-50%);
      color: #94a3b8;
      font-size: 0.8rem;
      cursor: pointer;
    }

    .btn-login {
      width: 100%;
      height: 42px;
      background: linear-gradient(135deg, #3730a3 0%, #4f46e5 100%);
      color: #fff;
      border: none;
      border-radius: 10px;
      font-size: 0.875rem;
      font-weight: 600;
      cursor: pointer;
      transition: opacity 0.2s, transform 0.2s;
      margin-top: 0.4rem;
      margin-bottom: 1rem;
    }

    .btn-login:hover {
      opacity: 0.92;
      transform: translateY(-1px);
    }

    .back-link {
      display: block;
      text-align: center;
      font-size: 0.78rem;
      color: #4f46e5;
      text-decoration: none;
      font-weight: 500;
    }

    .back-link:hover {
      text-decoration: underline;
    }

    .alert-err {
      background: #fef2f2;
      color: #dc2626;
      border: 1px solid #fee2e2;
      border-radius: 8px;
      padding: 9px 12px;
      font-size: 0.78rem;
      margin-bottom: 1rem;
      display: flex;
      align-items: center;
      gap: 7px;
    }

    /* ── RESPONSIVE ── */
    @media (max-width: 680px) {
      body.login-page {
        padding: 16px;
        align-items: flex-start;
      }

      .login-wrapper {
        flex-direction: column;
        width: 100%;
        border-radius: 20px;
      }

      .panel-left {
        flex: none;
        width: 100%;
        padding: 1.5rem 1.25rem;
        border-radius: 0;
      }

      .panel-left::before {
        width: 180px;
        height: 180px;
      }

      .panel-left::after {
        width: 110px;
        height: 110px;
      }

      .panel-left .school-logo {
        width: 52px;
        height: 52px;
        margin-bottom: 0.75rem;
      }

      .panel-left .portal-label {
        font-size: 0.65rem;
      }

      .panel-left .portal-title {
        font-size: 1.15rem;
      }

      .panel-left .portal-title span {
        font-size: 0.78rem;
      }

      .panel-right {
        width: 100%;
        padding: 1.75rem 1.25rem;
      }
    }
  </style>
</head>

<body class="hold-transition login-page">

  <div class="login-wrapper">
    <!-- LEFT -->
    <div class="panel-left">
      <div class="blob blob-1"></div>
      <div class="blob blob-2"></div>
      <img src="<?php echo $logo; ?>" alt="Logo" class="school-logo">
      <p class="portal-label"><?php echo $school_name; ?></p>
      <h1 class="portal-title">
        Recover Access<span>We'll get you back in.</span>
      </h1>
    </div>

    <!-- RIGHT -->
    <div class="panel-right">
      <h2 class="form-title">Reset Password</h2>
      <p class="form-subtitle">Verify your identity and set a new password.</p>

      <?php if (isset($_SESSION['fp_error'])): ?>
        <div class="alert-err">
          <i class="fas fa-exclamation-circle"></i>
          <?php echo $_SESSION['fp_error'];
          unset($_SESSION['fp_error']); ?>
        </div>
      <?php endif; ?>

      <form action="" method="post">
        <div class="field-wrap">
          <label class="field-label">Email Address</label>
          <input type="email" name="email" placeholder="enter your email" required>
          <i class="fas fa-envelope fi"></i>
        </div>

        <div class="field-wrap">
          <label class="field-label">Mobile Number</label>
          <input type="tel" name="phone" placeholder="enter your mobile number" required>
          <i class="fas fa-mobile-alt fi"></i>
        </div>

        <div class="field-wrap">
          <label class="field-label">New Password</label>
          <input type="password" name="password" id="passwordField" placeholder="enter new password" required>
          <i class="fas fa-eye-slash fi" id="togglePassword"></i>
        </div>

        <div class="field-wrap">
          <label class="field-label">Confirm New Password</label>
          <input type="password" name="password1" id="passwordField1" placeholder="confirm new password" required>
          <i class="fas fa-eye-slash fi" id="togglePassword1"></i>
        </div>

        <button type="submit" name="login" class="btn-login">Reset Password</button>
      </form>

      <a href="admin.php" class="back-link">
        <i class="fas fa-arrow-left mr-1"></i> Back to login
      </a>
    </div>
  </div>

  <script>
    function toggleVisibility(fieldId, iconId) {
      const field = document.getElementById(fieldId);
      const icon = document.getElementById(iconId);
      icon.addEventListener('click', function () {
        const show = field.type === 'password';
        field.type = show ? 'text' : 'password';
        this.classList.toggle('fa-eye', show);
        this.classList.toggle('fa-eye-slash', !show);
      });
    }
    toggleVisibility('passwordField', 'togglePassword');
    toggleVisibility('passwordField1', 'togglePassword1');
  </script>
  <?php @include("includes/foot.php"); ?>
</body>

</html>