<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
require_once('login.lib.php');

include('top.php');

if (user_is_authenticated()) {

require_once('settings.php');
require_once('db_connect.php');
require_once('groundbased/gb_settings.php');
require_once('utilities.php');
?>
<h2>Welcome to the K2Exo website.</h2>

<p>Use the menu above to navigate to the relevant pages.</p>

<h3>Most recent observations</h3>
<table class="tablesorter">
<thead>
	<tr>
		<th style="width:20px;"></th>
		<th>KIC/EPIC</th>
		<th>Instrument</th>
		<th>Obtained</th>
		<th>Contact</th>
		<th>Remarks</th>
	</tr>
</thead>
<tbody>
<?php
foreach ($db->query("SELECT * FROM gb_observations_view WHERE status=1 ORDER BY uploaded DESC LIMIT 10;") as $row) {
	echo "<tr>";
	echo "	<td style=\"text-align:center;width:20px;\">" . gb_instrument_type_image($row['inst_type']) . "</td>";
	echo "	<td style=\"text-align:right;\"><a href=\"/catalog/{$row['Kepler_id']}\">" . $row['Kepler_id'] . "</a></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>" . $row['obtained_from'] . "</td>";
	echo "<td>" . format_email($row['contact_email'], $row['contact_name']) . "</td>";
	echo "<td>" . htmlencode($row['remarks']) . "</td>";
	echo "</tr>";
}
?>
</tbody>
</table>

<h3>Upcoming observation times</h3>
<table class="tablesorter">
<thead>
	<tr>
		<th style="width:20px;"></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 LIMIT 10;") 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>
<?php
} else {
?>
<h2>K2Exo</h2>
<p>Please log in using the form below.</p>
<div style="text-align:center;">
<?php
require_once('login.lib.php');
login_render_login_form();
?>
<div>
<?php
}

include('bottom.php');
