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

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

// 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 register table (plain-text password)
        $stmt = $conn->prepare("SELECT * FROM register 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']) {
                    // Login successful
                    $_SESSION['user'] = $user;
                    $_SESSION['user_id'] = $user['id'];
                    header("Location: dashboard.php"); // redirect to user dashboard
                    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>User Login - ICMR-NIRRCH</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.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; }
</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">User Login</h3>

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

                    <form method="post" action="login.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="mt-3 text-center">
                        Don't have an account? <a href="register.php">Register here</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>
</body>
</html>
