<?php
include("sql.php");

if (isset($_POST["project_id"]) && isset($_POST["is_blueprint"])) {
    $project_id = $_POST["project_id"];
    $is_blueprint = $_POST["is_blueprint"];
} else {
    die("post error!");
}

$data_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 [project_id],[notes],COUNT(*) AS 'total' FROM [Blueprint] WHERE [project_id] = '" . $project_id . "' GROUP BY [project_id],[notes] ORDER BY [project_id];";

    $fetchResult = sqlsrv_query($conn, $sql);
    while ($row = sqlsrv_fetch_array($fetchResult, SQLSRV_FETCH_ASSOC)) {
        array_push($data_array, $row);
    }
} else {
    $sql = "SELECT [project_id],[File_Table].[type_id],[type_name],COUNT(*) AS 'total' ,[filetype_id] FROM [File_Table]  RIGHT JOIN [File_Category] ON [File_Table].[type_id] = [File_Category].[type_id] WHERE [project_id] = '" . $project_id . "' GROUP BY  [project_id],[File_Table].[type_id],[type_name],[filetype_id]
	UNION 
SELECT [project_id],[Blueprint].[type_id],[type_name],COUNT(*) AS 'total',[filetype_id] FROM [Blueprint]  RIGHT JOIN [File_Category] ON [Blueprint].[type_id] = [File_Category].[type_id] WHERE [project_id] = '" . $project_id . "' GROUP BY  [project_id],[Blueprint].[type_id],[type_name],[filetype_id]
	ORDER BY [project_id];";

    $fetchResult = sqlsrv_query($conn, $sql);
    while ($row = sqlsrv_fetch_array($fetchResult, SQLSRV_FETCH_ASSOC)) {
        array_push($data_array, $row);
    }
}


sqlsrv_close($conn);
echo json_encode($data_array, JSON_UNESCAPED_UNICODE);
