<?php 
  include "settings.php";
  include "logger.php";
  
  $db = new mysqli($DB_HOST,$DB_USER,$DB_PASS,$DB_NAME);
  $db->query("SET CHARSET SET utf8");
  $db->query("SET NAMES 'utf8'");
  if (mysqli_connect_errno()) {
    echo "Error connecting to DB";
    exit;
  }
  
  /*$DB_USER = 'root';
  $DB_PASS = '1111';
  $DB_HOST = '192.168.0.105';
  $DB_NAME = 'uf';*/
  
	writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroxml <> '.$_POST['data'] );

	$data = $_POST['data'];
	$data = stripslashes($data);
	$arraydata = (array)json_decode($data);
  
  
	function insertData($xml,$login,$password){
		
	}
  
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
  if ($arraydata['func']=='InPoinTrip')
  {
	  $datetime = $arraydata["datetime"];
	  $InPointGeoX = $arraydata["InPointGeoX"];
	  $InPointGeoY = $arraydata["InPointGeoY"];
	  $id_trip = $arraydata["id_trip"];
	  $id_point = $arraydata["id_point"];
	  $login = $arraydata["id_user"];
	  $login = $arraydata["login"];
	  $password = $arraydata["pwd"];
	  
	  $xml = '<Data Login="'.$login.'" Password="'.$password.'">
			  <p type="DriverGetToTripPoint" TripPointID="'.$id_point.'" TripID="'.$id_trip.'" IMEI="" geoX="'.$InPointGeoX.'" geoY="'.$InPointGeoY.'" />
			  </Data>';
	  
	  $query = "INSERT INTO `tms_input_web_data` (`date`,`login`,`packet`,`password`) VALUES ('".date('Y-m-d H:i:s')."','".$login."','".$xml."','".$password."')";
      if ($stmt = $db->prepare($query)){
      $stmt->execute();
      $stmt->close();
      $db->close();
      
	  writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroxml <> '.$query );
	  
	  }
      echo true;
  }
  
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  if ($arraydata['func']=='DeviceOnLineData')
  {
    $datetime = $arraydata["datetime"];
    $InPointGeoX = $arraydata["InPointGeoX"];
    $InPointGeoY = $arraydata["InPointGeoY"];
    $azimuth = $arraydata["azimuth"];
    $speed = $arraydata["speed"];
    $altitude = $arraydata["altitude"];
    //$id_trip = $arraydata["id_trip"];
    //$id_point = $arraydata["id_point"];
    //$login = $arraydata["id_user"];
    $login = $arraydata["login"];
    $password = $arraydata["pwd"];
    
    $xml = '<Data Login="'.$login.'" Password="'.$password.'">
      <p type="DeviceOnLineData"  >
        <p type="GPSData" Latitude="'.$InPointGeoX.'" Longitude="'.$InPointGeoY.'" Sattelites="" Gsm="" Charge="" Azimuth="'.$azimuth.'"
         Speed="'.$speed.'"  Altitude="'.$altitude.'"  IsValid="" Time="'.$datetime.'"/>

        <p type="SensorData"  SensorType=""  SensorValue="" Time=""/>
      </p>
</Data>';
    
    $query = "INSERT INTO `tms_input_web_data` (`date`,`login`,`packet`,`password`) VALUES ('".date('Y-m-d H:i:s')."','".$login."','".$xml."','".$password."')";
      if ($stmt = $db->prepare($query)){
      $stmt->execute();
      $stmt->close();
      $db->close();
      
    writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroxml <> '.$query );
    
    }
      echo true;
  }
  
  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  
  
  
  if ($arraydata['func']=='StateCheckpoint')
  {
    $id_trip = $arraydata["id_trip"];
    $id_point = $arraydata["id_point"];
    $state_point = $arraydata["state_point"];
    $id_user = $arraydata["id_user"];
    $login = $arraydata["login"];
    $password = $arraydata["pwd"];
    $strIdProductState = $arraydata["strIdProductState"];
    $cancelDescription = $arraydata["cancelDescription"];
    
    $xml = '<?xml version="1.0" encoding="utf-8"?>
              <Data Login="'.$login.'" Password="'.$password.'">
                <p type="TripChanged" TripID="'.$id_trip.'" UserID="'.$id_user.'" UserName="'.$login.'" UserType="" ChangedState="" DeletedPoints=" , ">
                  <p type="TripPointChanged" TripPointID="'.$id_point.'" ChangedState="'.$state_point.'" DeletedCargos="," DeletedTasks="," >
                  <p type="Cargo" ID="" Name="" Comment="" DimUnit="" State="" StateComments="" Value="" />
                  <p type="Task" ID="" State="" Comment=""  />';
    
    $arrayStrIdProductState = explode(',',$strIdProductState);
    
    foreach ($arrayStrIdProductState as $key=>$value)
    {
      $arrayIdProductState = explode('-',$value);
      $xml = $xml.'
        <p type="CargoChanged" ChangedState="'.$arrayIdProductState[1].'" CargoID="'.$arrayIdProductState[0].'" Value="" Comment="'.$cancelDescription.'"  />
      ';
    }
    $xml = $xml.'
      <p type="TaskChanged" TaskID=""  ChangedState=""  />
    </p>
    
    <p type="TripPointBase" RouteY="," RouteX="," TripPointBaseID="" TimeToLoadUnload=""                                       
             StartWork="" PointType="" PointState="" OrderInTrip="" Name="" EstTimeOut="" 
          EstTime="" EndWork="" Comment="" Code="" ClientName="" Address="" GeoX="5" GeoY="10" >
          <p type="Cargo" ID="" Name="" Comment="" DimUnit="" State=""
             StateComments="" Value="" />
          <p type="Task" ID="" State="" Comment="" />
       </p>
    </p>  
  </Data>';
  
    
    $query = "INSERT INTO `tms_input_web_data` (`date`,`login`,`packet`,`password`) VALUES ('".date('Y-m-d H:i:s')."','".$login."','".$xml."','".$password."')";
      if ($stmt = $db->prepare($query))
      {
        $stmt->execute();
        $stmt->close();
        $db->close();
        
        writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroxml <> '.$query );
      }
      echo true;
  }
  
  
  
  
