<?php

/**
 * Pad the julian day with 0 if its only 2 digits
 * @param string $string Julian Day with or without a sortie
 *
 * @return string Padded Julian Day
 */
function zero_pad($string)
{
   $pad_length = 3;

   if(!ctype_digit($string)) {
      $pad_length += 1;
   }

   return str_pad($string, $pad_length, '0', STR_PAD_LEFT);
}

/**
 * Converts a string from the database into HTML
 * @param string $string Cell from database
 *
 * @return string HTML
 */
function create_cell($string)
{


   if($string != null) {
      list($id, $priority, $status, $user) = explode('|', $string);

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

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


      if($priority == "unfunded") {
         $td_open = sprintf($td_open, "unfunded", $id);
      } else {
         $td_open = sprintf($td_open, $status, $id);
      }


      return $td_open . $user . '</td>';

   } 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"/>
   <link rel="stylesheet" type="text/css" href="../style/jquery.alerts.css"/>
   <!--[if IE]>
   <script type="text/javascript" src="../explorecanvas/excanvas.js"></script>
   <![endif]-->
   <!-- jquery -->
   <script type="text/javascript" src="../common/jquery.min.js"></script>
   <script type="text/javascript" src="../common/fht.js"></script>
   <script src="../common/common.js"></script>
   <script src="../common/jquery.alerts.js"></script>
   <!--<script src="edit.js"></script>-->
   <script src="edit_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="table_and_info">
<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 zero_pad($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>
            <?php
            $format_string = '<td class="progress_table_cell %s_hover">%s</td>';
            foreach($progress_values as $progress) {
               echo sprintf($format_string, $progress['progress_name'], $progress['description']);

            }
            ?>
         </tr>
      </table>
   </div>


</div>


<div id="right_side">
<div id="view_buttons">
   <form action="index.php" method="post">
      <input type="submit" class="thin_button" name="switch_to_order" value="Switch to edit view"><br/>
      <!--<input type="submit" class="thin_button" name="switch_to_progress" value="Switch to progress view">-->
   </form>
   <input id="search_options_button" type="button" class="thin_button" value="Search Options">
</div>

<div id="search_options">
   <form action="order.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 id="dangerous" style="display: none;">
   <input type="button" value="Show dangerous stuff" class="thin_button" id="show_dangerous_button"/>

   <div id="dangerous_buttons" style="display: none;">
      <h3>Please don't press unless you know what you're doing...</h3>
      <input type="button" value="Remove dataset" class="thin_button dangerous_button" id="remove_dataset"/>
      <input type="button" value="Remove flight" class="thin_button dangerous_button" id="remove_flight"/>
      <input type="button" value="Show progress details" class="thin_button dangerous_button"
           id="show_progress_button"/>

      <div id="progress_info" style="display: none;">
         <strong>Progress details are recorded so that we keep an accurate record of how long things are taking to
            get done. Please don't modify them unless absolutely necessary!</strong>

         <div class="info_table_row">
            <div class="info_table_title">
               <strong>Progress details</strong>
            </div>
         </div>
         <div class="info_table_row" style="display: none;">
            <div class="info_table_title">
               Number of progress stamps
            </div>
            <div class="info_table_content">
               <input type="text" id="number_progress_stamps"/>
            </div>
         </div>
         <div class="info_table_row">
            <div class="info_table_title">
               Time to delivery check
            </div>
            <div class="info_table_content">
               <input type="text" id="dataset_del_check_time"/>
            </div>
         </div>
         <div class="info_table_row">
            <div class="info_table_title">
               Time to complete
            </div>
            <div class="info_table_content">
               <input type="text" id="dataset_time_to_complete"/>
            </div>
         </div>
         <hr/>
         <div id="progress_info_" style="display: none;">
            <div class="info_table_row">
               <div class="info_table_title">
                  Progress
               </div>
               <div class="info_table_content">
                  <select id="progress_stamp_progress_">
                     <?php foreach($progress_values as $progress): ?>
                        <option class="<?php echo $progress['progress_name'] ?>_hover" value="<?php echo $progress['progress_name'] ?>"><?php echo $progress['description'] ?></option>
                     <?php endforeach ?>
                  </select>
               </div>
            </div>
            <div class="info_table_row">
               <div class="info_table_title">
                  User
               </div>
               <div class="info_table_content">
                  <input type="text" id="progress_stamp_user_"/>
                  <input type="hidden" id="progress_stamp_id_"/>
               </div>
            </div>
            <div class="info_table_row">
               <div class="info_table_title">
                  Date
               </div>
               <div class="info_table_content">
                  <input type="text" id="progress_stamp_date_"/>
               </div>
            </div>
            <div class="info_table_row">
               <div class="info_table_title">
                  Notes
               </div>
               <div class="info_table_content">
                  <input type="text" id="progress_stamp_notes_"/>
               </div>
            </div>
            <input type="button" value="Remove timestamp" class="thin_button" id="remove_progress_"/>
            <hr/>
         </div>
      </div>
   </div>
</div>

<div id="selection_info" style="display:none;">
<div id="dataset_info" style="display: none;">
   <div id="dataset_progress_info">
      <div class="info_table_row">
         <div class="info_table_title">
            <strong>Dataset info</strong>
         </div>
      </div>
      <div class="info_table_row">
         <div class="info_table_title">
            Set new progress
         </div>
         <div class="info_table_content">
            <select id="dataset_progress">
               <?php foreach($progress_values as $progress): ?>
                  <option class="<?php echo $progress['progress_name'] ?>_hover" value="<?php echo $progress['progress_name'] ?>"><?php echo $progress['description'] ?></option>
               <?php endforeach ?>
            </select>
         </div>
      </div>
      <div class="info_table_row">
         <div class="info_table_title">
            Set new priority
         </div>
         <div class="info_table_content">
            <select id='dataset_priority'>
               <?php foreach($priority_values as $priority): ?>
                  <option value="<?php echo $priority['priority_name'] ?>"><?php echo $priority['description'] ?></option>
               <?php endforeach ?>
            </select>
         </div>
      </div>
      <div class="info_table_row">
         <div class="info_table_title">
            Set priority offset
         </div>
         <div class="info_table_content">
            <select id='dataset_priority_offset'>
               <?php for($i = 10; $i >= -10; $i--): ?>
                  <option value='<?php echo $i ?>'><?php echo $i ?></option>
               <?php endfor ?>
            </select>
         </div>
      </div>
      <div class="info_table_row">
         <div class="info_table_title">
            Number of flightlines
         </div>
         <div class="info_table_content">
            <input type="text" id="dataset_flightlines"/>
         </div>
      </div>
      <input id="block_dataset" type="button" class="thin_button" value="Block dataset">
   </div>
</div>
<div id="general_flight_info">
<input type="button" value="Flight details" class="short_button thin_button" id="show_flight_button"/>
<input type="button" value="Project details" class="short_button thin_button" id="show_project_button"/>
<input type="button" value="PI details" class="short_button thin_button" id="show_pi_button"/>

<div id="flight_info" style="display: none;">
   <table>
      <thead>
      <tr>
         <td colspan="2">
            Flight details
         </td>
      </tr>
      </thead>
      <tbody>
      <tr>
         <td>Link to ticket</td>
         <td><span id="flight_ticket_link"></span></td>
      </tr>
      <tr>
         <td>Data location</td>
         <td><input type="text" id="flight_data_location"/></td>
      </tr>
      <tr>
         <td>Flight year</td>
         <td><input type="text" id="flight_year"/></td>
      </tr>
      <tr>
         <td>Julian day</td>
         <td><input type="text" id="flight_jday"/></td>
      </tr>
      <tr>
         <td>Flight letter</td>
         <td><input type="text" id="flight_letter"/></td>
      </tr>
      <tr>
         <td>Primary ticket</td>
         <td><input type="text" id="flight_primary_ticket"/></td>
      </tr>
      <tr>
         <td>Secondary tickets</td>
         <td><input type="text" id="flight_secondary_tickets"/></td>
      </tr>
      <tr>
         <td>Flight area</td>
         <td><input type="text" id="flight_area"/></td>
      </tr>
      <tr>
         <td>Flight KML weblink</td>
         <td><input type="text" id="flight_kmlweblink"/></td>
      </tr>
      <tr>
         <td>Boresight Flight</td>
         <td><span id="flight_boresite"></span></td>
      </tr>
      <tr>
         <td>Flight Priority</td>
         <td>
            <select id="flight_priority">
               <?php foreach($priority_values as $priority): ?>
                  <option value="<?php echo $priority['priority_name'] ?>"><?php echo $priority['description'] ?></option>
               <?php endforeach ?>
            </select>
         </td>
      </tr>
      <tr>
         <td colspan="2">Flight Notes</td>
      </tr>
      <tr>
         <td colspan="2"><textarea id="flight_notes"></textarea></td>
      </tr>
      </tbody>
   </table>
</div>
<div id="project_details" style="display: none;">
   <div id="project_info">
      <div id="project_info_" style="display: none;">
         <div class="info_table_row" id="change_project_div_" style="display: none;">
            <div class="info_table_title">
               Select a different project:
            </div>
            <div class="info_table_content">
               <select id="project_select_">
                  <?php foreach($projects as $project): ?>
                     <option value='<?php echo $project; ?>'><?php echo $project; ?></option>
                  <?php endforeach ?>
               </select>
            </div>
         </div>

         <div id="project_details_div_">
            <table>
               <thead>
               <tr>
                  <td colspan="2">
                     Project details
                  </td>
               </tr>
               </thead>
               <tbody>
               <tr>
                  <td>Project code</td>
                  <td><span id="project_code_"></span></td>
               </tr>
               <tr>
                  <td>Project EUFAR code</td>
                  <td><input type="text" id="project_eufar_code_"/></td>
               </tr>
               <!--<tr>
                        <td>Project Priority</td>
                        <td>
                            <select id="project_priority_">
                                <?php //foreach($priority_values as $priority): ?>
                                    <option value="<?php //echo $priority['priority_name'] ?>"><?php //echo $priority['description'] ?></option>
                                <?php //endforeach ?>
                            </select>
                        </td>
                    </tr>-->
               <tr>
                  <td colspan="2">Project summary</td>
               </tr>
               <tr>
                  <td colspan="2"><textarea id="project_summary_"></textarea></td>
               </tr>
               <tr>
                  <td colspan="2">Project objective</td>

               </tr>
               <tr>
                  <td colspan="2"><textarea id="project_objective_"></textarea></td>
               </tr>
               </tbody>
            </table>
         </div>
         <input type="button" value="Select a different project" class="thin_button" id="change_project_"/>
         <input type="button" value="Remove project" class="thin_button" id="remove_project_"/>
         <hr/>
      </div>
   </div>
   <div class="info_table_row" id="add_project_div" style="display: none;">
      <div class="info_table_title">
         Select a project to add:
      </div>
      <div class="info_table_content">
         <select id="add_project_select">
            <?php foreach($projects as $project): ?>
               <option value='<?php echo $project; ?>'><?php echo $project; ?></option>
            <?php endforeach ?>
         </select>
      </div>
   </div>
   <input type="button" value="Add another project" class="thin_button" id="add_project"/>
</div>
<div id="pi_details" style="display: none;">
   <div id="pi_info">
      <div id="pi_info_" style="display: none;">
         <div class="info_table_row" id="change_pi_div_" style="display: none;">
            <div class="info_table_title">
               Select a different PI:
            </div>
            <div class="info_table_content">
               <select id="pi_select_">
                  <option value="new_pi">Add new PI</option>

               </select>
            </div>
         </div>
         <div id="pi_details_div_">
            <table>
               <thead>
               <tr>
                  <td colspan="2">
                     PI Details
                  </td>
               </tr>
               </thead>
               <tbody>
               <tr>
                  <td>Project code</td>
                  <td><span id="pi_proj_"></span></td>
               </tr>
               <tr>
                  <td>PI name</td>
                  <td><input type="text" id="pi_name_"/><input type="hidden" id="pi_id_"/></td>
               </tr>
               <tr>
                  <td>PI email</td>
                  <td><input type="text" id="pi_email_"/></td>
               </tr>
               <tr>
                  <td colspan="2">PI address</td>
               </tr>
               <tr>
                  <td colspan="2"><textarea id="pi_address_"></textarea></td>
               </tr>
               </tbody>
            </table>
         </div>
         <!--<input type="button" value="Select a different PI" class="thin_button" id="change_pi_"/>
         <input type="button" value="Remove PI" class="thin_button" id="remove_pi_"/>-->
         <hr/>
      </div>
   </div>
   <!--<input type="button" value="Add another PI" class="thin_button" id="add_pi"/>-->
</div>
</div>
</div>
<div id="new_dataset" style="display: none">
   <h3>Add a new dataset</h3>

   <div class="info_table_row">
      <div class="info_table_title">
         Number of flightlines
      </div>
      <div class="info_table_content">
         <input type="number" min="0" max="100" id="new_dataset_lines"/>
      </div>
   </div>
   <div class="info_table_row">
      <div class="info_table_title">
         Dataset priority
      </div>
      <div class="info_table_content">
         <select id="new_dataset_priority">
            <?php foreach($priority_values as $priority): ?>
               <option value="<?php echo $priority['priority_name'] ?>"><?php echo $priority['description'] ?></option>
            <?php endforeach ?>
         </select>
      </div>
   </div>
   <input class="big_button" type="button" value="Add dataset" id="new_dataset_button"/>
</div>
<div id="spinner" style="display: none;">
   <img src="../images/spinner.gif"/>
</div>
<input id="update_button" type="button" value="Update" class="big_button" style="display: none;"/>

</div>


</div>
</body>
</html>

