<?php

/**
 * Create a cell for the progress part of the table
 * @param string $string Cell from the database
 *
 * @return string Returns the html for a cell
 */
function create_cell($string)
{
   if($string != null) {
      list($id, $priority, $status, $user) = explode('|', $string);

      $td_open = '<td class="%s progress_table_cell" id="%s"></td>';

      if($priority == 'not_required' || $status == "not_started") {
         $user = '';
      }


      if($priority == "unfunded") {
         $td_open = sprintf($td_open, "unfunded", $id);
      } elseif($priority == 'not_required' && $status != 'finished') {
         $td_open = sprintf($td_open, "not_requested", $id);
      } elseif($status == 'assigned') {
         $td_open = sprintf($td_open, "not_started", $id);
      } else {
         $td_open = sprintf($td_open, $status, $id);
      }


      return $td_open;

   } else {
      return '<td class="progress_table_cell progress_table_cell_empty"></td>';
   }
}

?>
<!DOCTYPE html>
<html>
<head>
   <meta charset="UTF-8">
   <title> Flight Processing Progress </title>
   <link rel="stylesheet" type="text/css" href="../style/default.css"/>
   <link rel="stylesheet" type="text/css" href="../style/fht.css"/>
   <link rel="stylesheet" type="text/css" href="../style/progress_table.css"/>
   <link rel="stylesheet" type="text/css" href="../style/dataset_info.css"/>
   <link rel="stylesheet" type="text/css" href="../style/colours.css"/>
   <!--[if IE]>
   <script type="text/javascript" src="../explorecanvas/excanvas.js"></script><![endif]-->
   <script src="progressChart.js"></script>
   <!-- jquery -->
   <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
   <script type="text/javascript" src="../common/fht.js"></script>
   <script src="../common/common.js"></script>
   <script src="progress_page_javascript.js"></script>

</head>
<body>
<div id="arsf_header">
   <a href="/">
      <img src="../images/banner.jpg">
   </a>
</div>
<div id="nerclogo">
   <a href="http://www.nerc.ac.uk/">
      <img src="/images/nerc_logo.png" width="86" height="60" border="0" alt="NERC Homepage" title="NERC Homepage">
   </a>
</div>
<div id="progress_table_and_legend">
   <div id="progress_table">
      <table class="fht">
         <thead>
         <tr>
            <th class='progress_table_cell_title'>
               <h3>Project Code</h3>
            </th>
            <th class='progress_table_cell_title'>
               <h3>Area</h3>
            </th>
            <th class='progress_table_cell_title'>
               <h3>Year</h3>
            </th>
            <th class='progress_table_cell_title'>
               <h3>Day</h3>
            </th>
            <?php foreach($sensors as $sensor): ?>
               <th class='progress_table_cell_title'>
                  <h3><?php echo $sensor['sensor_name'] ?></h3>
               </th>
            <?php endforeach ?>
         </tr>
         </thead>
         <tbody>
         <?php foreach($status_page_data as $page): ?>
            <tr id="<?php echo $page['flight_id'] ?>">
               <td class="progress_table_cell progress_table_cell_label">
                  <strong>
                     <?php echo $page['project_code'] ?>
                  </strong>
               </td>

               <td class='progress_table_cell progress_table_cell_label'>
                  <?php echo $page['area'] ?>
               </td>

               <td class='progress_table_cell progress_table_cell_label'>
                  <?php echo $page['year'] ?>
               </td>

               <td class='progress_table_cell progress_table_cell_label'>
                  <?php echo $page['day'] ?>
               </td>
               <?php
               foreach($sensors as $sensor) {
                  echo create_cell($page[strtolower($sensor['sensor_name'])]);
               }
               ?>
            </tr>
         <?php endforeach ?>
         </tbody>
      </table>
   </div>
   <br/>

   <div id="progress_table_legend">
      <table class="progress_table_legend">
         <tr>
            <td class="progress_table_cell unfunded_hover">Unfunded</td>
            <td class="progress_table_cell not_requested_hover">Not Required</td>
            <?php
            $format_string = '<td class="progress_table_cell %s_hover">%s</td>';
            foreach($progress_values as $progress) {
               if($progress['progress_name'] != 'assigned') {
                  echo sprintf($format_string, $progress['progress_name'], $progress['description']);
               }
            }
            ?>
         </tr>
      </table>
   </div>
