<?php

include_once("../include/auth.inc.php");
include_once("../include/global.inc.php");
$log_file_name = "../log/statistic_finishment_" . date("Ym") . ".log";

getSecHttpVars("1",array("cs_id", "attr", "val"));

myWriteLog("\$cs_id = $cs_id, \$attr = $attr, \$val = $val");

if ($attr == "Lademaße" || $attr == "Dispo-Info") {
  if ($attr == "Lademaße")
    $par_key = "RANKING_JB2CRVH_MEASURE_CS_IDS";
  if ($attr == "Dispo-Info")
    $par_key = "JB_DISPOINFO_ENABLED_CS_IDS";
  
  $cs_ids = array();
  $sql_query =
    "SELECT par_text FROM parameter WHERE par_key = '" . $par_key . "' AND hq_id = " . $hq_id;
  $res = $db->query($sql_query);
  if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query' : " . $res->getMessage());
  
  if ($row = ($phpVersion >= "8.0" ? $res->fetch_assoc() : $res->fetchRow())) {
    $cs_ids = explode(",", $row['par_text']);
  }
  
  $new_par_text = "";
  if ($val == 0) {
    for ($i = 0; $i < count($cs_ids); $i++) {
      if ($cs_ids[$i] != $cs_id)
        $new_par_text .= $cs_ids[$i] . ",";
    }
    if ($new_par_text != "")
      $new_par_text = substr($new_par_text, 0, -1);
  }
  
  if ($val == 1) {
    $new_par_text = $row['par_text'] . "," . $cs_id;
  }
  $res->free();
  
  $sql_query =
    "UPDATE parameter SET par_text = '" . $new_par_text . "' WHERE par_key = '" . $par_key . "' AND hq_id = " . $hq_id;
  $res = $db->query($sql_query);
  if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query' : " . $res->getMessage());
}

if ($attr == "Orte") {
  if ($val == 0) {
    $sql_query =
      "DELETE FROM parameter WHERE par_key IN ('CUSTOMER_MASK_JOBLIST_ADD_ON_ALIGNS', 'CUSTOMER_MASK_JOBLIST_ADD_ON_FIELDS', 'CUSTOMER_MASK_JOBLIST_ADD_ON_TITLES') AND emp_id = " . $cs_id;
    $res = $db->query($sql_query);
    if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query' : " . $res->getMessage());
  }
  if ($val == 1) {
    $sql_query =
      "INSERT INTO parameter (par_key, md_id, hq_id, emp_id, par_value) VALUES ('CUSTOMER_MASK_JOBLIST_ADD_ON_ALIGNS', 0, 0, " . $cs_id . ", 'l')";
    $res = $db->query($sql_query);
    if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query' : " . $res->getMessage());
    $sql_query =
      "INSERT INTO parameter (par_key, md_id, hq_id, emp_id, par_value) VALUES ('CUSTOMER_MASK_JOBLIST_ADD_ON_FIELDS', 0, 0, " . $cs_id . ", 'jb_tourcities')";
    $res = $db->query($sql_query);
    if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query' : " . $res->getMessage());
    $sql_query =
      "INSERT INTO parameter (par_key, md_id, hq_id, emp_id, par_value) VALUES ('CUSTOMER_MASK_JOBLIST_ADD_ON_TITLES', 0, 0, " . $cs_id . ", 'Stationen')";
    $res = $db->query($sql_query);
    if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query' : " . $res->getMessage());
  }
}

