<?php

if (isset($_GET['project_id'])) {

    include("sql.php");
    $project_id = $_GET['project_id'];
    $type_id = $_GET['type_id'];
    $is_blueprint = $_GET['is_blueprint'];
    $note = $_GET['note'];
    $array = [];

    $connectionInfo = array("Database" => "$dbname", "UID" => "$username", "PWD" => "$password", "CharacterSet" => "UTF-8");
    $conn = sqlsrv_connect($hostname, $connectionInfo);
    if ($conn === false) {
        die(print_r(sqlsrv_errors(), true));
    }
    
    if($is_blueprint == "true"){
        $sql = "SELECT DISTINCT [notes] FROM [21000X].[dbo].[Blueprint] WHERE [project_id] = '" . $project_id . "';";
    }else if($note != ""){
        $sql = "SELECT [SN],[original_name],[filename],[Blueprint].[project_id],[project_name],[type_id],[category_name],[notes],[keyword] FROM [21000X].[dbo].[Blueprint]
        INNER JOIN [Project_Table] ON [Blueprint].[project_id] = [Project_Table].[project_id]
        INNER JOIN [Construction_Category] ON [Project_Table].[category_id] = [Construction_Category].[category_id]
        WHERE [Blueprint].[project_id] = '" . $project_id . "' AND [notes] = '" . $note . "';";
    }else{
        $sql = "SELECT [SN],[original_name],[filename],[File_Table].[project_id],[project_name],[type_id],[category_name],null,[keyword] FROM [21000X].[dbo].[File_Table]
        INNER JOIN [Project_Table] ON [File_Table].[project_id] = [Project_Table].[project_id]
        INNER JOIN [Construction_Category] ON [Project_Table].[category_id] = [Construction_Category].[category_id]
		WHERE [File_Table].[project_id] = '" . $project_id . "' AND [type_id] = '".$type_id."';";
    }
    
    $fetchResult = sqlsrv_query($conn, $sql);
    while ($row = sqlsrv_fetch_array($fetchResult)) {
        array_push($array ,$row);
    }
    echo json_encode($array,JSON_UNESCAPED_UNICODE);
}

?>