<?php
include "ulf_util.php";
session_start();

// get year and month
if (isset($_GET['year']) && isset($_GET['month'])) {
        $year = $_GET['year'];
        $month = $_GET['month'];
}
else {
        $year = date("Y");
        $month = date("n");
}

// get number of days
$daysinmonth = array(31,28,31,30,31,30,31,31,30,31,30,31);
$days = $daysinmonth[$month - 1];
$result = is_leap_year($year);
if ($month == 2 && $result == true) {
        $days = $days + 1;
}

// get previous and next month
if ($month == 1) {
        $prev_year = $year - 1;
        $prev_month = 12;

        $next_year = $year;
        $next_month = $month + 1;
}
else if ($month == 12) {
        $prev_year = $year;
        $prev_month = $month - 1;

        $next_year = $year + 1;
        $next_month = 1;
}
else {
        $prev_year = $year;
        $prev_month = $month - 1;

        $next_year = $year;
        $next_month = $month + 1;
}

// set data path of each system
// $stations = array("NAL", "LYR", "HOR", "SDY", "POK", "IQA", "ISR");
// $stationinitials = array("N", "L", "H", "S", "P", "I", "R");
$stations = array("NAL", "LYR", "ISR", "HOR", "NAQ", "UNH", "SNK", "IQA", "MCM", "SPA", "VNA", "KSS", "JBS", "HAL", "RTH", "ZK1", "ZK2", "ZK3", "SDY", "PG2", "PG3", "PG4", "PG5");
$stationinitials = array("N", "L", "R", "H", "Q", "D", "Y", "I", "M", "A", "U", "K", "J", "B", "R", "6", "7", "8", "S", "2", "3", "4", "5");
$num_stations = 23;

//edit ccallie - add array of station names for tooltips
$stationnames = array(
    "NAL" => "Ny Alesund",
    "LYR" => "Longyearbyen",
    "ISR" => "Isfjord Radio",
    "HOR" => "Hornsund",
    "SDY" => "Sondrestrom",
    "POK" => "Poker Flat",
    "IQA" => "Iqaluit",
    "SNK" => "Sanikiluaq",
    "NAQ" => "Narsarsuaq",
    "UNH" => "University New Hampshire",
    "KSS" => "King Sejong",
    "JBS" => "Jang Bogo",
    "VNA" => "Von Neumeyer",
    "HAL" => "Halley Station",
    "RTH" => "Rothera Station",
    "SPA" => "South Pole",
    "MCM" => "McMurdo Station",
    "PG2" => "PG2",
    "PG3" => "PG3",
    "PG4" => "PG4",
    "PG5" => "PG5",
    "ZK1" => "ZK1",
    "ZK2" => "ZK2",
    "ZK3" => "ZK3"
);

$datapath_plot = "/var/www/html/ULF/data_plot/";
$datapath_txt = "/var/www/html/ULF/";
$webpath_plot = "http://mirl-dolly.sr.unh.edu/ULF/data_plot/";
$webpath_txt = "http://mirl-dolly.sr.unh.edu/ULF/data_txt/";
$statuspath = "http://mirl-dolly.sr.unh.edu/ULF/status/";

?>

<html>
<head>
<title>ULF Data Status</title>

<script language='javascript'>

// edit ccallie - replaced MoveToMonth button navigation with dropdown picker
function GoToSelectedMonth()
{
        var month = document.getElementById('month_select').value;
        var year = document.getElementById('year_select').value;
        document.location = "<?php echo($_SERVER['PHP_SELF']); ?>?year=" + year + "&month=" + month;
}

function GoToMain()
{
        document.location = "ulf_svalbard.html";
}

</script>

<style type="text/css">
th {
    position: sticky;
    top: 0;
    z-index: 2;
    background-color: #ffffff;
    border-style: solid;
    border-width: 1px;
    border-color: #000000;
    font-family: arial;
    font-size: 14px;
    color: #000000;
}

td {
    border-style: dotted;
    border-width: 1px;
    border-color: #AAAAAA;
    font-family: arial;
    font-size: 14px;
    color: #000000;
}

td.station-col {
    position: sticky;
    left: 0;
    z-index: 1;
    background-color: inherit;
}

th.corner {
    left: 0;
    z-index: 3;
}

/* edit ccallie - styles for month/year dropdown picker */
.date-picker {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-family: arial;
    font-size: 14px;
}

.date-picker select {
    font-family: arial;
    font-size: 14px;
    padding: 3px 6px;
}

.date-picker input[type=button] {
    font-family: arial;
    font-size: 14px;
    padding: 3px 10px;
}
</style>

</head>

<body>

<!-- Banner Start -->
<table border="0" cellpadding="0" cellspacing="0" height="80" width="100%" bgcolor="#004E9B">
        <tr>
                <td>
                <img src="mirl_logo.jpg" width="94" height="100%">
                </td>
                <td align="left">
                <font style="font-family:arial; font-size:20px; color:#FFFFFF"><b>ULF Magnetometer Database Status<br>
                <font style="font-family:arial; font-size:27px; color:#FFFFAA"><?php echo($year) ?> - <?php echo($month) ?>
                </td>
        </tr>
</table>
<!-- Banner End -->

<br/>

