<?php
ini_set("session.gc_maxlifetime", 28800); // 8 hours in seconds
ini_set("session.cookie_lifetime", 28800); // 8 hours in seconds
session_start();
ob_start();

date_default_timezone_set("Asia/Manila");
setlocale(LC_NUMERIC, "en_US.UTF-8");

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

$userType = isset($_SESSION["type"]) ? $_SESSION["type"] : null;
$userName = isset($_SESSION["username"]) ? $_SESSION["username"] : "error-user";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<!-- FAVICON -->
<link rel="apple-touch-icon" sizes="180x180" href="img/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="img/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="img/favicon-16x16.png">
<link rel="manifest" href="img/site.webmanifest">
 
<!-- Bootstrap CSS -->
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="https://getbootstrap.com/docs/5.3/assets/css/docs.css">
<link rel="stylesheet" type="text/css" href="bootstrap-5.3.3-dist/css/styling.css">
<link rel="stylesheet" type="text/css" href="https://cdn.lineicons.com/5.0/lineicons.css" />

<!-- DataTables CSS -->
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.9.2/semantic.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/2.1.7/css/dataTables.semanticui.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/2.2.2/css/buttons.dataTables.min.css">

<title>Controller | PG 7</title>
</head>
<body class="bg-pg7-secondary">
<!-- MODAL START -->
<div class="modal fade modal-bd" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header bg-light">
				<h6 class="modal-title fw-bold">Batch Details</h6>
				<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
			</div>
			<form method="POST">
				<div class="modal-body">
						<div class="input-group input-group-sm mb-2">
							<input type="hidden" name="bd_id" class="form-control" autocomplete="off">
						</div>
						
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Batch Code</span>
							<input type="text" name="bd_BatchCode" class="form-control inputDisabled" readonly>
						</div>

						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Batch Name</span>
							<input type="text" name="bd_BatchName" class="form-control" autocomplete="off">
						</div>
						
						<input type="checkbox" name="bd_BatchName_switch" class="form-check-input" id="flexSwitchCheckDefault"><small> Exclude BatchName from the Reports</small>

						<label for="inputContainer"><small><b>Bldg. No.</b> <br>Note: Please ensure that the Bldg. No. matches with the Batch Name.</small></label>
						<div id="inputContainer">
							<div class="input-group input-group-sm mb-1">
								<div class="form-check form-switch">
									<input class="form-check-input BldgNoSwitch" type="checkbox" id="flexSwitchCheckDefault" checked onchange="updateTextarea()">
								</div>
								<input class="form-control itemInput" list="items" name="item" id="item" placeholder="e.g LB-B10, LB-B11" oninput="updateTextarea()">
								<input type="text" class="form-control amountInput" pattern="[0-9]+(\.[0-9]+)?" title="Please enter a valid number" autocomplete="off" placeholder="Enter the balance..." oninput="updateTextarea()">
								<button type="button" class="btn btn-light btn-wo-border shadow-sm fs-4 d-flex align-items-center justify-content-center" onclick="addInput()" title="Add"><i class="lni lni-plus"></i></button>
								<button type="button" class="btn btn-light btn-wo-border shadow-sm fs-4 d-flex align-items-center justify-content-center" onclick="removeInput()" title="Delete"><i class="lni lni-minus"></i></button>
							</div>
						</div>
						<datalist id="items">
							<?php
							$result = executeQuery($conn, "SELECT DISTINCT BldgNo FROM batch_details WHERE Status = 1 AND is_deleted = FALSE AND is_current = TRUE");

							if ($result->num_rows > 0) {
								while($row = $result->fetch_assoc()) {
									$bldgNos = explode(",", $row["BldgNo"]);
									foreach ($bldgNos as $bldgNo) {
										$cleanedBldgNo = preg_replace("/\[.*?\]\{.*?\}/", "", $bldgNo);
										if ($cleanedBldgNo !== "") {
											echo "<option value='" . trim($cleanedBldgNo) . "'>";
										}
									}
								}
							}
							?>
						</datalist>
						
						<textarea name="bd_BldgNo" class="form-control hidden" id="hiddenTextarea"></textarea>
						
						<div class="input-group input-group-sm mt-2 mb-2">
							<span class="input-group-text">Check In Date</span>
							<input type="date" name="bd_CheckInDate" class="form-control" max="<?php echo date("Y-m-d"); ?>">
						</div>
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Breed Type</span>
							<select class="form-select" name="bd_Breed">
								<?php 
									echo getBreedTypeOptions($conn);
								?>
							</select>
						</div>
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Hen Housed</span>
							<input type="text" name="bd_HenHoused" class="form-control" pattern="[0-9]+(\.[0-9]+)?" title="Please enter a valid number" autocomplete="off">
						</div>
	
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Previous Production</span>
							<input type="text" name="bd_Prod_Previous" class="form-control" pattern="[0-9]+(\.[0-9]+)?" title="Please enter a valid number" autocomplete="off">
						</div>
						
						<!-- START EGGSIZE -->
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Previous JMB</span>
							<input type="text" name="bd_prev_JMB" class="form-control" pattern="[0-9]+(\.[0-9]+)?" title="Please enter a valid number" autocomplete="off">
						</div>
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Previous XLR</span>
							<input type="text" name="bd_prev_XLR" class="form-control" pattern="[0-9]+(\.[0-9]+)?" title="Please enter a valid number" autocomplete="off">
						</div>
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Previous LRG</span>
							<input type="text" name="bd_prev_LRG" class="form-control" pattern="[0-9]+(\.[0-9]+)?" title="Please enter a valid number" autocomplete="off">
						</div>
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Previous MED</span>
							<input type="text" name="bd_prev_MED" class="form-control" pattern="[0-9]+(\.[0-9]+)?" title="Please enter a valid number" autocomplete="off">
						</div>
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Previous SML</span>
							<input type="text" name="bd_prev_SML" class="form-control" pattern="[0-9]+(\.[0-9]+)?" title="Please enter a valid number" autocomplete="off">
						</div>
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Previous PUL</span>
							<input type="text" name="bd_prev_PUL" class="form-control" pattern="[0-9]+(\.[0-9]+)?" title="Please enter a valid number" autocomplete="off">
						</div>
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Previous PWE</span>
							<input type="text" name="bd_prev_PWE" class="form-control" pattern="[0-9]+(\.[0-9]+)?" title="Please enter a valid number" autocomplete="off">
						</div>
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Previous SPW</span>
							<input type="text" name="bd_prev_SPW" class="form-control" pattern="[0-9]+(\.[0-9]+)?" title="Please enter a valid number" autocomplete="off">
						</div>
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Previous B1</span>
							<input type="text" name="bd_prev_B1" class="form-control" pattern="[0-9]+(\.[0-9]+)?" title="Please enter a valid number" autocomplete="off">
						</div>
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Previous B2</span>
							<input type="text" name="bd_prev_B2" class="form-control" pattern="[0-9]+(\.[0-9]+)?" title="Please enter a valid number" autocomplete="off">
						</div>
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Previous B3</span>
							<input type="text" name="bd_prev_B3" class="form-control" pattern="[0-9]+(\.[0-9]+)?" title="Please enter a valid number" autocomplete="off">
						</div>
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Previous M.Dirty</span>
							<input type="text" name="bd_prev_M_Dirty" class="form-control" pattern="[0-9]+(\.[0-9]+)?" title="Please enter a valid number" autocomplete="off">
						</div>
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Previous B.Dirty</span>
							<input type="text" name="bd_prev_B_Dirty" class="form-control" pattern="[0-9]+(\.[0-9]+)?" title="Please enter a valid number" autocomplete="off">
						</div>
						<!-- END EGGSIZE -->
						
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Age In Weeks</span>
							<input type="text" name="bd_Age" class="form-control inputDisabled" pattern="[0-9]+(\.[0-9]+)?" title="Please enter a valid number" autocomplete="off" readonly>
						</div>
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">S.H.H</span>
							<input type="text" name="bd_S_H_H" class="form-control inputDisabled" pattern="[0-9]+(\.[0-9]+)?" title="Please enter a valid number" autocomplete="off" readonly>
						</div>
						<div class="input-group input-group-sm mb-2">
							<span class="input-group-text">Status</span>
							<select class="form-select" name="bd_Status">
								<option value="0">Inactive</option>
								<option value="1">Active</option>
							</select>
						</div>
				</div>
				<div class="modal-footer">
					<button type="submit" name="bdInsertData" class="btn btn-light btn-wo-border shadow-sm" onclick="return confirm( 'Are you sure you want to commit this action?' )" style="display: none;">Insert</button>
					<button type="submit" name="bdUpdate" class="btn btn-light btn-wo-border shadow-sm" onclick="return confirm( 'Are you sure you want to commit this action?' )">Update</button>
					<button type="submit" name="bdDelete" class="btn btn-light btn-wo-border shadow-sm" onclick="return confirm( 'Are you sure you want to commit this action?' )">Delete</button>
					<button type="button" class="btn btn-dark btn-wo-border shadow-sm" data-bs-dismiss="modal">Close</button>
				</div>
			</form>
		</div>
	</div>
