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

    $COLUMN_NAME = [];
    $table_header = [];
    $size_table_header = [];
    $table_value = [];
    $size_table_value = [];
    $table = [];
    $size_table = [];
    $id = "";
    $unit = "";

    $sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Component_Info2';";
    $fetchResult = sqlsrv_query($conn, $sql);
    while ($row = sqlsrv_fetch_array($fetchResult)) {
        array_push($COLUMN_NAME, $row["COLUMN_NAME"]);
    }

    $sql = "SELECT * FROM [BIMComponents].[dbo].[Component_Info2] Where [componentName] = '" . $name . "';";
    $fetchResult = sqlsrv_query($conn, $sql);
    $string = file_get_contents("../../assets/columnTable.json");
    $json = json_decode($string, true);
    while ($row = sqlsrv_fetch_array($fetchResult, SQLSRV_FETCH_NUMERIC)) {
        $table = $row;
    }
    
    foreach ($table as $key => $value) {
        if ($key != 0 && $key != 1 && $key != 2 && $key != 7) {
            array_push($table_header, $json[0][$COLUMN_NAME[$key]]);
            array_push($table_value, $value);
        }else if($key == 0){
            $id = $value;
        }else if ($key == 7){
            $unit = $value;
        }
    }

    $sql = "SELECT * FROM [BIMComponents].[dbo].[Component_Size2] WHERE [id] = '".$id."';";
    $fetchResult = sqlsrv_query($conn, $sql);
    while($row = sqlsrv_fetch_array($fetchResult)){
        array_push($size_table_header, $row["sizeColumn"]);
        array_push($size_table_value, $row["sizeValue"]." ".$unit);
    }

    $array = array();
    $array["header"] = $table_header;
    $array["value"] = $table_value;
    $array["sizeHeader"] = $size_table_header;
    $array["sizeValue"] = $size_table_value;
    $array["parent"] = $parent;
    echo json_encode($array);
}

sqlsrv_close($conn);
