<?php
session_start();
include '../config.php';

// ১. সিকিউরিটি চেক: ইউজার এডমিন কি না?
if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'admin') {
    header("Location: index.php");
    exit();
}

$msg = "";
$error = "";

// ২. টাকা অ্যাড করার লজিক
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $conn->real_escape_string($_POST['username']);
    $amount = intval($_POST['amount']);

    // চেক করা ইউজার আছে কি না
    $check_user = $conn->query("SELECT id FROM users WHERE username = '$username'");

    if ($check_user->num_rows > 0) {
        // টাকা আপডেট করা
        $sql = "UPDATE users SET balance = balance + $amount WHERE username = '$username'";
        if ($conn->query($sql) === TRUE) {
            $msg = "Success! $amount Tk added to user '$username'.";
        } else {
            $error = "Database Error!";
        }
    } else {
        $error = "User not found! Please check the username.";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add Money - Admin</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
</head>
<body class="bg-gray-900 text-white min-h-screen flex items-center justify-center p-4">

    <div class="w-full max-w-md bg-gray-800 rounded-xl shadow-2xl border border-gray-700 overflow-hidden">
        
        <!-- Header -->
        <div class="bg-gray-700 p-4 border-b border-gray-600 flex justify-between items-center">
            <h2 class="text-xl font-bold text-cyan-400"><i class="fas fa-wallet mr-2"></i> Add Balance</h2>
            <a href="panel.php" class="text-gray-400 hover:text-white text-sm"><i class="fas fa-arrow-left"></i> Back</a>
        </div>

        <div class="p-6">
            <!-- Notification Messages -->
            <?php if($msg): ?>
                <div class="bg-green-600/20 border border-green-500 text-green-400 p-3 rounded mb-4 text-center">
                    <i class="fas fa-check-circle"></i> <?php echo $msg; ?>
                </div>
            <?php endif; ?>

            <?php if($error): ?>
                <div class="bg-red-600/20 border border-red-500 text-red-400 p-3 rounded mb-4 text-center">
                    <i class="fas fa-exclamation-circle"></i> <?php echo $error; ?>
                </div>
            <?php endif; ?>

            <!-- Form -->
            <form method="POST" class="space-y-5">
                
                <div>
                    <label class="block text-sm text-gray-400 mb-2">Target Username</label>
                    <div class="relative">
                        <span class="absolute left-3 top-3 text-gray-500"><i class="fas fa-user"></i></span>
                        <input type="text" name="username" placeholder="Enter player username" required 
                            class="w-full pl-10 pr-4 py-3 bg-gray-900 border border-gray-600 rounded text-white focus:border-cyan-400 focus:outline-none focus:ring-1 focus:ring-cyan-400 transition">
                    </div>
                </div>

                <div>
                    <label class="block text-sm text-gray-400 mb-2">Amount (Tk)</label>
                    <div class="relative">
                        <span class="absolute left-3 top-3 text-gray-500"><i class="fas fa-money-bill-wave"></i></span>
                        <input type="number" name="amount" placeholder="e.g. 500" required min="1"
                            class="w-full pl-10 pr-4 py-3 bg-gray-900 border border-gray-600 rounded text-white focus:border-cyan-400 focus:outline-none focus:ring-1 focus:ring-cyan-400 transition">
                    </div>
                </div>

                <button type="submit" class="w-full bg-cyan-600 hover:bg-cyan-500 text-white font-bold py-3 rounded shadow-lg transform active:scale-95 transition">
                    CONFIRM TRANSFER <i class="fas fa-paper-plane ml-2"></i>
                </button>

            </form>
        </div>
        
        <!-- Footer Info -->
        <div class="bg-gray-900 p-4 text-center text-xs text-gray-500 border-t border-gray-700">
            Ensure username is correct before sending.
        </div>
    </div>

</body>
</html>