<?php
// --- TAMIM 1280 MASTER FIX START (অটোমেটিক ডিরেক্টরি ডিটেকশন) ---
$sessionPath = __DIR__ . '/sessions'; 
if (!file_exists($sessionPath)) { 
    @mkdir($sessionPath, 0755, true); 
}
if (is_writable($sessionPath)) {
    ini_set('session.save_path', $sessionPath);
}
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
// --- TAMIM 1280 MASTER FIX END ---

include 'db.php';

// যদি অলরেডি লগইন করা থাকে তবে ইনডেক্সে পাঠিয়ে দেবে
if (isset($_SESSION['user_id'])) {
    header("Location: index.php");
    exit();
}

$error = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $conn->real_escape_string($_POST['username']);
    $password = $_POST['password'];

    $sql = "SELECT * FROM admin_users WHERE username = '$username'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        // পাসওয়ার্ড চেক করা
        if (password_verify($password, $row['password'])) {
            $_SESSION['user_id'] = $row['id'];
            $_SESSION['username'] = $row['username'];
            header("Location: index.php");
            exit();
        } else {
            $error = "ACCESS DENIED: WRONG PASSWORD";
        }
    } else {
        $error = "USER NOT FOUND";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>System Login | Secured</title>
    <link rel="stylesheet" href="css/style.css?v=<?php echo time(); ?>">
    <style>
        .error-msg {
            color: #ff3333;
            background: rgba(255, 0, 0, 0.1);
            border: 1px solid #ff3333;
            padding: 10px;
            margin-bottom: 15px;
            font-size: 14px;
            text-transform: uppercase;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="container" style="max-width: 400px;">
        <div class="logo-area">
            <img src="assets/logo.png" alt="SYSTEM" onerror="this.style.display='none'">
        </div>

        <h1>👑 TAMIM 1280 👑</h1>

        <?php if($error): ?>
            <div class="error-msg">⚠️ <?php echo $error; ?></div>
        <?php endif; ?>

        <form action="" method="POST">
            <label>Hunter Name (User)</label>
            <input type="text" name="username" placeholder="ENTER USERNAME" required autocomplete="off">

            <label>Security Code (Pass)</label>
            <input type="password" name="password" placeholder="ENTER PASSWORD" required>

            <button type="submit">LOGIN TO SYSTEM</button>
        </form>
    </div>
</body>
</html>