<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

$config_loaded = false;
$config_paths = ['Config.php', 'config.php', '../Config.php', '../config.php'];
foreach ($config_paths as $path) {
    if (file_exists($path)) {
        require_once $path;
        $config_loaded = true;
        break;
    }
}

if (!$config_loaded) {
    die("<div style='background:#ff4d4d; color:#fff; padding:20px; text-align:center; border-radius:12px; margin:50px auto; max-width:600px;'><h2>Configuration File Missing!</h2></div>");
}

if (!isset($conn) && isset($con)) {
    $conn = $con;
}

$error = false;

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['login_submit'])) {
    $user = trim($_POST['username'] ?? '');
    $pass = trim($_POST['password'] ?? '');

    $stmt = $conn->prepare("SELECT * FROM modarator_permissions WHERE username=? AND password=? LIMIT 1");
    if ($stmt) {
        $stmt->bind_param("ss", $user, $pass);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result && $result->num_rows > 0) {
            $row = $result->fetch_assoc();

            $_SESSION['admin_logged_in'] = true;
            $_SESSION['admin_user'] = $row['username'];
            $_SESSION['admin_pass'] = $row['password'];
            $_SESSION['permissions'] = $row;

            header("Location: index.php");
            exit;
        } else {
            $error = true;
        }

        $stmt->close();
    } else {
        $error = true;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Moderator Login</title>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        body { margin: 0; font-family: 'Plus Jakarta Sans', sans-serif; background-color: #f3f6fc; display: flex; justify-content: center; align-items: center; height: 100vh; }
        .login-box { background: #fff; padding: 40px; border-radius: 24px; box-shadow: 0 10px 30px rgba(0,0,0,0.06); width: 100%; max-width: 400px; text-align: center; border: 1px solid #e8eef6; }
        .login-box h2 { margin: 0 0 10px; color: #0f172a; font-weight: 800; }
        .login-box p { color: #64748b; font-size: 14px; margin-bottom: 30px; font-weight: 500; }
        .input-group { text-align: left; margin-bottom: 20px; }
        .input-group label { display: block; font-size: 13px; font-weight: 700; color: #0f172a; margin-bottom: 8px; }
        .input-group input { width: 100%; padding: 14px; border: 1px solid #e8eef6; border-radius: 12px; font-size: 15px; font-family: inherit; box-sizing: border-box; }
        .input-group input:focus { border-color: #4f46e5; outline: none; }
        .btn-login { width: 100%; padding: 14px; background: linear-gradient(135deg, #4f46e5 0%, #3b82f6 100%); color: #fff; border: none; border-radius: 12px; font-size: 15px; font-weight: 700; cursor: pointer; }
        .error-msg { background: rgba(239, 68, 68, 0.1); color: #ef4444; padding: 12px; border-radius: 12px; font-size: 13px; font-weight: 600; margin-bottom: 20px; text-align: left; }
    </style>
</head>
<body>
    <div class="login-box">
        <div style="width:48px; height:48px; background:#4f46e5; color:#fff; border-radius:12px; display:flex; justify-content:center; align-items:center; font-size:20px; margin:0 auto 20px;">
            <i class="fa-solid fa-server"></i>
        </div>
        <h2>Moderator Access</h2>
        <p>Enter credentials to open your workspace</p>

        <?php if ($error): ?>
            <div class="error-msg"><i class="fa-solid fa-circle-xmark"></i> Invalid Username or Password!</div>
        <?php endif; ?>

        <form action="admin_login.php" method="POST">
            <div class="input-group">
                <label>Username</label>
                <input type="text" name="username" required autocomplete="off">
            </div>
            <div class="input-group">
                <label>Password</label>
                <input type="password" name="password" required>
            </div>
            <button type="submit" name="login_submit" class="btn-login">Unlock Workspace</button>
        </form>
    </div>
</body>
</html>