</div>

<div class="modal fade modal-esdelete modal-sm" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header bg-light">
				<h6 class="modal-title fw-bold">Egg Standards</h6>
				<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
			</div>
			<form method="POST">
				<div class="modal-body">
					<div class="input-group">
						<input type="hidden" name="es_id" class="form-control inputDisabled" readonly>
					</div>
					 <p>Are you sure you want to delete this?</p>
				</div>
				<div class="modal-footer">
					<button type="submit" name="esDelete" class="btn btn-light btn-wo-border shadow-sm">Proceed</button>
					<button type="button" class="btn btn-dark btn-wo-border shadow-sm" data-bs-dismiss="modal">Cancel</button>
				</div>
			</form>
		</div>
	</div>
</div>
<!-- MODAL END -->
<nav class="navbar navbar-expand bg-pg7-main">
	<div class="container-fluid">
		<div class="collapse navbar-collapse">
			<ul class="navbar-nav me-auto mb-lg-0">
				<li class="nav-item">
					<a href="dashboard.php?p=home" class="nav-link" title="Go to dashboard"><i class="lni lni-dashboard-square-1 fs-2"></i></a>
				</li>
				<li class="nav-item dropdown py-1">
					<a class="nav-link dropdown-toggle text-white" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
						Controller
					</a>
					<ul class="dropdown-menu">
						<li><a href="controller.php?u=<?php echo isset($userType) && !empty($userType) ? $userType : ""; ?>&p=es" class="dropdown-item">Egg Standards</a></li>
						<li><a href="controller.php?u=<?php echo isset($userType) && !empty($userType) ? $userType : ""; ?>&p=bd" class="dropdown-item">Batch Details</b></a></li>
					</ul>
				</li>
				<div class="vr border border-dark border-2 mx-2"></div>
				<li class="nav-item py-2">
					<i class="lni lni-user-4 text-white fs-2"></i> 
				</li>
				<li class="nav-item py-1">
					 <a class="nav-link">
						<?php echo isset($userName) && !empty($userName) ? $userName : ""; ?>
					</a>
				</li>
			</ul>
			<ul class="navbar-nav ms-auto mb-lg-0">
				<li class="nav-item">
					<h1 class="display-8 opacity-50 px-2">PG 7</h1>
				</li>
			</ul>
		</div>
	</div>
