<?php
if (isset($_POST["dir"])) {
    include("../connectSQL_Component.php");

    $file_tables = [];
    $dir_tables = [];
    $dir = $_POST["dir"];
    $array = [];
    $sql = "SELECT [id],[title], [file_path] FROM [BIMComponents].[dbo].[Video_Table] WHERE dir_name = '" . $dir . "'";
    $fetchResult = sqlsrv_query($conn, $sql);
    while ($row = sqlsrv_fetch_array($fetchResult, SQLSRV_FETCH_ASSOC)) {
        array_push($file_tables, $row);
    }

    $sql = "SELECT [dir_name] FROM [BIMComponents].[dbo].[Video_Node] WHERE [parent] = '" . $dir . "'";
    $fetchResult = sqlsrv_query($conn, $sql);
    while ($row = sqlsrv_fetch_array($fetchResult, SQLSRV_FETCH_ASSOC)) {
        array_push($dir_tables, $row);
    }
    $sql = "SELECT [parent] FROM [BIMComponents].[dbo].[Video_Node] WHERE [dir_name] = '" . $dir . "'";
    $fetchResult = sqlsrv_query($conn, $sql);
    while ($row = sqlsrv_fetch_array($fetchResult, SQLSRV_FETCH_ASSOC)) {
        $array["parent"] = $row["parent"];
    }
    $array["dir_names"] = $dir_tables;
    $array["file_tables"] = $file_tables;

    echo json_encode($array);

    sqlsrv_close($conn);
}
