<?php
// --- CUSTOM SESSION PATH FIX START (লগইন লুপ এবং /tmp এরর সমাধান) ---
$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();
}
// --- CUSTOM SESSION PATH FIX END ---

include 'config.php';

// ১. অ্যাডমিন কি না তা চেক করা
if (!isset($_SESSION['email']) || $_SESSION['email'] !== 'tamimhasan1281@gmail.com') {
    header("Location: index.php"); // অ্যাডমিন না হলে হোমপেজে পাঠাবে
    exit();
}

$message = "";

// ইউজার অ্যাড/রিফিল
if(isset($_POST['add_user'])) {
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $pass = password_hash($_POST['pass'], PASSWORD_DEFAULT);
    $tokens = (int)$_POST['tokens'];
    
    $check = $conn->query("SELECT id FROM users WHERE email='$email'");
    if($check->num_rows > 0) {
        $conn->query("UPDATE users SET tokens = tokens + $tokens WHERE email='$email'");
        $message = "<span style='color: #00eaff;'>[SUCCESS] Tokens Added.</span>";
    } else {
        $conn->query("INSERT INTO users (email, password, tokens) VALUES ('$email', '$pass', $tokens)");
        $message = "<span style='color: #00eaff;'>[SUCCESS] User Authorized.</span>";
    }
}

// টোকেন সরাসরি পরিবর্তন
if(isset($_POST['update_token_direct'])) {
    $user_id = (int)$_POST['user_id'];
    $new_tokens = (int)$_POST['new_tokens'];
    $conn->query("UPDATE users SET tokens = $new_tokens WHERE id=$user_id");
    $message = "<span style='color: #00eaff;'>[UPDATED] Token Modified.</span>";
}

// ইউজার ডিলিট (অ্যাডমিন প্রোটেকশনসহ)
if(isset($_POST['delete_user'])) {
    $user_id = (int)$_POST['user_id'];
    // চেক করা যে ডিলিট করা ইউজার অ্যাডমিন কি না
    $target = $conn->query("SELECT email FROM users WHERE id=$user_id")->fetch_assoc();
    if($target['email'] === 'tamimhasan1281@gmail.com') {
        $message = "<span style='color: #ff3333;'>[DENIED] System Admin cannot be removed!</span>";
    } else {
        $conn->query("DELETE FROM users WHERE id=$user_id");
        $message = "<span style='color: #ff3333;'>[TERMINATED] User Access Revoked.</span>";
    }
}

$users_list = $conn->query("SELECT * FROM users ORDER BY id DESC");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Admin TAMIM 1280</title>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Orbitron&family=Rajdhani:wght@500;700&display=swap">
    <style>
        :root { --primary: #00eaff; --secondary: #0051ff; --danger: #ff3333; --bg: #050505; }
        body { background: var(--bg); color: white; font-family: 'Rajdhani', sans-serif; display: flex; justify-content: center; align-items: flex-start; min-height: 100vh; padding: 20px; }
        .container { width: 100%; max-width: 600px; background: rgba(10,20,30,0.9); border: 1px solid var(--secondary); border-top: 2px solid var(--primary); padding: 30px; border-radius: 10px; text-align: center; }
        h2 { font-family: 'Orbitron'; color: #fff; font-size: 18px; margin-bottom: 25px; }
        input { width: 100%; padding: 10px; background: #000; border: 1px solid #333; border-left: 3px solid var(--primary); color: #fff; margin-bottom: 10px; outline:none; }
        .main-btn { width: 100%; padding: 12px; background: transparent; border: 1px solid var(--primary); color: var(--primary); font-family: 'Orbitron'; cursor: pointer; }
        .user-table { width: 100%; border-collapse: collapse; margin-top: 30px; text-align: left; }
        .user-table th { color: #555; font-size: 11px; border-bottom: 1px solid #222; padding: 10px; }
        .user-table td { padding: 12px 10px; border-bottom: 1px solid #111; font-size: 14px; }
        .del-btn { color: var(--danger); background: none; border: 1px solid var(--danger); font-size: 9px; padding: 4px 8px; cursor: pointer; font-family: 'Orbitron'; }
        .token-box { width: 60px; background: #000; color: #00eaff; border: 1px solid #333; padding: 3px; text-align: center; }
        .update-btn { background: var(--secondary); border:none; color:white; font-size: 9px; padding: 4px; cursor: pointer; }
    </style>
</head>
<body>
    <div class="container">
        <h2>TAMIM 1280 ADMIN [SECURED]</h2>
        <form method="POST">
            <input type="email" name="email" placeholder="Player Email" required>
            <input type="text" name="pass" placeholder="Password (For New User)" required>
            <input type="number" name="tokens" placeholder="Tokens" required>
            <button type="submit" name="add_user" class="main-btn">AUTHORIZE PLAYER</button>
        </form>

        <div style="margin: 15px 0;"><?php echo $message; ?></div>

        <table class="user-table">
            <thead>
                <tr>
                    <th>PLAYER EMAIL</th>
                    <th>TOKENS</th>
                    <th>ACTION</th>
                </tr>
            </thead>
            <tbody>
                <?php while($user = $users_list->fetch_assoc()): ?>
                <tr>
                    <td style="font-size: 12px;"><?php echo $user['email']; ?></td>
                    <td>
                        <form method="POST" style="display:flex; align-items:center;">
                            <input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
                            <input type="number" name="new_tokens" class="token-box" value="<?php echo $user['tokens']; ?>">
                            <button type="submit" name="update_token_direct" class="update-btn">SET</button>
                        </form>
                    </td>
                    <td>
                        <?php if($user['email'] !== 'tamimhasan1281@gmail.com'): ?>
                        <form method="POST" onsubmit="return confirm('Terminate Access?');">
                            <input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
                            <button type="submit" name="delete_user" class="del-btn">DEL</button>
                        </form>
                        <?php else: ?>
                            <span style="color:var(--primary); font-size:9px; font-family:'Orbitron';">SYSTEM ADMIN</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
        <br><a href="index.php" style="color:#555; text-decoration:none; font-size:11px;">« BACK TO INTERFACE</a>
    </div>
</body>
</html>