</nav>

<div class="container-fluid mt-3">
	<form method="POST" class="Controller">
		<?php if (isset($_GET["p"]) && $_GET["p"] !== "es") { ?>
		<div class="row px-1">
			<div class="col-auto">
				<div class="btn-group" role="group">
					<button type="button" name="addnew__<?php echo isset($_GET["p"]) ? $_GET["p"] : ""; ?>" class="btn btn-light btn-wo-border shadow-sm fs-4 d-flex align-items-center justify-content-center" title="Add New"><i class="lni lni-plus"></i></button>
					
					<?php if (isset($_GET["p"]) && $_GET["p"] === "bd") { ?>
					<button type="submit" name="RefreshButton" class="btn btn-light btn-wo-border shadow-sm fs-4 d-flex align-items-center justify-content-center" title="Refresh Input Production Table"><i class="lni lni-refresh-circle-1-clockwise"></i></button>
					<?php } ?>
				</div>
			</div>
			<div class="col-auto dt-search-placeholder">
				<!-- Search Box Here -->
			</div>
		</div>
		<?php } ?>
		
		<?php if (isset($_GET["p"]) && $_GET["p"] === "es") { ?>
		<div class="row px-1">
			<div class="col-auto">
				<div class="btn-group" role="group">
					<button type="submit" name="SaveButton" class="btn btn-light btn-wo-border shadow-sm fs-4 d-flex align-items-center justify-content-center" title="Save"><i class="lni lni-floppy-disk-1"></i></button>
					<button type="submit" name="NewRowButton" class="btn btn-light btn-wo-border shadow-sm fs-4 d-flex align-items-center justify-content-center" title="Add New Row"><i class="lni lni-plus"></i></button>
				</div>
			</div>
		</div>
		<?php } ?>
		
		<div class="row px-1 mt-1 mb-1">
			<div class="col-auto">
				<div class="dispMessage"></div>
			</div>
		</div>
		<div class="row px-1 mt-3">
			<div class="col-auto">
				<div id="spinner" style="display: none;">
					<div class="loader"></div>
				</div>
				<?php
				if (isset($_GET["p"]) && $_GET["p"] === "es")
				{
					require_once("esDisplay_Table.php");
				}
				else if (isset($_GET["p"]) && $_GET["p"] === "bd") 
				{
					require_once("bdDisplay_Table.php");
				}
				?>
			</div>
		</div>
	</form>