if ($attr == "Stadtteile" || $attr == "Kartenansicht" || $attr == "Barzahler" || $attr == "ManuelleDisposition" /* || $attr == "Beweisfoto" || $attr == "Servicepreis" */) {
  if ($attr == "Stadtteile") {
    $par_key = "MASK_INSERTADDRESS_DISTRICT";
    $par_value = "Berlin";
  }
  if ($attr == "Kartenansicht") {
    $par_key = "MASK_JB_MAP_VIEW_ENABLED";
    $par_value = "1";
  }
  if ($attr == "Barzahler") {
    $par_key = "MASK_CUST_SET_PAYER";
    $par_value = "1";
  }
  if ($attr == "ManuelleDisposition") {
    $par_key = "CUSTOMER_MANUAL_DISPO";
    $par_value = "1";
  }
//  if ($attr == "Beweisfoto") {
//    $par_key = "MASK_TR_PHOTO_CS";
//    $par_value = "2";
//  }
  if ($val == 0) {
    $sql_query =
      "DELETE FROM parameter WHERE par_key = '" . $par_key . "_" . $cs_id . "' ";
    myWriteLog("\$sql_query = $sql_query");
    $res = $db->query($sql_query);
    if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query' : " . $res->getMessage());
//    if ($attr == "Beweisfoto") {
//      $sql_query =
//        "DELETE FROM parameter WHERE par_key = 'MASK_MIN_MAX_TR_PHOTO_CS_" . $cs_id . "' ";
//      myWriteLog("\$sql_query = $sql_query");
//      $res = $db->query($sql_query);
//      if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query' : " . $res->getMessage());
//    }
//    if ($attr == "Servicepreis") {
//      $sql_query =
//        "DELETE FROM parameter WHERE par_key = 'INV_PRINT_SRVPRICE_" . $cs_id . "' ";
//      myWriteLog("\$sql_query = $sql_query");
//      $res = $db->query($sql_query);
//      if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query' : " . $res->getMessage());
//    }
  }
  if ($val == 1) {
    $sql_query =
      "INSERT INTO parameter (par_key, md_id, hq_id, emp_id, par_value, par_text) VALUES ('" . $par_key . "_" . $cs_id . "', 0, 0, 0, '" . $par_value . "', '')";
    myWriteLog("\$sql_query = $sql_query");
    $res = $db->query($sql_query);
    if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query' : " . $res->getMessage());
//    if ($attr == "Beweisfoto") {
//      $sql_query =
//        "INSERT INTO parameter (par_key, md_id, hq_id, emp_id, par_value, par_text) VALUES ('MASK_MIN_MAX_TR_PHOTO_CS_" . $cs_id . "', 0, 0, 0, '0|1', '')";
//      myWriteLog("\$sql_query = $sql_query");
//      $res = $db->query($sql_query);
//      if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query' : " . $res->getMessage());
//    }
  }
}

if ($attr == "AnzahlFotos") {
  $sql_query =
    "UPDATE parameter SET par_value = ('" . $val . "') WHERE par_key = 'MASK_MIN_MAX_TR_PHOTO_CS_" . $cs_id . "'";
  myWriteLog("\$sql_query = $sql_query");
  $res = $db->query($sql_query);
  if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query' : " . $res->getMessage());
}

if ($attr == "PZM-Modus") {
  $hq_id = $cs_id;
  $sql_query =
    "UPDATE parameter SET par_value = '" . ($val == "Rundtour" ? "1" : "0") . "' WHERE par_key = 'PZM_ROUNDTRIPKM' AND hq_id = " . $hq_id;
  myWriteLog("\$sql_query = $sql_query");
  $res = $db->query($sql_query);
  if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query' : " . $res->getMessage());
}

if ($attr == "Routenberechnung") {
  $hq_id = $cs_id;
  $sql_query =
    "UPDATE parameter SET par_value = '" . ($val == "Absolut_kuerzeste" ? "2" : ($val == "Kuerzeste_von_drei" ? "1" : "0")) . "' WHERE par_key = 'PZM_SHORTEST' AND hq_id = " . $hq_id;
  myWriteLog("\$sql_query = $sql_query");
  $res = $db->query($sql_query);
  if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query' : " . $res->getMessage());
}

if (substr($attr, -4) == "_eap") {

  $hq_id = $cs_id;
  $url = "http://" . $dbhostPZM . "/api/zones/" . $hq_id;
  $postArray = array(
    "zone" => str_replace("_eap", "", $attr),
    "eap" => ($val == 1 ? true : false)
  );
  $jsonData = json_encode(array($postArray));

  myWriteLog("\$jsonData = " . $jsonData);
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);

  $json_response = curl_exec($ch);
  $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close($ch);
//  myWriteLog("get_zone_distance: [" . $json_response . "] [" . $status . "]");
//  $responseArr = json_decode($json_response, true);

  if ($status != 200) {
    myWriteLog("Error: call to URL " . $url . " failed with status " . $status . ", response \"" . $json_response . "\", curl_error " . curl_error($ch) . ", curl_errno " . curl_errno($ch));
    echo "retValue = \"Error\";\n";
    exit();
  }
}

echo "retValue = \"ok\";\n";

function myWriteLog($log_text) {
  global $log_file_name;

  $fileHandle = @fopen($log_file_name, 'a');
  @fwrite($fileHandle, "[" . date("Y-m-d H:i:s") . "] " . $log_text .  "\n");
  @fclose($fileHandle);
}
?>  
