<?php

require_once "config.php";

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

$method = $_SERVER['REQUEST_METHOD'];

if ($method === 'GET') {
    $cKodeMember = $_GET['cKodeMember'] ?? '';

    $query = $conn->prepare("SELECT SUM(nSTotal) AS nSTotal FROM tbkeranjang WHERE cKodeMember = ?");
    $query->bind_param("s", $cKodeMember);
    
    if ($query->execute()) {
        $result = $query->get_result();
            
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $nSTotal = $row['nSTotal'];
        // Format nilai menjadi rupiah tanpa desimal
        $formattedTotal = "Rp. " . number_format($nSTotal, 0, ',', '.');
        echo json_encode(['nSTotal' => $formattedTotal]);
    } else {
        echo json_encode(['nSTotal' => 'Rp. 0']);
    }

    } else {
        echo json_encode(['error' => 'Failed to execute query']);
    }

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

mysqli_close($conn);
?>