<!-- edit ccallie - replaced previous/next buttons with month and year dropdowns -->
<div class="date-picker">
    <label for="month_select">Month:</label>
    <select id="month_select">
        <?php
        $monthnames = array(1=>"January", 2=>"February", 3=>"March", 4=>"April",
                            5=>"May", 6=>"June", 7=>"July", 8=>"August",
                            9=>"September", 10=>"October", 11=>"November", 12=>"December");
        foreach ($monthnames as $mnum => $mname) {
                $selected = ($mnum == $month) ? "selected" : "";
                echo("<option value=$mnum $selected>$mname</option>");
        }
        ?>
    </select>

    <label for="year_select">Year:</label>
    <select id="year_select">
        <?php
        // edit ccallie - year range from current year back to 2000
        for ($y = date("Y"); $y >= 2000; $y--) {
                $selected = ($y == $year) ? "selected" : "";
                echo("<option value=$y $selected>$y</option>");
        }
        ?>
    </select>

    <input type="button" value="Go" onClick="GoToSelectedMonth()"/>
    <input name="gotomain" type="button" value="Go to main" onClick="GoToMain()"/>
</div>

<br><br>

<!-- Main Start -->
<table width="100%" cellspacing="0" border="0">
        <tr>
                <th class='corner'></th>

                <?php
                        for ($i = 1; $i <= $days; $i++) {
                                echo("<th width=18>" . $i . "</th>");
                        }
                ?>
        </tr>

<?php
        for ($n = 0; $n < $num_stations; $n++) {
                //edit ccallie - add contrast to table rows
                //echo("<tr>");
                $rowcolor = ($n % 2 == 0) ? "#cccccc" : "#ffffff";
                echo("<tr style=\"background-color:" . $rowcolor . "\">");

                //edit ccallie - station name mouseover tooltip
                //echo("<td width=20 height=40>" . $stations[$n] . "</td>");
                $tooltip = isset($stationnames[$stations[$n]]) ? $stationnames[$stations[$n]] : "";
                echo("<td class='station-col' width=20 height=40 title=\"" . $tooltip . "\">" . $stations[$n] . "</td>");

                //edit ccallie - check if station has any data for the month; if not, show single merged cell
                $has_data = false;
                for ($i = 1; $i <= $days; $i++) {
                        if (is_file_exists($datapath_plot . $stations[$n] . "/", $year, $month, $i, $stationinitials[$n], "pdf") ||
                            is_file_exists($datapath_plot . $stations[$n] . "/", $year, $month, $i, $stationinitials[$n], "jpg") ||
                            is_file_exists($datapath_plot . $stations[$n] . "/", $year, $month, $i, $stationinitials[$n], "png")) {
                                $has_data = true;
                                break;
                        }
                }

                if (!$has_data) {
                        //edit ccallie - replace row of N/A cells with single merged "no data" message
                        //edit ccallie - font size set to 14px
                        echo("<td colspan=" . $days . " align=center style=\"background-color:" . $rowcolor . "; color:#888888; font-family:arial; font-size:14px;\">No data available for this period</td>");
                } else {
                        for ($i = 1; $i <= $days; $i++) {
                                //edit ccallie - set row background for contrast
                                //echo("<td width=18 height=40 align=center>");
                                echo("<td width=18 height=40 align=center style=\"background-color:" . $rowcolor . "\">");
                                echo("<font style=\"font-family:arial; font-size:11px; color:#000000\">");
                                if (is_file_exists($datapath_plot . $stations[$n] . "/", $year, $month, $i, $stationinitials[$n], "pdf")) {
                                        $plotpath = get_file_path($webpath_plot . $stations[$n] . "/", $year, $month, $i, $stationinitials[$n], "pdf");
                                        echo("<a href=" . $plotpath . " target='_blank'>PDF</a><br>");
                                }
                                else if (is_file_exists($datapath_plot . $stations[$n] . "/", $year, $month, $i, $stationinitials[$n], "jpg")) {
                                        $plotpath = get_file_path($webpath_plot . $stations[$n] . "/", $year, $month, $i, $stationinitials[$n], "jpg");
                                        echo("<a href=" . $plotpath . " target='_blank'>JPG</a><br>");
                                }
                                else if (is_file_exists($datapath_plot . $stations[$n] . "/", $year, $month, $i, $stationinitials[$n], "png")) {
                                        $plotpath = get_file_path($webpath_plot . $stations[$n] . "/", $year, $month, $i, $stationinitials[$n], "png");
                                        echo("<a href=" . $plotpath . " target='_blank'>NOTE</a><br>");
                                }
                                else {
                                        //edit ccallie - remove N/A link, replace with plain greyed-out text
                                        //echo("<a href=http://mirl-dolly.sr.unh.edu/ULF/NA.jpg>N/A</a><br>");
                                        echo("<span style=\"color:#888888;\">N/A</span><br>");
                                }
                                echo("</td>");
                        }
                }
                echo("</tr>");
        }
?>

</table>
<!-- Main End -->

    <td>
    <img src="Map_MICA_N_labels_2020.png" width="50%" height="50%">
    <img src="Map_MICA_S_2020.png" width="35%" height="50%">
    </td>

<br>
<?php
#echo("<a href=" . $statuspath . "Status_" . $year . "_" . $month . ".bmp>Database Import Status</a>");
?>
<br>

</body>
</html>