</div>
<div id="right_side">
<div id="view_buttons">
   <input id="search_options_button" type="button" class="thin_button" value="Search Options">
</div>
<div id="selection_info" style="display: none;">
   <h2 id="flight_description"></h2>
   <hr/>
   <div id="flight_details">
      <h3>Flight Information</h3>

      <div class="info_table_row">
         <div class="info_table_title">
            <strong>Primary ticket</strong>
         </div>
         <div id="flight_ticket" class="info_table_content">
         </div>
      </div>
      <div id="flight_secondary_tickets_div">
         <div class="info_table_row">
            <div class="info_table_title">
               <strong>Secondary tickets</strong>
            </div>
            <div id="flight_secondary_tickets" class="info_table_content">
            </div>
         </div>
      </div>

      <div id="flight_kml_file_div">
         <div class="info_table_row">
            <div class="info_table_title">
               <strong>Quicklooks and KML file</strong>
            </div>
            <div id="flight_kml_file" class="info_table_content">
            </div>
         </div>
      </div>

      <div id="flight_project_info">
         <h3>Project Information</h3>

         <div id="flight_project_info_" style="display: none">
            <div class="info_table_row">
               <div class="info_table_title">
                  <strong>Principal investigator</strong>
               </div>
               <div id="flight_project_pi_" class="info_table_content">
               </div>
            </div>
            <div class="info_table_row">
               <div class="info_table_title">
                  <strong>Project code:</strong>
               </div>
               <div id="flight_project_code_" class="info_table_content">
               </div>
            </div>
            <div id="flight_project_summary_div_">
               <div class="info_table_row">
                  <strong>Project summary</strong>
               </div>
               <p id="flight_project_summary_"></p>
            </div>
            <hr/>
         </div>
      </div>
   </div>
   <div id="dataset_details">
      <h3>Dataset Information</h3>

      <div class="info_table_row">
         <div class="info_table_title">
            <strong>Sensor</strong>
         </div>
         <div id="dataset_sensor" class="info_table_content">
         </div>
      </div>
      <div id="no_flightlines_div">
         <div class='info_table_row'>
            <div class='info_table_title'>
               <strong>No. Flightlines</strong>
            </div>
            <div id="dataset_no_flightlines" class='info_table_content'>
            </div>
         </div>
      </div>
      <div class="info_table_row">
         <div class="info_table_title">
            <strong>Priority</strong>
         </div>
         <div id="dataset_priority" class="info_table_content">
         </div>
      </div>
      <div class="info_table_row">
         <div class="info_table_title">
            <strong>Processing progress</strong>
         </div>
         <div id="dataset_progress" class="info_table_content">
         </div>
      </div>
      <div id="dataset_block_div" style="display: none;">
         <div class="info_table_row">
            <div class="info_table_title">
               <strong>Reason for block</strong>
            </div>
            <div id="dataset_blocked" class="info_table_content">
            </div>
         </div>
      </div>
      <div class="info_table_row">
         <div class="info_table_title">
            <strong>Progress Chart</strong>
         </div>
      </div>
      <div>
         <canvas id="progress_chart" width="400" height="270"></canvas>
      </div>
   </div>
</div>
<div id="spinner" style="display: none;">
   <img src="../images/spinner.gif"/>
