<?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'];

            $stmt1 = $conn->prepare("SELECT cjenismember FROM tbmember WHERE ckodecust = ?");
            $stmt1->bind_param("s", $ckodecust);
            $stmt1->execute();
            $res1 = $stmt1->get_result()->fetch_assoc();
            $stmt1->close();

            if (!$res1 || !isset($res1['cjenismember'])) {
                echo json_encode(['error' => 'Member not found']);
                break;
            }

            $cjenismember = $res1['cjenismember'];

            $stmtInfo = $conn->prepare("SELECT ckode, cnama, ndiscextra, cketerangan, ccolor1, ccolor2 FROM tbjenismember WHERE ckode = ?");
            $stmtInfo->bind_param("s", $cjenismember);
            $stmtInfo->execute();
            $memberInfo = $stmtInfo->get_result()->fetch_assoc();
            $stmtInfo->close();

            echo json_encode($memberInfo);
        } else {
            echo json_encode(['error' => 'ckodecust is required']);
        }
        break;

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

$conn->close();

?>