<?php
include 'config.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $type = $_POST['type'] ?? '';

    $admin_username = $_POST['admin_username'] ?? '';
    $admin_password = $_POST['admin_password'] ?? '';

    // Prepared statement — username ব্যবহার করবে (email নয়)
    $admin_stmt = $conn->prepare("SELECT 1 FROM admin WHERE username = ? AND password = ?");
    $admin_stmt->bind_param("ss", $admin_username, $admin_password);
    $admin_stmt->execute();
    $admin_result = $admin_stmt->get_result();

    if ($admin_result->num_rows === 0) {
        http_response_code(403);
        echo json_encode([
            "status" => "error",
            "message" => "Access Denied"
        ], JSON_PRETTY_PRINT);
        $admin_stmt->close();
        $conn->close();
        exit();
    }
    $admin_stmt->close();

    if ($type === 'getdata') {
        $sql = "SELECT * FROM freefire";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            $rows = array();
            while ($row = $result->fetch_assoc()) {
                $rows[] = $row;
            }
            echo json_encode($rows);
        } else {
            echo "No Matches Found";
        }
    }

    if ($type === 'add') {
        $match_title = $_POST['match_title'] ?? '';
        $match_time = $_POST['match_time'] ?? '';
        $total_prize = $_POST['total_prize'] ?? '';
        $per_kill = $_POST['per_kill'] ?? '';
        $entry_fee = $_POST['entry_fee'] ?? '';
        $entry_type = $_POST['entry_type'] ?? '';
        $match_type = $_POST['match_type'] ?? '';
        $version = $_POST['version'] ?? '';
        $play_map = $_POST['play_map'] ?? '';
        $registration = $_POST['registration'] ?? '';
        $total_player = $_POST['total_player'] ?? '';
        $prize_details = $_POST['prize_details'] ?? '';
        $timer = $_POST['timer'] ?? '';
        $join_type = $_POST['join_type'] ?? ''; // ⚡ Safety check
        $image_link = $_POST['image_link'] ?? ''; // ⚡ নতুন image_link param
        
        // ⚡ নতুন অতিরিক্ত ৩টি ফিল্ড
        $locked = $_POST['locked'] ?? '';
        $locked_type = $_POST['locked_type'] ?? '';
        $player_name_change = $_POST['player_name_change'] ?? '';

        $room_id = "NO";
        $room_pass = "NO";
        $match_result = "NO";

        $sql = "INSERT INTO freefire (match_title, match_time, total_prize, per_kill, entry_fee, entry_type, match_type, version, play_map, registration, total_player, prize_details, room_id, room_pass, match_result, timer, join_type, image_link, locked, locked_type, player_name_change) VALUES ('$match_title', '$match_time', '$total_prize', '$per_kill', '$entry_fee', '$entry_type', '$match_type', '$version', '$play_map', '$registration', '$total_player', '$prize_details', '$room_id', '$room_pass', '$match_result', '$timer', '$join_type', '$image_link', '$locked', '$locked_type', '$player_name_change')";

        if ($conn->query($sql) === TRUE) {
            echo "Add Matches Successfully";
        } else {
            echo "Error Add Matches: " . $conn->error;
        }
    }

    if ($type === 'delete') {
        $id = $_POST['id'] ?? '';
        $sql = "DELETE FROM freefire WHERE id='$id'";

        if ($conn->query($sql) === TRUE) {
            echo "Match Deleted Successfully";
        } else {
            echo "Error Deleting Match: " . $conn->error;
        }
    }

    if ($type === 'move') {
        $result_value = $_POST['result'] ?? '';
        $id = $_POST['id'] ?? '';
        $sql = "UPDATE freefire SET match_result='$result_value' WHERE id='$id'";

        if ($conn->query($sql) === TRUE) {
            echo "Match Moved Successfully";
        } else {
            echo "Error Updating Record: " . $conn->error;
        }
    }

    if ($type === 'update') {
        $id = $_POST['id'] ?? '';
        $match_title = $_POST['match_title'] ?? '';
        $match_time = $_POST['match_time'] ?? '';
        $total_prize = $_POST['total_prize'] ?? '';
        $per_kill = $_POST['per_kill'] ?? '';
        $registration = $_POST['registration'] ?? '';
        $total_player = $_POST['total_player'] ?? '';
        $prize_details = $_POST['prize_details'] ?? '';
        $room_id = $_POST['room_id'] ?? '';
        $room_pass = $_POST['room_pass'] ?? '';
        $timer = $_POST['timer'] ?? '';
        $join_type = $_POST['join_type'] ?? ''; // ⚡ নতুন প্যারামিটার যোগ করা হয়েছে
        
        // ⚡ নতুন অতিরিক্ত ৩টি ফিল্ড
        $locked = $_POST['locked'] ?? '';
        $locked_type = $_POST['locked_type'] ?? '';
        $player_name_change = $_POST['player_name_change'] ?? '';

        // SQL Query তে নতুন ফিল্ডগুলো আপডেট করার লজিক যোগ করা হয়েছে
        $sql = "UPDATE freefire SET match_title='$match_title', match_time='$match_time', total_prize='$total_prize', per_kill='$per_kill', registration='$registration', total_player='$total_player', prize_details='$prize_details', room_id='$room_id', room_pass='$room_pass', timer='$timer', join_type='$join_type', locked='$locked', locked_type='$locked_type', player_name_change='$player_name_change' WHERE id='$id'";

        if (mysqli_query($conn, $sql)) {
            echo "Match Updated Successfully";
        } else {
            echo "Error Updating Match: " . mysqli_error($conn);
        }
    }

    $conn->close();
} else {
    echo "Invalid Request Method";
}
?>
