<?php

require_once "config.php";

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

$method = $_SERVER['REQUEST_METHOD'];

switch ($method) {
    case 'GET':
        if (isset($_GET['ckodecust'])) {
            $ckodecust = $_GET['ckodecust'];

            $query = $conn->prepare("
                SELECT 
                    m.cJenisMember,
                    m.*, 
                    jc.* 
                FROM 
                    tbmember m
                JOIN 
                    tbjenismember jc ON jc.ckode  = m.cJenisMember
                WHERE 
                    m.ckodecust = ?
            ");
            $query->bind_param('s', $ckodecust);
            $query->execute();
            $result = $query->get_result();

            $data = $result->fetch_assoc(); 
            
            if ($data) {
                $numericFields = ['nhrtermin', 'ndisccash', 'ndiscextra'];

                foreach ($numericFields as $field) {
                    if (isset($data[$field])) {
                        $floatVal = (float)$data[$field];
                        $data[$field] = ($floatVal == intval($floatVal)) ? intval($floatVal) : $floatVal;
                    }
                }

                echo json_encode($data);
            } else {
                echo json_encode(array('error' => 'Data tidak ditemukan.'));
            }
        } else {
            echo json_encode(array('error' => 'Parameter "ckodecust" is required.'));
        }
        break;

    default:
        echo json_encode(array('error' => 'Invalid request method.'));
        break;
}

$conn->close();

?>
