<?php
require('protect.php');

require_once('utilities.php');
require_once('db_connect.php');
require_once('groundbased/gb_settings.php');
require_once('redirect.php');

$mode = isset($_GET['mode']) ? trim($_GET['mode']) : '';
if ($mode == 'submit') {
	$instid = isset($_POST['instid']) ? trim($_POST['instid']) : '';
	$t_start = isset($_POST['t_start']) ? trim($_POST['t_start']) : '';
	$t_start_time = isset($_POST['t_start_time']) ? trim($_POST['t_start_time']) : '';
	$t_end = isset($_POST['t_end']) ? trim($_POST['t_end']) : '';
	$t_end_time = isset($_POST['t_end_time']) ? trim($_POST['t_end_time']) : '';
	$remarks = isset($_POST['remarks']) ? trim($_POST['remarks']) : '';

	if (!empty($instid) && !empty($t_start) && !empty($t_end)) {
		$stmt = $db->prepare("INSERT INTO gb_observation_times (userid,instid,t_start,t_end,remarks) VALUES (:userid,:instid,:tstart,:tend,:remarks);");
		$stmt->bindValue(':userid', $_SESSION['userid'], PDO::PARAM_INT);
		$stmt->bindValue(':instid', $instid, PDO::PARAM_STR);
		$stmt->bindValue(':tstart', $t_start . ' ' . $t_start_time, PDO::PARAM_STR);
		$stmt->bindValue(':tend', $t_end . ' ' . $t_end_time, PDO::PARAM_STR);
		$stmt->bindValue(':remarks', $remarks, PDO::PARAM_STR);
		$stmt->execute();
		$stmt->closeCursor();

		redirect('./obs_times.php', 303);
	}
}

$page_title = 'Observation times';
include('top.php');
?>
<h2>Observation times</h2>

<h3>Upcoming observation times</h3>
<table class="tablesorter">
<thead>
	<tr>
		<th></th>
		<th>Instrument</th>
		<th>Period (UTC)</th>
		<th>Contact</th>
		<th>Remarks</th>
	</tr>
</thead>
<tbody>
<?php
foreach ($db->query("SELECT gb_observation_times.instid AS instid,gb_instruments.name as inst_name,gb_instruments.type AS inst_type,users.name AS contact_name,users.email AS contact_email,gb_observation_times.t_start,gb_observation_times.t_end,gb_observation_times.remarks FROM gb_observation_times LEFT JOIN gb_instruments ON gb_observation_times.instid=gb_instruments.instid LEFT JOIN users ON gb_observation_times.userid=users.userid WHERE t_end > UTC_TIMESTAMP ORDER BY t_start DESC;") as $row) {
	echo "<tr>";
	echo "	<td style=\"text-align:center;width:20px;\">" . gb_instrument_type_image($row['inst_type']) . "</td>";
	echo "	<td style=\"white-space:nowrap;\"><a href=\"/groundbased/instrument.php?id=" . urlencode($row['instid']) . "\" class=\"gb_inst_link\">" . htmlentities($row['inst_name']) . "</a></td>\n";
	echo "	<td>" . date('jS M Y H:i', strtotime($row['t_start'])) . " &ndash; " . date('jS M Y H:i', strtotime($row['t_end'])) . "</td>";
	echo "	<td>" . format_email($row['contact_email'], $row['contact_name']) . "</td>";
	echo "	<td>" . htmlencode($row['remarks']) . "</td>";
	echo "</tr>";
}
?>
</tbody>
</table>

<h3>Past observation times</h3>
<table class="tablesorter">
<thead>
	<tr>
		<th></th>
		<th>Instrument</th>
		<th>Period (UTC)</th>
		<th>Contact</th>
		<th>Remarks</th>
	</tr>
</thead>
<tbody>
<?php
foreach ($db->query("SELECT gb_observation_times.instid AS instid,gb_instruments.name as inst_name,gb_instruments.type AS inst_type,users.name AS contact_name,users.email AS contact_email,gb_observation_times.t_start,gb_observation_times.t_end,gb_observation_times.remarks FROM gb_observation_times LEFT JOIN gb_instruments ON gb_observation_times.instid=gb_instruments.instid LEFT JOIN users ON gb_observation_times.userid=users.userid WHERE t_end <= UTC_TIMESTAMP ORDER BY t_start DESC;") as $row) {
	echo "<tr>";
	echo "	<td style=\"text-align:center;width:20px;\">" . gb_instrument_type_image($row['inst_type']) . "</td>";
	echo "	<td style=\"white-space:nowrap;\"><a href=\"/groundbased/instrument.php?id=" . urlencode($row['instid']) . "\" class=\"gb_inst_link\">" . htmlentities($row['inst_name']) . "</a></td>\n";
	echo "	<td>" . date('jS M Y H:i', strtotime($row['t_start'])) . " &ndash; " . date('jS M Y H:i', strtotime($row['t_end'])) . "</td>";
	echo "	<td>" . format_email($row['contact_email'], $row['contact_name']) . "</td>";
	echo "	<td>" . htmlencode($row['remarks']) . "</td>";
	echo "</tr>";
}
?>
</tbody>
</table>
<div id="gb_inst_dialog" style="text-align:left;"></div>


<h3>Add new observation period</h3>
<p>Please note that times are put in UTC and in the format "YYYY-MM-DD". Times are put in HH:MM format.</p>
<form action="?mode=submit" method="post">
<div>
	<label for="instid">Instrument:</label>
	<select name="instid" id="instid">
		<option value="">--- Please select ---</option>
<?php
echo gb_instrument_options($db);
?>
	</select><br />
	<label for="t_start">Start (UTC):</label><input type="text" name="t_start" id="t_start" />&nbsp;<input type="text" name="t_start_time" id="t_start_time" maxlength="5" style="width:5em;" /><br />
	<label for="t_end">End (UTC):</label><input type="text" name="t_end" id="t_end" />&nbsp;<input type="text" name="t_end_time" id="t_end_time" maxlength="5" style="width:5em;" /><br />
	<label for="remarks">Remarks:</label><textarea rows="3" cols="20" name="remarks" id="remarks"></textarea><br /><br />

	<div style="padding-left:200px;">
		<input type="submit" value="Submit" />
	</div>
</div>
</form>
<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){
	$("#t_start").datepicker({
		firstDay: 1,
		dateFormat: 'yy-mm-dd',
		onClose: function(selectedDate){
			$("#t_end").datepicker("option", "minDate", selectedDate);
		}
	});
	$("#t_end").datepicker({
		firstDay: 1,
		dateFormat: 'yy-mm-dd',
		onClose: function(selectedDate){
			$("#t_start").datepicker("option", "maxDate", selectedDate);
		}
	});
	$("#t_start_time,#t_end_time").timepicker({
		'timeFormat': 'H:i'
	});
});
//]]>
</script>
<?php
include('bottom.php');
?>
