<?php
/**
 * Cincinnati Fire Department Portal - Login Page
 */
require_once __DIR__ . '/config/auth.php';

startSession();

// Already logged in? Go to dashboard
if (!empty($_SESSION['authenticated'])) {
    header('Location: dashboard.php');
    exit;
}

$error = '';
$expired = isset($_GET['expired']);

// Handle login submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    validateCsrf();
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';

    if ($username === '' || $password === '') {
        $error = 'Please enter both username and password.';
    } else {
        $result = attemptLogin($username, $password);
        if (isset($result['success'])) {
            header('Location: dashboard.php');
            exit;
        } else {
            $error = $result['error'];
        }
    }
}

$csrf = generateCsrfToken();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sign In — Cincinnati Fire Department</title>
    <link rel="icon" type="image/x-icon" href="favicon.ico">
    <link rel="icon" type="image/png" sizes="32x32" href="assets/img/favicon-32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="assets/img/favicon-16.png">
    <link rel="apple-touch-icon" sizes="180x180" href="assets/img/apple-touch-icon.png">
    <style>
        *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", system-ui, sans-serif;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
            -webkit-font-smoothing: antialiased;
            background: #0B2240;
            position: relative;
            overflow: hidden;
        }

        /* Full-bleed background */
        body::before {
            content: '';
            position: fixed;
            inset: 0;
            background: url('assets/img/login-bg.jpg') center center / cover no-repeat;
            z-index: 0;
        }

        /* Dark gradient overlay for readability */
        body::after {
            content: '';
            position: fixed;
            inset: 0;
            background: linear-gradient(
                160deg,
                rgba(11, 34, 64, 0.82) 0%,
                rgba(11, 34, 64, 0.65) 40%,
                rgba(11, 34, 64, 0.75) 100%
            );
            z-index: 1;
        }

        .login-container {
            width: 100%;
            max-width: 420px;
            position: relative;
            z-index: 2;
        }

        .login-card {
            background: rgba(255, 255, 255, 0.12);
            backdrop-filter: blur(24px) saturate(180%);
            -webkit-backdrop-filter: blur(24px) saturate(180%);
            border-radius: 20px;
            border: 1px solid rgba(255, 255, 255, 0.18);
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3), 0 0 0 1px rgba(255,255,255,0.05) inset;
            padding: 44px 36px;
            text-align: center;
        }

        .logo-section {
            margin-bottom: 28px;
        }

        .logo-section img.cfd-logo {
            height: 120px;
            width: auto;
            object-fit: contain;
            margin-bottom: 16px;
        }

        .logo-section .cfd-logo-fallback {
            width: 90px;
            height: 90px;
            background: rgba(255,255,255,0.15);
            border: 2px solid rgba(255,255,255,0.3);
            border-radius: 50%;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            color: #fff;
            font-size: 26px;
            font-weight: 800;
            letter-spacing: 2px;
            margin-bottom: 16px;
        }

        .logo-section h1 {
            font-size: 22px;
            font-weight: 700;
            color: #FFFFFF;
            line-height: 1.3;
            text-shadow: 0 1px 3px rgba(0,0,0,0.2);
        }

        .logo-section .subtitle {
            font-size: 14px;
            color: rgba(255, 255, 255, 0.6);
            margin-top: 4px;
            font-weight: 500;
            letter-spacing: 0.5px;
        }


        .alert {
            padding: 12px 16px;
            border-radius: 10px;
            font-size: 14px;
            margin-bottom: 20px;
            text-align: left;
        }

        .alert-error {
            background: rgba(220, 38, 38, 0.2);
            color: #FCA5A5;
            border: 1px solid rgba(220, 38, 38, 0.3);
        }

        .alert-warning {
            background: rgba(255, 149, 0, 0.2);
            color: #FDE68A;
            border: 1px solid rgba(255, 149, 0, 0.3);
        }

        .form-group {
            margin-bottom: 16px;
            text-align: left;
        }

        .form-group label {
            display: block;
            font-size: 13px;
            font-weight: 600;
            color: rgba(255, 255, 255, 0.7);
            margin-bottom: 6px;
        }

        .form-group input {
            width: 100%;
            padding: 13px 16px;
            font-size: 16px;
            border: 1px solid rgba(255, 255, 255, 0.15);
            border-radius: 12px;
            background: rgba(255, 255, 255, 0.08);
            color: #FFFFFF;
            outline: none;
            transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
            font-family: inherit;
        }

        .form-group input:focus {
            border-color: rgba(140, 198, 63, 0.6);
            box-shadow: 0 0 0 3px rgba(140, 198, 63, 0.15);
            background: rgba(255, 255, 255, 0.12);
        }

        .form-group input::placeholder {
            color: rgba(255, 255, 255, 0.3);
        }

        .btn-login {
            width: 100%;
            padding: 14px;
            font-size: 16px;
            font-weight: 600;
            background: linear-gradient(135deg, #1B6CB0 0%, #8CC63F 100%);
            color: #FFFFFF;
            border: none;
            border-radius: 12px;
            cursor: pointer;
            transition: opacity 0.2s, transform 0.1s;
            font-family: inherit;
            margin-top: 8px;
            text-shadow: 0 1px 2px rgba(0,0,0,0.2);
            box-shadow: 0 4px 15px rgba(27, 108, 176, 0.3);
        }

        .btn-login:hover {
            opacity: 0.9;
            box-shadow: 0 6px 20px rgba(27, 108, 176, 0.4);
        }

        .btn-login:active {
            transform: scale(0.98);
        }

        .register-link {
            margin-top: 20px;
            font-size: 14px;
            color: rgba(255, 255, 255, 0.5);
        }

        .register-link a {
            color: #8CC63F;
            text-decoration: none;
            font-weight: 600;
        }

        .register-link a:hover {
            text-decoration: underline;
            color: #A4D65A;
        }

        .footer-text {
            margin-top: 24px;
            font-size: 12px;
            color: rgba(255, 255, 255, 0.35);
        }

        @media (max-width: 480px) {
            .login-card {
                padding: 32px 24px;
                border-radius: 16px;
            }
            body { padding: 16px; }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-card">
            <div class="logo-section">
                <img src="assets/img/cfd-logo.png" alt="Cincinnati Fire Department" class="cfd-logo">
                <h1>Cincinnati Fire Department</h1>
                <p class="subtitle">Staff Portal</p>
            </div>

            <?php if ($expired): ?>
                <div class="alert alert-warning">Your session has expired. Please sign in again.</div>
            <?php endif; ?>

            <?php if ($error): ?>
                <div class="alert alert-error"><?= htmlspecialchars($error) ?></div>
            <?php endif; ?>

            <form method="POST" action="index.php" autocomplete="on">
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrf) ?>">

                <div class="form-group">
                    <label for="username">Username</label>
                    <input type="text" id="username" name="username" placeholder="Enter your username"
                           autocomplete="username" required autofocus
                           value="<?= htmlspecialchars($_POST['username'] ?? '') ?>">
                </div>

                <div class="form-group">
                    <label for="password">Password</label>
                    <input type="password" id="password" name="password" placeholder="Enter your password"
                           autocomplete="current-password" required>
                </div>

                <button type="submit" class="btn-login">Sign In</button>
            </form>

            <p class="register-link">
                Need an account? <a href="register.php">Request Access</a>
            </p>

            <img src="assets/img/city-logo.png" alt="City of Cincinnati" style="height:40px; width:auto; margin-top:20px; opacity:0.5; filter:brightness(0) invert(1);">
        </div>

        <div class="login-footer">
            <div class="login-footer-top">
                <a href="#">Privacy Policy</a>
                <span class="login-footer-dot"></span>
                <a href="#">Terms of Use</a>
                <span class="login-footer-dot"></span>
                <a href="#">Accessibility</a>
            </div>
            <div class="login-footer-copy">
                &copy; <?= date('Y') ?> City of Cincinnati &middot; All rights reserved
            </div>
            <div class="login-footer-credit">
                Designed &amp; developed by <strong>Bob Hug</strong> &middot; Powered by <strong>ResponseIQ</strong>
            </div>
        </div>
    </div>

    <style>
    .login-footer {
        text-align: center;
        margin-top: 20px;
        padding-top: 16px;
    }
    .login-footer-top {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        margin-bottom: 10px;
    }
    .login-footer-top a {
        color: rgba(255,255,255,0.45);
        font-size: 11px;
        font-weight: 500;
        text-decoration: none;
        letter-spacing: 0.2px;
        transition: color 0.2s;
    }
    .login-footer-top a:hover { color: rgba(255,255,255,0.8); }
    .login-footer-dot {
        width: 3px;
        height: 3px;
        border-radius: 50%;
        background: rgba(255,255,255,0.2);
    }
    .login-footer-copy {
        font-size: 11px;
        color: rgba(255,255,255,0.3);
        margin-bottom: 4px;
    }
    .login-footer-credit {
        font-size: 10px;
        color: rgba(255,255,255,0.2);
        letter-spacing: 0.2px;
    }
    .login-footer-credit strong {
        color: rgba(255,255,255,0.35);
        font-weight: 600;
    }
    </style>
</body>
</html>
