<?php
// Tobedone:
// - Adressen checken ob sie von Google Maps gefunden wurden
// - Problem, wenn Reihenfolge nicht eingehalten (Station in der Mitte unerledigt, die davor und danach jedoch schon erledigt)
include_once("../include/caglobal.inc.php");
include_once("../include/ranking.inc.php");

$country_codes = array(
  "AL" => "Albania",
  "AT" => "Austria",
  "BE" => "Belgium",
  "BG" => "Bulgaria",
  "CH" => "Switzerland",
  "CZ" => "Czech Republic",
  "DE" => "Germany",
  "DK" => "Denmark",
  "ES" => "Spain",
  "FI" => "Finland",
  "FR" => "France",
  "GB" => "United Kingdom",
  "GR" => "Greece",
  "HR" => "Croatia",
  "HU" => "Hungary",
  "IE" => "Ireland",
  "IS" => "Iceland",
  "IT" => "Italy",
  "LI" => "Liechtenstein",
  "LT" => "Lithuania",
  "LU" => "Luxembourg",
  "LV" => "Latvia",
  "MC" => "Monaco",
  "MK" => "Macedonia",
  "NL" => "Netherlands",
  "NO" => "Norway",
  "PL" => "Poland",
  "PT" => "Portugal",
  "RO" => "Romania",
  "SE" => "Sweden",
  "SI" => "Slovenia",
  "SK" => "Slovakia",
  "UA" => "Ukraine");

function showInGoogleMaps($jb_id) {
  global $usr_id, $country_codes;

  // gespeicherte Job-Daten holen
  list($hq_id, $cr_id, $cr_sid) = getFieldsValueFromId("job", "jb_id", $jb_id, array("hq_id", "cr_id", "cr_sid"));
//  if ($hq_id != 3 && $hq_id != 2)
//    return "";
  $debug_text = "\$cr_id = " . $cr_id . ", \$cr_sid = " . $cr_sid . "\n";
  $points = "";
  $points_txt = "";
  if ($cr_id != 0) {
    $locatingStateArray = checkLocatingState($cr_id);    
    $debug_text .= "\$locatingStateArray = " . json_encode($locatingStateArray) . "\n";
    if ($locatingStateArray[0] == "0") {
      $points = "/" . $locatingStateArray[2] . "," . $locatingStateArray[1];
      $points_txt = "(" . $locatingStateArray[2] . ", " . $locatingStateArray[1] . ")";
    }
  }
  $debug_text .= "\$points = " . $points . "\n";

  $i = 1;
  $prevStopMap = "";
  $prevStopText = "";
  do {
    $tourFound = 0;
    // gespeicherte Tour-Daten holen
    if (existsEntry("tour", array("jb_id", $jb_id, "tr_sort", $i))) {
      $tourFound = 1;
      list($csc_id, $tr_comp, $tr_person, $ad_street, $tr_hsno,
        $ad_zipcode, $ad_city, $ad_country, $tr_remark, $tr_status, $tr_ware_from_to, $tr_commission_no, $tr_mediationarea_id, $tr_status, $tr_sign, $tr_signname, $tr_finishtime, $tr_id) =
        getTourData($jb_id, $i, "tour");
      $debug_text .= "\$tr_sort = " . $i . ", \$tr_status = " . $tr_status . "\n";

      $country_code = $country_codes[($ad_country == "D" ? "DE" : $ad_country)];
      if ($country_code == "Germany")
        $country_code = "";

      $endStopMap = "/" . str_replace("/", "%2F", my_str_check($ad_street)) . " " . str_replace("/", "%2F", $tr_hsno) . ", " . str_replace("/", "%2F", $ad_zipcode) . " " .
        str_replace("/", "%2F", my_str_check($ad_city)) . ($country_code == "" ? "" : "," . $country_code);
      $endStopText = my_str_check($ad_street) . " " . $tr_hsno . ", " . ($country_code == "" ? "" : $country_code . " ") . $ad_zipcode . " " . my_str_check($ad_city);

      if ($tr_status == 1 && $points == "") {
        $prevStopMap = $endStopMap;
        $prevStopText = $endStopText;
      }
      $i++;
    }
  } while ($tourFound == 1 && $tr_status == 1);

  $debug_text .= "\$prevStopMap = " . $prevStopMap . "\n";
  $debug_text .= "\$endStopMap = " . $endStopMap . "\n";

  $retStr = "";
  if (($points != "" || $prevStopMap != "") && $endStopMap != "") {
    $points .= $prevStopMap . $endStopMap;
    $points_txt .= $prevStopText . " &minus;&gt; " . $endStopText;
    $retStr = "<a href=\"https://www.google.de/maps/dir" . $points . "/" . "\" target=\"_blank\">" . $points_txt . "</a>";
  } elseif ($points == "" && $prevStopMap == "") {
    $retStr .= "<i>Keine&nbsp;aktuellen&nbsp;GPS&minus;Daten&nbsp;von&nbsp;" . $cr_sid . "&nbsp;verfügbar&nbsp;und&nbsp;erste&nbsp;Station&nbsp;noch&nbsp;nicht&nbsp;erledigt</i>";
  } elseif ($endStopMap == "") {
    $retStr .= "<i>Keine&nbsp;unerledigte&nbsp;Station&nbsp;mehr&nbsp;vorhanden</i>";
  }
  $debug_text .= "\$retStr = " . $retStr . "\n";

  writeLog_showInGoogleMaps("\$jb_id = " . $jb_id . ", \$usr_id = " . $usr_id . "\n" . $debug_text);
  return $retStr;
}

function writeLog_showInGoogleMaps($log_text) {
  $today = getdate();
//  echo $log_text . "\n"; die();
  $fileHandle = @fopen("../log/showInGoogleMaps_" . $today['year'] . sprintf("%02d", $today['mon']) . ".log", 'a');
  @fwrite($fileHandle, "[" . date("Y-m-d H:i:s") . "] " . $log_text . "\n");
  @fclose($fileHandle);
  return;
}
?>
