<?php
$tenant = service('tenantContext')->hasTenant() ? service('tenantContext')->getTenant() : null;
// safe_hex_color(), not esc(..., 'css') — see app/Helpers/theme_helper.php
// for why the escaped version silently broke every branded color on this
// page (var(--brand) resolved to nothing, background-color fell back to
// transparent — white text on a white button, invisible).
$primaryColor   = safe_hex_color($tenant->primary_color ?? null, '#4f46e5');
$secondaryColor = safe_hex_color($tenant->secondary_color ?? null, '#f59e0b');
$hotelName      = $tenant->hotel_name ?? 'MaxYields PMS';

// A real property's own photography belongs here via tenant branding once
// an upload flow exists (see PHASE3-NOTES.md) — this Unsplash photo is a
// placeholder so the page isn't a blank panel until then. Hardcoded
// literal in this file, not tenant input, so no escaping is needed to
// use it directly in the background: url(...) below.
$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>Log In · <?= 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>

            <h1 class="h3 fw-bold mb-1">Welcome back <span aria-hidden="true">👋</span></h1>
            <p class="text-muted mb-4">Sign in to access your front desk dashboard.</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; ?>
            <?php if (isset($validation)): ?>
                <div class="alert alert-danger py-2 small"><?= $validation->listErrors() ?></div>
            <?php endif; ?>

            <form action="<?= site_url('login') ?>" method="post">
                <?= csrf_field() ?>
                <div class="mb-3">
                    <label class="form-label small fw-semibold" for="email">Email</label>
                    <div class="input-icon">
                        <i class="bi bi-envelope"></i>
                        <input type="email" class="form-control form-control-lg" id="email" name="email"
                               value="<?= esc(old('email')) ?>" placeholder="you@hotel.com" required autofocus>
                    </div>
                </div>
                <div class="mb-3">
                    <label class="form-label small fw-semibold" for="password">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="Enter your password" required>
                        <i class="bi bi-eye toggle-eye" id="togglePassword" role="button" aria-label="Show password"></i>
                    </div>
                </div>

                <button type="submit" class="btn btn-brand btn-lg w-100 mt-2">
                    <i class="bi bi-box-arrow-in-right me-1"></i> Sign In
                </button>
            </form>

            <p class="text-muted small mt-3 mb-0 text-center">
                <a href="<?= site_url('forgot-password') ?>" class="text-decoration-none fw-semibold" style="color: var(--brand);">Forgot your password?</a>
            </p>
            <p class="text-muted small mt-3 mb-0 text-center">
                New staff member? Ask your manager to create your account.
            </p>

            <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">Run your whole property<br>from one screen.</div>
                <div class="hero-sub">Reservations, front desk, guest CRM, restaurant, and payments — built for independent hotels in Ghana.</div>
            </div>

            <div class="row row-cols-4 g-2 mb-4">
                <div class="col">
                    <div class="feature-card h-100">
                        <i class="bi bi-calendar-check"></i>
                        <p class="title">Reservations</p>
                        <p class="desc">Live booking calendar</p>
                    </div>
                </div>
                <div class="col">
                    <div class="feature-card h-100">
                        <i class="bi bi-door-open"></i>
                        <p class="title">Front Desk</p>
                        <p class="desc">Check-in &amp; folios</p>
                    </div>
                </div>
                <div class="col">
                    <div class="feature-card h-100">
                        <i class="bi bi-people"></i>
                        <p class="title">Guest CRM</p>
                        <p class="desc">Full stay history</p>
                    </div>
                </div>
                <div class="col">
                    <div class="feature-card h-100">
                        <i class="bi bi-graph-up-arrow"></i>
                        <p class="title">Payments</p>
                        <p class="desc">Hubtel &amp; invoicing</p>
                    </div>
                </div>
            </div>

            <div class="trust-badge d-flex align-items-center gap-2">
                <i class="bi bi-shield-check fs-5"></i>
                <span>Your data lives in <?= esc($hotelName) ?>'s own private database — never shared with other properties.</span>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
document.getElementById('togglePassword').addEventListener('click', function () {
    const pw = document.getElementById('password');
    const showing = pw.type === 'text';
    pw.type = showing ? 'password' : 'text';
    this.classList.toggle('bi-eye', showing);
    this.classList.toggle('bi-eye-slash', !showing);
});
</script>
</body>
</html>
