<?php

require_once "config.php";

header('Content-Type: application/json');

$method = $_SERVER['REQUEST_METHOD'];

error_log("Received method: " . $method);
if ($method === 'GET') {
    $cusename = $_GET['cusername'] ?? '';

    $query = $conn->prepare(
        "SELECT t.ckode, t.nlike, t.cusername, b.*
         FROM tblike t
         JOIN tbbarang b ON t.ckode = b.cKode
         WHERE t.cusername = ?"
    );
    $query->bind_param("s", $cusename);
    
    if ($query->execute()) {
        $result = $query->get_result();
        
        if ($result->num_rows > 0) {
            $data = [];
            while ($row = $result->fetch_assoc()) {
                $data[] = $row;
            }
            echo json_encode($data);
        } else {
            echo json_encode([]);
        }
    } else {
        echo json_encode(['error' => 'Failed to execute query']);
    }

    $query->close();
} else {
    echo json_encode(['error' => 'Method not allowed']);
}

mysqli_close($conn);
?>
