<?php
require_once("dbconn.php");
require_once("phpFunctions.php");

$FCode_BatchName = isset($_POST["fc_BatchName"]) ? $_POST["fc_BatchName"] : null;

$result = executeQuery($conn, "SELECT BldgNo FROM batch_details WHERE Status = 1 AND is_deleted = FALSE AND is_current = TRUE AND BatchName = ?", "s", [$FCode_BatchName]);

if ($result === false) { 
	throw new Exception("Error executing query: " . $conn->error);
} 

$options = '<option value="" disabled>SELECT</option>';

if ($result->num_rows > 0) 
{
    while($row = $result->fetch_assoc()) 
	{
		$bldgNos = explode(',', $row["BldgNo"]);
		foreach ($bldgNos as $bldgNo) {
			$cleanedBldgNo = preg_replace('/\[\d+\]/', '', $bldgNo);
            $options .= '<option value="' . $cleanedBldgNo . '">' . $cleanedBldgNo . '</option>';
        }
    }
} else {
	$options = '<option value="" disabled selected>SELECT</option>';
}

echo $options;
?>