require_api('helper_api.php');
require_api('history_api.php');
require_api('lang_api.php');
require_api('print_api.php');
require_api('string_api.php');
$t_access_level_needed = config_get('view_history_threshold');
if (!access_has_bug_level($t_access_level_needed, $f_bug_id)) {
    return;
}
?>

<a id="history"></a><br />

<?php 
collapse_open('history', '', 'table-container');
$t_history = history_get_events_array($f_bug_id);
?>
<table>
	<thead>
		<tr>
			<td class="form-title" colspan="4">
<?php 
collapse_icon('history');
echo lang_get('bug_history');
?>
			</td>
		</tr>

		<tr class="row-category-history">
			<th class="small-caption">
				<?php 
 /**
  * Get last change values for:
  * - Summary
  * - Priorität
  * - Produktversion
  * - Zielversion
  * - Behoben in Version
  * - Status
  * - Lösung
  * - Reproduzierbarkeit
  * - Sichtbarkeit
  * - Auswirkung
  * - Bearbeiter
  * - Plattform
  * - OS
  * - OS Version
  *
  * @param $bug_id
  * @param $version_date
  * @param $int_filter_string
  * @return array
  */
 public function calculate_last_change($bug_id, $version_date, $int_filter_string)
 {
     $output_value = null;
     $spec_filter_string = lang_get($int_filter_string);
     $min_time_difference = 0;
     $min_time_difference_event_id = 0;
     $bug_history_events = history_get_events_array($bug_id);
     for ($event_index = 0; $event_index < count($bug_history_events); $event_index++) {
         $bug_history_event = $bug_history_events[$event_index];
         if ($bug_history_event['note'] == $spec_filter_string) {
             $bug_history_event_date = strtotime($bug_history_event['date']);
             $local_time_difference = $version_date - $bug_history_event_date;
             /* initial value */
             if ($min_time_difference == 0) {
                 $min_time_difference = $local_time_difference;
                 $min_time_difference_event_id = $event_index;
             }
             /* overwrite existing if it is closer to event date */
             if ($min_time_difference > $local_time_difference) {
                 $min_time_difference = $local_time_difference;
                 $min_time_difference_event_id = $event_index;
             }
         }
     }
     $output_change = null;
     $output_values = null;
     if (!empty($bug_history_events)) {
         $output_change = $bug_history_events[$min_time_difference_event_id]['change'];
         $output_values = explode(' => ', $output_change);
     }
     if ($min_time_difference <= 0) {
         $output_value = $output_values[0];
     } else {
         $output_value = $output_values[1];
     }
     /**
      * TODO kann wegen der Feldbezeichnung eventuell noch zu Problemen führen.
      */
     if (strlen($output_change) == 0) {
         $output_value = bug_get_field($bug_id, $int_filter_string);
     }
     return $output_value;
 }