<?php
$tenant = service('tenantContext')->hasTenant() ? service('tenantContext')->getTenant() : null;
$primaryColor   = safe_hex_color($tenant->primary_color ?? null, '#4f46e5');
$secondaryColor = safe_hex_color($tenant->secondary_color ?? null, '#f59e0b');
$hotelName      = $tenant->hotel_name ?? 'MaxYields PMS';
$heroImage      = 'https://images.unsplash.com/photo-1758448500688-3ababa93fd67?auto=format&fit=crop&w=1400&q=80';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reset Password · <?= esc($hotelName) ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <?php include APPPATH . 'Views/auth/_auth_shared_styles.php'; ?>
</head>
<body>
<div class="login-shell d-flex">
    <div class="form-col col-lg-5 col-12 d-flex flex-column justify-content-center px-4 px-md-5 py-5 mx-auto">
        <div style="max-width: 380px; width: 100%; margin: 0 auto;">

            <div class="d-flex align-items-center gap-2 mb-4">
                <div class="brand-mark"><i class="bi bi-buildings-fill"></i></div>
                <div>
                    <div class="brand-name"><?= esc($hotelName) ?></div>
                    <div class="text-muted small">Hotel Property Management</div>
                </div>
            </div>

            <a href="<?= site_url('forgot-password') ?>" class="text-decoration-none small fw-semibold d-inline-flex align-items-center gap-1 mb-4" style="color: var(--brand);">
                <i class="bi bi-arrow-left"></i> Back
            </a>

            <div class="icon-circle mb-3"><i class="bi bi-shield-lock"></i></div>
            <h1 class="h3 fw-bold mb-2">Enter Reset Code</h1>
            <p class="text-muted mb-4">We texted a 6-digit code to your phone ending in <strong><?= esc($maskedPhone) ?></strong>. It expires in 10 minutes.</p>

            <?php if (session()->getFlashdata('error')): ?>
                <div class="alert alert-danger py-2 small"><?= esc(session()->getFlashdata('error')) ?></div>
            <?php endif; ?>
            <?php if (session()->getFlashdata('success')): ?>
                <div class="alert alert-success py-2 small"><?= esc(session()->getFlashdata('success')) ?></div>
            <?php endif; ?>

            <form action="<?= site_url('forgot-password/verify') ?>" method="post">
                <?= csrf_field() ?>
                <div class="mb-3">
                    <label class="form-label small fw-semibold" for="code">Reset Code</label>
                    <div class="input-icon">
                        <i class="bi bi-hash"></i>
                        <input type="text" inputmode="numeric" pattern="[0-9]{6}" maxlength="6" class="form-control form-control-lg" id="code" name="code"
                               placeholder="6-digit code" required autofocus style="letter-spacing: 0.3em;">
                    </div>
                </div>
                <div class="mb-3">
                    <label class="form-label small fw-semibold" for="password">New Password</label>
                    <div class="input-icon">
                        <i class="bi bi-lock"></i>
                        <input type="password" class="form-control form-control-lg" id="password" name="password" placeholder="At least 8 characters" minlength="8" required>
                        <i class="bi bi-eye toggle-eye" data-target="password" role="button"></i>
                    </div>
                </div>
                <div class="mb-3">
                    <label class="form-label small fw-semibold" for="password_confirm">Confirm New Password</label>
                    <div class="input-icon">
                        <i class="bi bi-lock"></i>
                        <input type="password" class="form-control form-control-lg" id="password_confirm" name="password_confirm" placeholder="Re-enter password" minlength="8" required>
                        <i class="bi bi-eye toggle-eye" data-target="password_confirm" role="button"></i>
                    </div>
                </div>

                <button type="submit" class="btn btn-brand btn-lg w-100 mt-2">
                    <i class="bi bi-check-circle me-1"></i> Reset Password
                </button>
            </form>

            <form action="<?= site_url('forgot-password/resend') ?>" method="post" class="text-center mt-3">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-link btn-sm text-decoration-none p-0">Didn't get a code? Resend it</button>
            </form>

            <p class="text-center text-muted small mt-5 mb-0">&copy; <?= date('Y') ?> <?= esc($hotelName) ?> · Powered by MaxYields PMS</p>
        </div>
    </div>

    <div class="hero-col col-lg-7 d-none d-lg-block">
        <div class="hero-overlay">
            <div class="mb-4">
                <div class="hero-headline mb-2">Almost there.</div>
                <div class="hero-sub">Enter the code we texted you and choose a new password to get back into your dashboard.</div>
            </div>

            <div class="trust-badge d-flex align-items-center gap-2">
                <i class="bi bi-shield-check fs-5"></i>
                <span>This code only works once and expires automatically — no need to worry if it sits unused.</span>
            </div>
        </div>
    </div>
</div>

<script>
document.querySelectorAll('.toggle-eye').forEach(function (icon) {
    icon.addEventListener('click', function () {
        const input = document.getElementById(this.dataset.target);
        const showing = input.type === 'text';
        input.type = showing ? 'password' : 'text';
        this.classList.toggle('bi-eye', showing);
        this.classList.toggle('bi-eye-slash', !showing);
    });
});
</script>
</body>
</html>
