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

$ProductionDate = isset($_SESSION["ProductionDate"]) ? $_SESSION["ProductionDate"] : date("Y-m-d");
$headers = [
	"Breed Type", 
	"id", 
	"Age In Weeks", 
	"Standard H.D.%", 
	"Standard H.H.%", 
	"Egg Weight, g", 
	"Egg Weight, Weeks", 
	"Egg Weight, Cum", 
	"Egg Mass, Weeks", 
	"Egg Mass, Cum"
];
$inputNames = [
	"BreedType", 
	"id", 
	"AgeInWeeks", 
	"Stnd_H_D_per", 
	"Stnd_H_H_per", 
	"EggWeight_g", 
	"EggWeight_Weeks", 
	"EggWeight_Cum", 
	"EggMass_Weeks", 
	"EggMass_Cum"
];

echo "<table id='MyTable_controller' class='ui display controller' style='display: none;'>";
echo "<thead></tr>";
echo "<tr>";
foreach ($headers as $index => $headerText) 
{
	echo "<th class='bg-light text-uppercase'>{$headerText}</th>";
}
echo "</tr></thead><tbody>";

$result = executeQuery($conn, "SELECT * FROM egg_standards WHERE is_deleted = FALSE AND is_current = TRUE ORDER BY BreedType, AgeInWeeks ASC");

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

while ($row = mysqli_fetch_assoc($result)) 
{
	echo "<tr>";
	foreach ($inputNames as $index => $inputName) 
	{
		if ($index == 0) {
			echo "<td style='display: flex; align-items: center;'><button type='button' class='btn btn-light text-danger btn-wo-border shadow-sm fs-4 d-flex align-items-center justify-content-center es-clickable' data-id='". $row["id"] . "' data-table='egg_standards' title='Delete'><i class='lni lni-minus-circle'></i></button><input type='text' class='form-control' name='{$inputName}_{$row["id"]}' value='" . $row[$inputName] . "'></td>";
		}
		else if ($index == 1) {
			echo "<td><input type='text' class='form-control inputDisabled' name='{$inputName}_{$row["id"]}' value='" . $row[$inputName] . "' readonly></td>";
		}
		else {
			echo "<td><input type='text' class='form-control' name='{$inputName}_{$row["id"]}' value='" . $row[$inputName] . "'></td>";
		}
	}
	echo "</tr>";
}
echo "</tbody></table>";
?>