</div>

<!-- JavaScript Libraries -->
<script type="text/javascript" src="https://code.jquery.com/jquery-3.7.1.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/2.1.7/js/dataTables.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/2.1.7/js/dataTables.semanticui.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.9.2/semantic.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/2.11.8/umd/popper.min.js" integrity="sha512-TPh2Oxlg1zp+kz3nFA0C5vVC6leG/6mm1z9+mA81MI5eaUVqasPLO8Cuk4gMF4gUfP5etR73rgU/8PNMsSesoQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/3.0.2/js/dataTables.buttons.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/3.0.2/js/buttons.print.min.js"></script>

<!-- Custom JavaScript -->
<script type="text/javascript" src="bootstrap-5.3.3-dist/js/custom.js"></script>
<script type="text/javascript" src="bootstrap-5.3.3-dist/js/DataTables_controller.js"></script>

<script type="text/javascript">
$( document ).on( "change", ".form-check-input", function() {
	if ( $( this ).is( ":checked" ) ) {
		$( this ).prop( "checked", true );
	} else {
		$( this ).prop( "checked", false );
	}
} );

$( document ).ready( function() {
	$( ".modal-esdelete, .modal-bd" ).on( "hidden.bs.modal", function () {
        $( this ).find( "form" ).trigger( "reset" );
    } );

	$( "button[name='addnew__bd']" ).on( "click", function() {
		$( ".modal-bd input, textarea" ).val( "" );
		$( ".modal-bd input[name='bd_CheckInDate']" ).val( "<?php echo date("Y-m-d"); ?>" );
		$( "input[name='bd_CheckInDate']" ).trigger( "change" );
		
		$( "button[name='bdInsertData']" ).show();
		$( "button[name='bdUpdate'], button[name='bdDelete']" ).hide();
		$( ".modal-bd" ).modal( "show" );
		
		$.ajax( {
			url: "BatchCode_Generator.php",
			method: "GET",
			dataType: "json",
			success: function( response ) {
				$( "input[name='bd_BatchCode']" ).val( response.newBatchCode );
			},
			error: function() {
				$( ".dispMessage" ).html( "<div class='alert alert-danger'>" + response.message + "</div>" );
			}
		} );
	} );
	
	$( ".modal-bd" ).on( "shown.bs.modal", function() {
		$( "select[name='bd_Breed'], input[name='bd_CheckInDate']" ).trigger( "change" );
	} );
	
	$( "input[name='bd_CheckInDate']" ).on( "change", function() {
		var checkInDate = $( this ).val();
		var ageInWeeks = Math.floor( ( Date.now() - new Date( checkInDate ) ) / (7 * 24 * 60 * 60 * 1000) );
		
		$( "input[name='bd_Age']" ).val( ageInWeeks );
	} );

	$( "select[name='bd_Breed'], input[name='bd_Age'], input[name='bd_CheckInDate']" ).on( "change", function() {
		var pValue = new URLSearchParams( window.location.search ).get( "p" );
		
		if ( pValue === "bd" ) 
		{
			$.ajax( {
				url: "SHH_Calculator.php",
				type: "POST",
				data: {
					bd_Breed: $( "select[name='bd_Breed']" ).val(),
					bd_Age: $( "input[name='bd_Age']" ).val(),
				},
				success: function( data ) {
					try {
						var parsedData = JSON.parse( data );
						if ( parsedData.error ) {
							return;
						} else {
							$( "input[name='bd_S_H_H']" ).val( parsedData.SHH );
						}
					} catch ( e ) {
						return;
					}
				},
				error: function() {
					$( ".dispMessage" ).html( "<div class='alert alert-danger'>" + response.message + "</div>" );
				}
			} );
		}
	} );
	
	$( ".bd-clickable" ).on( "click", function() {
		var data_id = $( this ).data( "id" );
		var pValue = new URLSearchParams( window.location.search ).get( "p" );
		
		if ( pValue === "bd" ) 
		{
			$.ajax( {
				url: "bdRowData.php",
				type: "GET",
				data: { 
					"data_id": data_id
				},
				success: function( data ) {
					try {
						var parsedData = JSON.parse( data );
						if ( parsedData.error ) {
							return;
						} else {	
							$( ".modal-bd input[name='bd_id']" ).val( parsedData.id );
							$( ".modal-bd input[name='bd_BatchCode']" ).val( parsedData.BatchCode );
							$( ".modal-bd input[name='bd_BatchName']" ).val( parsedData.BatchName );
							
							if ( parsedData.BatchName_switch === 0 ) {
								$( ".modal-bd input[name='bd_BatchName_switch']" ).prop( "checked", false );
							} else {
								$( ".modal-bd input[name='bd_BatchName_switch']" ).prop( "checked", true );
							}
							
							$( ".modal-bd textarea[name='bd_BldgNo']" ).val( parsedData.BldgNo );

							$( "#hiddenTextarea" ).val( parsedData.BldgNo );
							populateInputsFromTextarea();
							
							$( ".modal-bd input[name='bd_CheckInDate']" ).val( parsedData.CheckInDate );
							$( ".modal-bd select[name='bd_Breed']" ).val( parsedData.Breed );
							$( ".modal-bd input[name='bd_HenHoused']" ).val( parsedData.HenHoused );
							$( ".modal-bd input[name='bd_Prod_Previous']" ).val( parsedData.Prod_Previous );
							
							$( ".modal-bd input[name='bd_prev_JMB']" ).val( parsedData.prev_JMB );
							$( ".modal-bd input[name='bd_prev_XLR']" ).val( parsedData.prev_XLR );
							$( ".modal-bd input[name='bd_prev_LRG']" ).val( parsedData.prev_LRG );
							$( ".modal-bd input[name='bd_prev_MED']" ).val( parsedData.prev_MED );
							$( ".modal-bd input[name='bd_prev_SML']" ).val( parsedData.prev_SML );
							$( ".modal-bd input[name='bd_prev_PUL']" ).val( parsedData.prev_PUL );
							$( ".modal-bd input[name='bd_prev_PWE']" ).val( parsedData.prev_PWE );
							$( ".modal-bd input[name='bd_prev_SPW']" ).val( parsedData.prev_SPW );
							$( ".modal-bd input[name='bd_prev_B1']" ).val( parsedData.prev_B1 );
							$( ".modal-bd input[name='bd_prev_B2']" ).val( parsedData.prev_B2 );
							$( ".modal-bd input[name='bd_prev_B3']" ).val( parsedData.prev_B3 );
							$( ".modal-bd input[name='bd_prev_M_Dirty']" ).val( parsedData.prev_M_Dirty );
							$( ".modal-bd input[name='bd_prev_B_Dirty']" ).val( parsedData.prev_B_Dirty );
							
							$( ".modal-bd select[name='bd_Status']" ).val( parsedData.Status );

							$( "button[name='bdInsertData']" ).hide();
							$( "button[name='bdUpdate'], button[name='bdDelete']" ).show();
							$( ".modal-bd" ).modal( "show" );
						}
					} catch ( e ) {
						return;
					}
				},
				error: function() {
					$( ".dispMessage" ).html( "<div class='alert alert-danger'>" + response.message + "</div>" );
				}
			} );
		}
	} );
	
	$( ".es-clickable" ).on( "click", function() {
		var data_id = $( this ).data( "id" );
		var data_table = $( this ).data( "table" );
		
		var pValue = new URLSearchParams( window.location.search ).get( "p" );
		
		if ( pValue === "es" ) 
		{
			$.ajax( {
				url: "esRowData.php",
				type: "GET",
				data: { 
					"data_id": data_id,
					"data_table": data_table
				},
				success: function( data ) {
					try {
						var parsedData = JSON .parse( data );
						if ( parsedData.error ) {
							return;
						} else {
							$( ".modal-esdelete input[name='es_id']" ).val( parsedData.id );
							$( ".modal-esdelete" ).modal( "show" );
						}
					} catch ( e ) {
						return;
					}
				},
				error: function() {
					$( ".dispMessage" ).html( "<div class='alert alert-danger'>" + response.message + "</div>" );
				}
			} );
		}
	} );
} );
</script>
</body>
<?php
require_once("RefreshData.php");
require_once("Save.php");
require_once("NewRow.php");
require_once("Controller_InsertQuery.php");
require_once("Controller_UpdateQuery.php");
require_once("Controller_DeleteQuery.php");

if (
(!isset($_SESSION["logged_in"]) || $_SESSION["logged_in"] === FALSE)
|| 
(isset($userType) && $userType === 2)
) 
{
	header("Location: logout.php");
	exit();
} 

if (isset($conn)) { $conn->close(); }
ob_end_flush();
?>
</html>