<?php
session_start();
require "dbconnect.php";

$msg = ""; // Store messages

// ✅ Check if form is submitted
if (isset($_POST['submit'])) {
    $email = mysqli_real_escape_string($con, $_POST['email']);
    $password = mysqli_real_escape_string($con, $_POST['password']);

    // ✅ Fetch User with `status = 1` (Active User Only)
    $sql = "SELECT * FROM user_register WHERE email = ? AND status = 1";
    $stmt = $con->prepare($sql);
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $res = $stmt->get_result();

    if ($res->num_rows > 0) {
        $row = $res->fetch_assoc();

        // ✅ Check if password matches using `password_verify()`
        if (password_verify($password, $row['password'])) {
            $_SESSION['loggedin'] = true;
            $_SESSION['USER_ID'] = $row['id'];
            $_SESSION['USER_NAME'] = $row['username'];
            $_SESSION['USER_EMAIL'] = $row['email'];

            session_regenerate_id(true); // Secure session fixation
            header("Location: dashboard.php");  // ✅ Redirect to dashboard
            exit();
        } else {
            $msg = "❌ Invalid Email or Password!";
        }
    } else {
        $msg = "❌ Account not found or not activated!";
    }
    $stmt->close();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - Chemtechie</title>
    <link rel="stylesheet" href="css/style.css"> <!-- ✅ CSS File -->
    
    <!-- ✅ Bootstrap 5 (For Alert Messages & Form Styling) -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
</head>
<body>
    <div class="login-container">
        <h2>Login to Your Account</h2>

        <!-- ✅ Error Message (Show only if $msg is not empty) -->
        <?php if (!empty($msg)): ?>
            <div class="alert alert-danger text-center">
                <?php echo htmlspecialchars($msg); ?>
            </div>
        <?php endif; ?>

        <form method="POST" action="login.php">
            <div class="form-group">
                <label for="email">Email:</label>
                <input type="email" name="email" class="form-control" id="email" required>
            </div>

            <div class="form-group">
                <label for="password">Password:</label>
                <input type="password" name="password" class="form-control" id="password" required>
            </div>

            <button type="submit" name="submit" class="btn btn-primary w-100">Login</button>
        </form>

        <h4 class="mt-3"><a href="registration.php" class="text-white">Don't have an account? Join Us</a></h4>
    </div>

    <!-- ✅ Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

    <!-- ✅ Custom Styling -->
    <style>
        body {
            background-image: url('img/reback.jpg');
            background-size: cover;
            background-position: center;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .login-container {
            background-color: rgba(0, 0, 0, 0.8);
            padding: 30px;
            width: 400px;
            border-radius: 20px;
            text-align: center;
            box-shadow: 0px 0px 15px rgba(255, 255, 255, 0.2);
            color: white;
        }
        .form-group input {
            border-radius: 5px;
            padding: 10px;
            font-size: 16px;
        }
        .btn-primary {
            background: linear-gradient(45deg, #007bff, #0056b3);
            border: none;
            font-size: 18px;
            padding: 10px 20px;
            border-radius: 25px;
            transition: 0.3s ease-in-out;
            width: 100%;
            color: white;
        }
        .btn-primary:hover {
            background: linear-gradient(45deg, #0056b3, #003366);
            transform: scale(1.05);
        }
    </style>
</body>
</html>
