<?php
include '../config/connect.php';

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

$category_id = isset($_GET['category_id']) ? intval($_GET['category_id']) : 0;

$sql = "SELECT id,port_odp FROM pelanggan WHERE id_master_topologi = ?";
$stmt = $con->prepare($sql);
$stmt->bind_param("i", $category_id);
$stmt->execute();
$result = $stmt->get_result();

$sql2 = "SELECT jumlah_port FROM master_topologi WHERE id = ?";
$stmt2 = $con->prepare($sql2);
$stmt2->bind_param("i", $category_id);
$stmt2->execute();
$result2 = $stmt2->get_result();
$row2 = $result2->fetch_assoc();
$jum = $row2['jumlah_port'];

if ($result->num_rows > 0) 
{
    $angka = [];
    while ($row = $result->fetch_assoc()) {
    $angka[] = $row['port_odp'];
    }

    $ang = range(1, $jum);
    $kosong=array_diff($ang,$angka);
    $subcategories = [];
    foreach ($kosong as $key) 
    {
        $subcategories[] = array("id'" => $key, "port_odp" => $key);
    }
}
else
{
    $a = 1;
    $subcategories = [];
    while ($a <= $jum) {
    $subcategories[] = array("id'" => $a, "port_odp" => $a);
    $a ++;
    }
}

echo json_encode($subcategories);

$stmt->close();
$con->close();
?>