<?php

require_once "config.php";
header('Content-Type: application/json');

$method = $_SERVER['REQUEST_METHOD'];

switch ($method) {
    case 'GET':
        $cemail = $_GET['cemail'] ?? '';

        if ($cemail === '') {
            echo json_encode([
                "totalpoin" => number_format(0, 0, ',', '.')
            ]);
            exit;
        }

        $stmt = $conn->prepare("SELECT IFNULL(SUM(npoin), 0) AS totalpoin FROM tbbukupoin WHERE ckodecust = ?");
        $stmt->bind_param("s", $cemail);
        $stmt->execute();

        $result = $stmt->get_result()->fetch_assoc();
        $stmt->close();

        $result['totalpoin'] = number_format((int)$result['totalpoin'], 0, ',', '.');
        echo json_encode($result);
        break;
}

$conn->close();
