<?php
require_once __DIR__ . '/includes/auth.php';
if (isLoggedIn()) { redirectToDashboard(); }
$error_message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    $conn = getDBConnection();
    $stmt = $conn->prepare('SELECT id, name, password, user_type, account_status FROM users WHERE email = ?');
    $stmt->bind_param('s', $email);
    $stmt->execute();
    $result = $stmt->get_result();
    if ($result->num_rows === 1) {
        $user = $result->fetch_assoc();
        if (($user['account_status'] ?? 'active') !== 'active') {
            $error_message = 'This account is inactive. Please contact the administrator.';
        } elseif (password_verify($password, $user['password'])) {
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['user_name'] = $user['name'];
            $_SESSION['user_type'] = $user['user_type'];
            $_SESSION['account_status'] = $user['account_status'];
            $stmt->close();
            $conn->close();
            redirectToDashboard();
        } else {
            $error_message = 'Invalid password.';
        }
    } else {
        $error_message = 'No account found with this email.';
    }
    $stmt->close();
    $conn->close();
}
?>
<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><meta name='viewport' content='width=device-width, initial-scale=1.0'><title>Login - Oldage Home</title><link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'><link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css'><link href='https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap' rel='stylesheet'><link rel='stylesheet' href='css/style.css'></head><body class="auth-page"><?php include 'includes/header.php'; ?><section class='py-5'><div class='container'><div class='row justify-content-center'><div class='col-md-6 col-lg-5'><div class='card shadow'><div class='card-body p-5'><h2 class='text-center mb-4'>Login to Your Account</h2><?php if ($error_message !== ''): ?><div class='alert alert-danger'><?php echo htmlspecialchars($error_message); ?></div><?php endif; ?><form method='post' action='<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>'><div class='mb-3'><label for='email' class='form-label'>Email Address</label><input type='email' class='form-control' id='email' name='email' required></div><div class='mb-3'><label for='password' class='form-label'>Password</label><input type='password' class='form-control' id='password' name='password' required></div><div class='d-grid'><button type='submit' class='btn btn-primary'>Login</button></div></form><div class='text-center mt-3'><p>Don't have an account? <a href='register.php'>Register here</a></p><p class='small text-muted mb-0'>Admins use the same login form and will be redirected to the admin panel automatically.</p></div></div></div></div></div></div></section><?php include 'includes/footer.php'; ?><script src='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js'></script><script src='js/main.js'></script></body></html>

