<?php
include_once ("../include/auth.inc.php");
include_once ("../include/global.inc.php");
include_once ("../locating/hha_alarm.inc.php");

getSecHttpVars("1", array("loc_created_ids", "pwd", "upd_pwd"));
//echo "alert(\"" . str_replace('"', "'", $ret_value) . "\");";

$ret_value = "";

if (trim($upd_pwd) != "") {
  $ret_value = "fail";
//echo "UPDATE user SET usr_password = PASSWORD('" . $upd_pwd . "') WHERE usr_id = " . $_SESSION['usr_id'] . " AND usr_password = PASSWORD('" . $pwd . "')" . "\n";

  // $res = $db->query("UPDATE user SET usr_password = PASSWORD('" . $upd_pwd . "') WHERE usr_id = " . $_SESSION['usr_id'] . " AND usr_password = PASSWORD('" . $pwd . "')");
  // if ($db->affected_rows > 0)
  //   $ret_value = "ok";
  $res = $db->exec("UPDATE user SET usr_password = PASSWORD('" . $upd_pwd . "') WHERE usr_id = " . $_SESSION['usr_id'] . " AND usr_password = PASSWORD('" . $pwd . "')");
  if ($res > 0)
    $ret_value = "ok";
  echo "retValue = '" . $ret_value . "';\n";
}

if (trim($loc_created_ids) != "") {
  $loc_created_idsArr = explode(";", $loc_created_ids);
  $loc_created_ids = "";
  for ($i = 0; $i < count($loc_created_idsArr) - 1; $i++) {
    $loc_created_idsArrArr = explode("|", $loc_created_idsArr[$i]);
    $sqlquery = "SELECT loc_type, ROUND(loc_long, 6) AS loc_long, ROUND(loc_lat, 6) AS loc_lat, loc_acc, loc_created" .
      " FROM phoenix_log.locating WHERE usr_id = " . $loc_created_idsArrArr[0] . " AND loc_created > '" . $loc_created_idsArrArr[1] . "'" .
      " AND loc_type != 3 AND loc_type != 14 AND loc_type != 9 " .
      " ORDER BY loc_created DESC";
    $res1 = $db->query($sqlquery);
    if (DB::isError($res1)) die ("$PHP_SELF: '$sqlquery' : " . $res1->getMessage());
    //$cnt = $res1->numRows();
    $prev_loc_lat = 0.0;
    $prev_loc_long = 0.0;
    $pref_loc_created = "0000-00-00 00:00:00";
//    $first_loc_lat = 0.0;
//    $first_loc_long = 0.0;
//    $first_loc_created = "0000-00-00 00:00:00";
    $first_loc_lat = $loc_created_idsArrArr[3];
    $first_loc_long = $loc_created_idsArrArr[4];
    $first_loc_created = $loc_created_idsArrArr[1];
    $cnt = 0;
    $rows = array();
    while ($row1 = $res1->fetch_assoc()) {
      if (($row1["loc_long"] != $prev_loc_long && $row1["loc_lat"] != $prev_loc_lat)) {
      	if ($cnt == 0) {
      	  $first_loc_created = $row1["loc_created"];
          $first_loc_lat = $row1["loc_lat"];
          $first_loc_long = $row1["loc_long"];
      	}
        $rows[] = $row1;
        $cnt++;
        $prev_loc_lat = $row1["loc_lat"];
        $prev_loc_long = $row1["loc_long"];
        $prev_loc_created = $row1["loc_created"];
      }
    }
    $res1->free();
    
    $all_cnt = $loc_created_idsArrArr[2] + $cnt;
    $loc_created_ids .= $loc_created_idsArrArr[0] . "|" . $first_loc_created . "|" . $all_cnt . "|" . $first_loc_lat . "|" . $first_loc_long . ";";
    
    $prev_loc_lat = $loc_created_idsArrArr[3];
    $prev_loc_long = $loc_created_idsArrArr[4];
    $prev_loc_created = $loc_created_idsArrArr[1];
    if (count($rows) > 0)
      $ret_value .= "  [\n";
    for ($j = 0; $j < count($rows); $j++) {
      $row1 = $rows[$j];
      $direction = getDirection($row1["loc_long"], $row1["loc_lat"], $prev_loc_long, $prev_loc_lat);
      $ret_value .= "    [" . $loc_created_idsArrArr[0] . ", " . $all_cnt-- . ", " . $row1["loc_lat"] . ", " .  $row1["loc_long"] . ", '" . $direction  . "', '" . formDateTime($row1["loc_created"]) . "'" . "],\n";
      $prev_loc_long = $row1["loc_long"];
      $prev_loc_lat = $row1["loc_lat"];
      $prev_loc_created = $row1["loc_created"];
    }
    if (count($rows) > 0)
      $ret_value .= "  ],\n";
  }
}

if (trim($upd_pwd) == "")
  echo "retValue = [\n  ['" . $loc_created_ids . "'],\n" . $ret_value . "];\n";
?>
