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

            $stmt2 = $conn->prepare("SELECT * FROM tbjenismemberdtl WHERE ckode = ? ORDER BY cnourut ASC");
            $stmt2->bind_param("s", $cjenismember);
            $stmt2->execute();

            $result = $stmt2->get_result();
            $rows = [];

            while ($row = $result->fetch_assoc()) {
                $rows[] = $row;
            }

            $stmt2->close();

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

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

$conn->close();

?>