<?php
/**
 * admin/login.php
 * Admin login page - only password required.
 */

require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

$error = '';

// If already logged in, redirect to register page
if (isLoggedIn()) {
    header('Location: register.php');
    exit;
}

// Handle login form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['password'])) {
    $password = trim($_POST['password']);
    if ($password === ADMIN_PASSWORD) {
        $_SESSION['admin_logged_in'] = true;
        header('Location: register.php');
        exit;
    } else {
        $error = 'Incorrect Password';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Login · Emehama Village</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <link rel="stylesheet" href="../assets/css/login.css">
    <link rel="shortcut icon" type="image/x-icon" href="../images/ChatGPT%20Image%20Jul%2013,%202026,%2007_24_33%20PM.png" />
</head>
<body>
    <div class="login-container">
        <div class="login-card">
            <div class="login-header">
                <div class="logo-icon">
                    <img style="width: 40px; height: 40px; border-radius: 50%; " src="../images/ChatGPT%20Image%20Jul%2013,%202026,%2007_24_33%20PM.png" alt="Emehama Village Community Logo">
                </div>
                <h1>Emehama Village</h1>
                <p class="welcome-text">Community Admin Portal</p>
            </div>

            <?php if ($error): ?>
                <div class="error-message" id="errorMsg">
                    <i class="fas fa-exclamation-circle"></i>
                    <?php echo htmlspecialchars($error); ?>
                </div>
            <?php endif; ?>

            <form action="" method="POST" class="login-form" id="loginForm">
                <div class="form-group">
                    <label for="password">Enter Password</label>
                    <div class="password-wrapper">
                        <input type="password" id="password" name="password" placeholder="Your admin password" required autofocus>
                        <button type="button" id="togglePassword" class="toggle-btn" aria-label="Show password">
                            <i class="fas fa-eye"></i>
                        </button>
                    </div>
                </div>
                <button type="submit" class="btn-login">
                    <span>Sign In</span>
                    <i class="fas fa-arrow-right"></i>
                </button>
            </form>

            <div class="login-footer">
                <p>&copy; <?php echo date('Y'); ?> Emehama Village Community. All rights reserved.</p>
            </div>
        </div>
    </div>

    <script src="../assets/js/login.js"></script>
</body>
</html>