<?php
// Start session
session_start();
include 'db.php'; // Database connection

$error = '';
$email = '';

// Success message from previous page
$success = $_SESSION['success'] ?? '';
unset($_SESSION['success']); // Remove after displaying

// Handle login form submission
if (isset($_POST['login'])) {
    $email = trim($_POST['email']);
    $password = $_POST['password'];

    // --- Validation ---
    if (empty($email)) {
        $error = "Email is required.";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = "Invalid email format.";
    } elseif (empty($password)) {
        $error = "Password is required.";
    } else {
        // Check user in database
        $stmt = $conn->prepare("SELECT * FROM admin WHERE email=? AND status=1");
        if ($stmt) {
            $stmt->bind_param("s", $email);
            $stmt->execute();
            $res = $stmt->get_result();

            if ($res->num_rows > 0) {
                $user = $res->fetch_assoc();
                
                // Plain-text password check
                if ($password === $user['password']) {
                    $_SESSION['user'] = $user;
                    $_SESSION['user_id'] = $user['id'];
                    header("Location: admdashboard.php");
                    exit;
                } else {
                    $error = "Invalid password.";
                }
            } else {
                $error = "User not found or inactive.";
            }
            $stmt->close();
        } else {
            $error = "Database error: " . $conn->error;
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ICMR-NIRRCH - Admin Login</title>
<link rel="icon" href="assets/img/logo.png" type="image/png">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">
<style>
body { 
    padding-top: 120px;
    font-family: "Noto Sans", sans-serif;
    background-color: #f8f9fa;
}
.card { border-radius: 0.5rem; }
.card-title { font-weight: bold; }
.btn-success { background-color: #1490d7; border-color: #1490d7; }
.btn-success:hover { background-color: #0f78c1; border-color: #0f78c1; }
a { text-decoration: none; }
a:hover { text-decoration: underline; }
</style>
</head>
<body>

<?php include '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-sm">
                <div class="card-body p-4">
                    <h3 class="card-title mb-4 text-center">Admin Login</h3>

                    <!-- Success Message -->
                    <?php if(!empty($success)): ?>
                        <div id="success-msg" class="alert alert-success"><?= htmlspecialchars($success) ?></div>
                    <?php endif; ?>

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

                    <form method="post" action="admin.php">
                        <div class="mb-3">
                            <input type="email" name="email" class="form-control" placeholder="Email" required
                                   value="<?= htmlspecialchars($email) ?>">
                        </div>
                        <div class="mb-3">
                            <input type="password" name="password" class="form-control" placeholder="Password" required>
                        </div>
                        <button type="submit" class="btn btn-success w-100" name="login">Login</button>
                    </form>
                    <p class="text-center mt-3"><a href="forgot-password.php">Forgot Password?</a></p>
                </div>
            </div>
        </div>
    </div>
</div>
</section>

<?php include 'footer.php'; ?>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>

<!-- Auto-hide success message after 5 seconds -->
<script>
const successMsg = document.getElementById('success-msg');
if (successMsg) {
    setTimeout(() => {
        successMsg.style.display = 'none';
    }, 5000);
}
</script>

</body>
</html>
