<?php
// Safe bootstrap guards for this view.
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

if (!function_exists('e')) {
    function e($value): string
    {
        return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
    }
}

$appUrl = defined('APP_URL') ? rtrim((string) APP_URL, '/') : '';

// Use the application's CSRF helper when available. The fallback prevents a
// view-level fatal error, but the controller should still validate CSRF.
$csrfField = '';
if (class_exists('Auth') && method_exists('Auth', 'csrfField')) {
    $csrfField = Auth::csrfField();
} else {
    if (empty($_SESSION['_csrf_token'])) {
        $_SESSION['_csrf_token'] = bin2hex(random_bytes(32));
    }
    $csrfField = '<input type="hidden" name="_token" value="' . e($_SESSION['_csrf_token']) . '">';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Forgot Password – MJPOS SYSTEMS</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<style>
  *{box-sizing:border-box}
  body{margin:0;min-height:100vh;font-family:'Segoe UI',system-ui,-apple-system,sans-serif;
       background:linear-gradient(135deg,#0f172a 0%,#1e3a5f 45%,#0891b2 100%);
       display:flex;flex-direction:column;align-items:center;justify-content:center;
       padding:20px;overflow-x:hidden}

  body::before{
    content:'';position:fixed;top:-100px;right:-100px;width:340px;height:340px;
    background:radial-gradient(circle,rgba(255,255,255,.07),transparent 70%);
    border-radius:50%;pointer-events:none;
  }
  body::after{
    content:'';position:fixed;bottom:-120px;left:-80px;width:300px;height:300px;
    background:radial-gradient(circle,rgba(34,211,238,.1),transparent 70%);
    border-radius:50%;pointer-events:none;
  }

  .fp-card{
    width:100%;max-width:440px;background:#fff;border-radius:18px;
    box-shadow:0 20px 50px rgba(0,0,0,.25);overflow:hidden;position:relative;z-index:2;
  }
  .fp-header{
    background:linear-gradient(135deg,#0e7490,#0891b2);color:#fff;
    padding:32px 32px 26px;text-align:center;
  }
  .fp-icon{
    width:56px;height:56px;background:rgba(255,255,255,.15);border-radius:14px;
    display:flex;align-items:center;justify-content:center;font-size:24px;
    margin:0 auto 14px;border:1px solid rgba(255,255,255,.2);backdrop-filter:blur(4px);
  }
  .fp-header h5{font-weight:800;margin-bottom:4px}
  .fp-header p{opacity:.82;font-size:.82rem;margin:0}
  .fp-body{padding:28px 32px 30px}

  .form-label-modern{font-size:.8rem;font-weight:700;color:#334155;margin-bottom:6px;display:block}
  .input-modern{
    border:1.5px solid #e2e8f0;border-radius:10px;padding:11px 14px;font-size:.92rem;
    width:100%;transition:border-color .15s,box-shadow .15s;background:#f8fafc;
  }
  .input-modern:focus{outline:none;border-color:#0891b2;box-shadow:0 0 0 3px rgba(8,145,178,.12);background:#fff}
  .input-group-modern{position:relative}
  .input-group-modern i.input-leading{
    position:absolute;left:14px;top:50%;transform:translateY(-50%);color:#94a3b8;font-size:.95rem;z-index:2
  }
  .input-group-modern .input-modern{padding-left:40px}

  .btn-fp{
    background:linear-gradient(135deg,#0e7490,#0891b2);border:none;border-radius:10px;
    padding:12px;font-weight:700;font-size:.95rem;color:#fff;width:100%;
    transition:transform .12s,box-shadow .15s;
  }
  .btn-fp:hover{transform:translateY(-1px);box-shadow:0 8px 18px rgba(8,145,178,.35);color:#fff}

  .helper-text{font-size:.82rem;color:#64748b;margin-bottom:22px;line-height:1.5}

  .back-link{
    display:inline-flex;align-items:center;gap:6px;color:#fff;opacity:.85;
    text-decoration:none;font-size:.85rem;font-weight:600;margin-top:22px;
    position:relative;z-index:2;
  }
  .back-link:hover{opacity:1;color:#fff}

  /* Success state */
  .success-box{
    background:#f0fdf4;border:1.5px solid #86efac;border-radius:12px;padding:18px;
  }
  .success-box .s-title{color:#15803d;font-weight:700;font-size:.92rem;margin-bottom:10px;
    display:flex;align-items:center;gap:6px}
  .reset-url-box{
    background:#fff;border:1px solid #d1fae5;border-radius:8px;padding:10px 12px;
    word-break:break-all;font-size:.78rem;margin-bottom:10px;
  }
  .expiry-note{font-size:.76rem;color:#64748b;display:flex;align-items:center;gap:5px;margin-bottom:12px}

  .alert-modern{
    border-radius:10px;border:none;font-size:.85rem;padding:12px 16px;margin-bottom:18px;
  }
</style>
</head>
<body>

<div class="fp-card">

  <div class="fp-header">
    <div class="fp-icon"><i class="bi bi-key-fill"></i></div>
    <h5>Forgot Password?</h5>
    <p>MJPOS SYSTEMS</p>
  </div>

  <div class="fp-body">

    <?php // Flash messages
    if (!empty($_SESSION['flash'])):
      foreach ($_SESSION['flash'] as $type => $messages):
        foreach ($messages as $msg):
          $cls = $type === 'error' ? 'danger' : ($type === 'success' ? 'success' : 'info');
    ?>
    <div class="alert alert-<?= $cls ?> alert-modern alert-dismissible" role="alert">
      <i class="bi bi-<?= $cls==='danger'?'x-circle':'info-circle' ?> me-1"></i>
      <?= e($msg) ?>
      <button type="button" class="btn-close" style="font-size:.7rem" data-bs-dismiss="alert"></button>
    </div>
    <?php endforeach; endforeach;
      unset($_SESSION['flash']);
    endif; ?>

    <?php
    $resetDisplay = $_SESSION['reset_token_display'] ?? null;
    if ($resetDisplay && isset($_GET['sent'])):
      unset($_SESSION['reset_token_display']);
    ?>

    <div class="success-box mb-3">
      <div class="s-title">
        <i class="bi bi-check-circle-fill"></i>
        Reset link generated for <?= e($resetDisplay['name']) ?>
      </div>
      <p class="small text-muted mb-2">Click the link below to set a new password:</p>
      <div class="reset-url-box">
        <a href="<?= e($resetDisplay['url']) ?>" class="text-decoration-none fw-semibold" style="color:#0891b2">
          <?= e($resetDisplay['url']) ?>
        </a>
      </div>
      <div class="expiry-note">
        <i class="bi bi-clock-history"></i>
        Expires <?= date('d M Y, H:i', strtotime($resetDisplay['expires'])) ?> (2 hours)
      </div>
      <a href="<?= e($resetDisplay['url']) ?>" class="btn-fp d-block text-center text-decoration-none">
        <i class="bi bi-arrow-right-circle me-1"></i>Reset Password Now
      </a>
    </div>

    <?php else: ?>

    <p class="helper-text">
      Enter your username or email and we'll generate a secure password
      reset link valid for 2 hours.
    </p>

    <form method="POST" action="<?= e($appUrl) ?>/auth/forgot-password">
      <?= $csrfField ?>
      <div class="mb-4">
        <label class="form-label-modern" for="username">Username or Email</label>
        <div class="input-group-modern">
          <i class="bi bi-person input-leading"></i>
          <input type="text" name="username" id="username" class="input-modern"
                 placeholder="Enter your username or email"
                 autofocus required>
        </div>
      </div>
      <button type="submit" class="btn-fp">
        <i class="bi bi-send me-1"></i> Generate Reset Link
      </button>
    </form>

    <?php endif; ?>

  </div>
</div>

<a href="<?= e($appUrl) ?>/auth/login" class="back-link">
  <i class="bi bi-arrow-left"></i> Back to Login
</a>

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