<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();
include 'config.php';

$msg = '';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $email = trim($_POST["email"]);
    $password = trim($_POST["password"]);

    // Registration table से user लाओ
    $sql = "SELECT id, user_id, name, email, conf_password, Status 
            FROM Registration 
            WHERE email = ?";
    $stmt = $link->prepare($sql);

    if ($stmt) {
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result && $result->num_rows === 1) {
            $user = $result->fetch_assoc();

            // ✅ Plain text password check
            if ($password === $user['conf_password']) {
                if (strtolower($user['Status']) === "active") {
                    // Session create
                    $_SESSION['id']      = $user['id'];
                    $_SESSION['user_id'] = $user['user_id'];
                    $_SESSION['name']    = $user['name'];
                    $_SESSION['email']   = $user['email'];

                    header("Location: dashboard.php");
                    exit();
                } else {
                    $msg = "⚠️ Your account is under review. Please wait for admin approval.";
                }
            } else {
                $msg = "❌ Invalid password.";
            }
        } else {
            $msg = "❌ User not found.";
        }

        $stmt->close();
    } else {
        $msg = "❌ Database error: " . $link->error;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<?php include 'head.php'; ?>
<body class="hold-transition theme-primary bg-img" style="background-image: url(../images/auth-bg/bg-9.jpg)">

<div class="container h-p100">
    <div class="row align-items-center justify-content-md-center h-p100">
        <div class="col-12">
            <div class="row justify-content-center g-0">
                <div class="col-lg-5 col-md-6 col-12">
                    <div class="bg-white rounded10 shadow-lg">
                        <div class="content-top-agile p-20 pb-0">
                            <h2 class="text-primary">Welcome Back</h2>
                            <p class="mb-0">Sign in to continue</p>
                        </div>
                        <div class="p-40">
                            <?php if ($msg != ''): ?>
                                <div style="color:red; margin-bottom:15px;"><?php echo $msg; ?></div>
                            <?php endif; ?>
                            <form action="" method="post">
                                <div class="form-group">
                                    <div class="input-group mb-3">
                                        <span class="input-group-text bg-transparent"><i class="ti-email"></i></span>
                                        <input type="email" class="form-control ps-15 bg-transparent" name="email" placeholder="Email" required>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="input-group mb-3">
                                        <span class="input-group-text bg-transparent"><i class="ti-lock"></i></span>
                                        <input type="password" class="form-control ps-15 bg-transparent" name="password" placeholder="Password" required>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-12 text-center">
                                        <button type="submit" class="btn btn-success mt-10">SIGN IN</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
</body>
</html>
