<?php

require_once "config.php";

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

$method = $_SERVER['REQUEST_METHOD'];

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

    $query = $conn->prepare("SELECT b.cFileImage, k.* FROM tbkeranjang k JOIN tbbarang b ON k.cKodeBrg = b.cKode WHERE k.cKodeMember = ?");
    $query->bind_param("s", $cKodeMember);
    
    if ($query->execute()) {
        $result = $query->get_result();
        
        if ($result->num_rows > 0) {
            $data = [];
            while ($row = $result->fetch_assoc()) {
                
                $row['nQty'] = (int)$row['nQty']; 
                $row['nHarga'] = "Rp. " . number_format((float)$row['nHarga'], 2, ',', '.');
                $row['nDisc1'] = (int)$row['nDisc1'];
                $row['nDisc2'] = (int)$row['nDisc2'];
                $row['nDisc3'] = (int)$row['nDisc3'];
                $row['nSTotal'] = "Rp. " . number_format((float)$row['nSTotal'], 2, ',', '.');

                // Menambahkan data ke array
                $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);
?>