</div>
<div id="search_options">
   <form action="index.php" method="post">
      <br>

      <div class="info_table_row">
         <div class="info_table_title">
            <strong>Main Search Options</strong>
         </div>
      </div>
      <div class="info_table_row">
         <div class="info_table_title">
            Principal investigator
         </div>
         <div class="info_table_content">
            <select name="pi" class="search_select">
               <option value='all'>All PIs</option>
               <?php
               $selected_string = '';

               foreach($pis as $pi) {
                  if($pi['id'] == $search_values['pi']) {
                     $selected_string = 'selected="true" ';
                  } else {
                     $selected_string = '';
                  }
                  echo sprintf("<option %svalue='%s'>%s</option>", $selected_string, $pi['id'], $pi['pi_name']);
               }
               ?>
            </select>
         </div>
      </div>
      <div class="info_table_row">
         <div class="info_table_title">
            Project
         </div>
         <div class="info_table_content">
            <select name="project" class="search_select">
               <option value='all'>All Projects</option>
               <?php
               $selected_string = '';

               foreach($projects as $project) {
                  if($project == $search_values['project']) {
                     $selected_string = 'selected="true" ';
                  } else {
                     $selected_string = '';
                  }
                  echo sprintf("<option %svalue='%s'>%s</option>", $selected_string, $project, $project);
               }

               ?>
            </select>
         </div>
      </div>
      <div class="info_table_row">
         <div class="info_table_title">
            Exclude Project
         </div>
         <div class="info_table_content">
            <select name="exclude_proj" class="search_select">
               <option value='none'>Nothing</option>
               <?php
               $selected_string = '';

               foreach($projects as $project) {
                  if($project == $search_values['exclude_proj']) {
                     $selected_string = 'selected="true" ';
                  } else {
                     $selected_string = '';
                  }
                  echo sprintf("<option %svalue='%s'>%s</option>", $selected_string, $project, $project);
               }

               ?>
            </select>
         </div>
      </div>
      <div class="info_table_row">
         <div class="info_table_title">
            <strong>Date Search Options</strong>
         </div>
      </div>
      <div class="info_table_row">
         <div class="info_table_title">
            Start date
         </div>
         <div class="info_table_content">
            <select name="start_day" class="date_select">
               <?php
               $selected_string = '';

               for($i = 1; $i < 32; $i++) {
                  if($project == $search_values['start_day']) {
                     $selected_string = 'selected="true" ';
                  } else {
                     $selected_string = '';
                  }
                  echo sprintf("<option %svalue='%s'>%s</option>", $selected_string, $i, $i);
               }
               ?>
            </select>
            <select name="start_month" class="date_select">
               <?php
               $selected_string = '';

               foreach($months as $num => $name) {
                  if($num == $search_values['start_month']) {
                     $selected_string = 'selected="true" ';
                  } else {
                     $selected_string = '';
                  }
                  echo sprintf("<option %svalue='%s'>%s</option>", $selected_string, $num, $name);
               }
               ?>
            </select>
            <select name="start_year" class="date_year_select">
               <?php
               $selected_string = '';

               foreach($years as $year) {
                  if($year == $search_values['start_year']) {
                     $selected_string = 'selected="true" ';
                  } else {
                     $selected_string = '';
                  }
                  echo sprintf("<option %svalue='%s'>%s</option>", $selected_string, $year, $year);

               }
               ?>
            </select>
         </div>
      </div>
      <div class="info_table_row">
         <div class="info_table_title">
            End date
         </div>
         <div class="info_table_content">
            <select name="end_day" class="date_select">
               <?php
               $selected_string = '';

               for($i = 1; $i < 32; $i++) {
                  if($project == $search_values['end_day']) {
                     $selected_string = 'selected="true" ';
                  } else {
                     $selected_string = '';
                  }
                  echo sprintf("<option %svalue='%s'>%s</option>", $selected_string, $i, $i);
               }
               ?>
            </select>
            <select name="end_month" class="date_select">
               <?php
               $selected_string = '';

               foreach($months as $num => $name) {
                  if($num == $search_values['end_month']) {
                     $selected_string = 'selected="true" ';
                  } else {
                     $selected_string = '';
                  }
                  echo sprintf("<option %svalue='%s'>%s</option>", $selected_string, $num, $name);
               }
               ?>
            </select>
            <select name="end_year" class="date_year_select">
               <?php
               $selected_string = '';

               foreach($years as $year) {
                  if($year == $search_values['end_year']) {
                     $selected_string = 'selected="true" ';
                  } else {
                     $selected_string = '';
                  }
                  echo sprintf("<option %svalue='%s'>%s</option>", $selected_string, $year, $year);

               }
               ?>
            </select>
         </div>
      </div>
      <input type="submit" id="submit" name="submit" value="Refine search" class="big_button"/>
      <br/>
      <br/>
   </form>
</div>
</div>
<div id="arsf_footer">
</div>
</body>
</html>