////----------------------------- new functional tasks & odometr ------------///////////////////////////  
	
	
	if ($arraydata['func']=='ChangeStatedMobTask')
	{
		$login = $arraydata["login"];
		$password = $arraydata["pwd"];
		$TaskID = $arraydata["TaskID"];
		$DeviceID = $arraydata["DeviceID"];
		$State = $arraydata["State"];
		
		$xml = '<Data Login="'.$login.'" Password="'.$password.'">
				<p type="ChangeStatedMobTask">
				<p type="ChangeStatedMobTask"  TaskID="'.$TaskID.'" DeviceID="'.$DeviceID.'" State ="'.$State.'" />
				</p>
				</Data>';
				
		$query = "INSERT INTO `tms_input_web_data` (`date`,`login`,`packet`,`password`) VALUES ('".date('Y-m-d H:i:s')."','".$login."','".$xml."','".$password."')";
		if ($stmt = $db->prepare($query))
		{
        $stmt->execute();
        $stmt->close();
        $db->close();
        
			writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroxml <> '.$query );
			echo true;
		}
		else
		{
			echo false;
		}
		
	}
	
	
	
	
	if ($arraydata['func']=='CreateMobTask')
	{
		$login = $arraydata["login"];
		$password = $arraydata["pwd"];
		$Address = $arraydata["Address"];
		$Contact = $arraydata["Contact"];
		$Name = $arraydata["Name"];
		$Descr = $arraydata["Descr"];
		$Date = $arraydata["Date"];
		$DeviceID = $arraydata["DeviceID"];
		$TripID = $arraydata["TripID"];
		
		$xml = '
		<Data Login="'.$login.'" Password="'.$password.'">
		<p type="CreateMobTask">
		<p type="CreateMobTask"  Address="'.$Address.'" Date="'.$Date.'" Contact="'.$Contact.'" Name="'.$Name.'" Descr ="'.$Descr.'" DeviceID="'.$DeviceID.'" TripID="'.$TripID.'" />
		</p>
		</Data>';
		
		$query = "INSERT INTO `tms_input_web_data` (`date`,`login`,`packet`,`password`) VALUES ('".date('Y-m-d H:i:s')."','".$login."','".$xml."','".$password."')";
		if ($stmt = $db->prepare($query))
		{
        $stmt->execute();
        $stmt->close();
        $db->close();
        
			writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroxml <> '.$query );
			echo true;
		}
		else
		{
			echo false;
		}
		
	}
	
	if ($arraydata['func']=='OdomStChanged')
	{
		$login = $arraydata["login"];
		$password = $arraydata["pwd"];
		$Value = $arraydata["Value"];
		$Date = $arraydata["Date"];
		$IMEI = $arraydata["IMEI"];
		
		$xml = '
		<Data Login="'.$login.'" Password="'.$password.'">
		<p type="OdomStChanged">
		<p type="OdomStChanged"  Value="'.$Value.'" Date="'.$Date.'" IMEI="'.$IMEI.'" />
		</p>
		</Data>';
		
		$query = "INSERT INTO `tms_input_web_data` (`date`,`login`,`packet`,`password`) VALUES ('".date('Y-m-d H:i:s')."','".$login."','".$xml."','".$password."')";
		if ($stmt = $db->prepare($query))
		{
        $stmt->execute();
        $stmt->close();
        $db->close();
        
			writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroxml <> '.$query );
			echo true;
		}
		else
		{
			echo false;
		}
	
	}
	
	if ($arraydata['func']=='OdomEndChanged')
	{
		$login = $arraydata["login"];
		$password = $arraydata["pwd"];
		$Value = $arraydata["Value"];
		$Date = $arraydata["Date"];
		$IMEI = $arraydata["IMEI"];
		
		$xml = '
		<Data Login="'.$login.'" Password="'.$password.'">
		<p type="OdomEndChanged">
		<p type="OdomEndChanged"  Value="'.$Value.'" Date="'.$Date.'" IMEI="'.$IMEI.'" />
		</p>
		</Data>';
		
		$query = "INSERT INTO `tms_input_web_data` (`date`,`login`,`packet`,`password`) VALUES ('".date('Y-m-d H:i:s')."','".$login."','".$xml."','".$password."')";
		if ($stmt = $db->prepare($query))
		{
        $stmt->execute();
        $stmt->close();
        $db->close();
        
			writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroxml <> '.$query );
			echo true;
		}
		else
		{
			echo false;
		}
		
	}
	
	if ($arraydata['func']=='CancelMobTask')
	{
		$login = $arraydata["login"];
		$password = $arraydata["pwd"];
		$TaskID = $arraydata["TaskID"];
		$DeviceID = $arraydata["DeviceID"];
		$Comment = $arraydata["Comment"];
		
		$xml = '
		<Data Login="'.$login.'" Password="'.$password.'">
		<p type="CancelMobTask">
		<p type="CancelMobTask"  TaskID="'.$TaskID.'" DeviceID="'.$DeviceID.'" Comment="'.$Comment.'" />
		</p>
		</Data>';
		
		$query = "INSERT INTO `tms_input_web_data` (`date`,`login`,`packet`,`password`) VALUES ('".date('Y-m-d H:i:s')."','".$login."','".$xml."','".$password."')";
		if ($stmt = $db->prepare($query))
		{
        $stmt->execute();
        $stmt->close();
        $db->close();
        
			writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchroxml <> '.$query );
			echo true;
		}
		else
		{
			echo false;
		}
		
	}
	
	
	
	
?>