<?php
include("connectSQL_Component.php");
if (isset($_GET["parent"])) {
    $type = $_GET["type"];
    $parent = $_GET["parent"];

    $jsonFileContents = file_get_contents("../../assets/equipmentTable2.json");
    $json = json_decode($jsonFileContents);

    for ($i = 0; $i < count($json); $i++) {
        if ($parent == $json[$i]->category_code) {
            $parent = $json[$i]->category_name;
        }
        for ($j = 0; $j < count($json[$i]->children); $j++) {
            if ($type == $json[$i]->children[$j]->component_code) {
                $type = $json[$i]->children[$j]->component_name;
            }
        }
    }

    if ($type != "")
        $sql = "SELECT DISTINCT [revitVersion] FROM [BIMComponents].[dbo].[Component_Info] WHERE [componentGroup] LIKE '%" . $parent . "%' AND [category] = '" . $type . "';";
    else if ($type == "")
        $sql = "SELECT DISTINCT [revitVersion] FROM [BIMComponents].[dbo].[Component_Info] WHERE [componentGroup] LIKE '%" . $parent . "%';";

    $fetchResult = sqlsrv_query($conn, $sql);
    $revitVersion = [];
    while ($row = sqlsrv_fetch_array($fetchResult)) {
        if ($row["revitVersion"] != null) {
            array_push($revitVersion, $row["revitVersion"]);
        }
    }

    echo json_encode($revitVersion);
}
