<?php

require_once "config.php";

header('Content-Type: application/json');
$method = $_SERVER['REQUEST_METHOD'];

switch ($method) {
    case 'GET':
        $id = isset($_GET['ID']) ? $_GET['ID'] : null;
        $cNoID = isset($_GET['cNoID']) ? $_GET['cNoID'] : null;
        $cKodeMember = isset($_GET['cKodeMember']) ? $_GET['cKodeMember'] : null;

        if (!$id || !$cNoID || !$cKodeMember) {
            echo json_encode([
                'success' => false,
                'error' => 'Parameter tidak lengkap'
            ]);
            exit;
        }

        $query_str = "DELETE FROM tbkeranjang WHERE ID = ? AND cNoID = ? AND cKodeMember = ?";
        $stmt = $conn->prepare($query_str);
        $stmt->bind_param("sss", $id, $cNoID, $cKodeMember);

        if ($stmt->execute()) {
            echo json_encode([
                'success' => true,
                'message' => 'Data berhasil dihapus'
            ]);
        } else {
            echo json_encode([
                'success' => false,
                'error' => 'Gagal menghapus data: ' . $stmt->error
            ]);
        }

        break;

    default:
        http_response_code(405);
        echo json_encode([
            'success' => false,
            'error' => 'Method tidak diperbolehkan'
        ]);
        break;
}

$conn->close();
?>
