<?php
include("../../db.php");

$driverId =  $_POST["driver_id"];
$latitude =  $_POST["latitude"];
$longitude = $_POST["longitude"];
$accuracy =  $_POST["accuracy"];
$provider =  $_POST["provider"];
$hasSpeed =  $_POST["has_speed"];
$speed =     $_POST["speed"];
$error =    $_POST["error"];

$message = "Unknown Error";
$status = 0;

if ($driverId != '') {

    $rowData = $db->getRow("select * from gps_tracking where driver_id='$driverId'");

    $dataArray = array(
        "driver_id" => "$driverId",
        "latitude" => "$latitude",
        "longitude" => "$longitude",
        "accuracy" => "$accuracy",
        "provider" => "$provider",
        "has_speed" => "$hasSpeed",
        "speed" => "$speed",
        "error" => "$error"
    );

    if ($rowData && count($rowData) > 0){
        $result = $db->updateAry("gps_tracking", $dataArray, "where driver_id='$driverId'");
    } else {
        $result = $db->insertAry("gps_tracking", $dataArray);
    }

    if ($result) {
        $status = 1;
        $message = "Success";
    } else {
        $message = "Unable to perform operation! Please check and try again";
    }

} else {
    $message = "User id required";
}

echo json_encode(array("status" => $status, "message" => $message));