<?php
    include_once 'opendb.php';
     $response["status"] = -1;
     $response["message"] = "Error";
     $CompID=0;
     if (isset($_POST["Comp_Id"]))
    {
         $CompID=$_POST["Comp_Id"]; 
    }
    if($CompID==0) {
         $response["message"] = "Error: Web Co ID invalid";
            // echo no users JSON
            die( json_encode($response));
    }
    if (isset($_POST["CustCode"]) && isset($_POST["Latitude"]) && isset($_POST["Longitude"]))
    {
	
        $CustCode=$_POST["CustCode"] ;
        $Latitude=$_POST["Latitude"] ;
        $Longitude=$_POST["Longitude"] ;
        $client_Code=$_POST["Client_Code"];
        
        $edittime= date('ymdHi');
        $sql = "select * from subsidiary WHERE latitude > 0 and longitude > 0 and  sub_code like '$CustCode' and comp_id=" .$CompID;        
        $sresult = $conn->query($sql);
        if ($sresult->num_rows > 0 )
        {
            $response["message"] = "Error: Location already updated, contact office to change your location";
        } else {
        
            $sql = "update subsidiary set latitude=$Latitude,longitude=$Longitude,loc_upd_by='$client_Code',loc_edit_time='$edittime' WHERE sub_code like '$CustCode' and comp_id=" .$CompID;
            //file_put_contents("log.txt", $sql);
            $conn->query($sql);
            
            if (!$conn -> query($sql)) {
                $conn ->close();
                die("Error : " . $mysqli -> error.$sql);
            } else {
                   $response["status"] = 1;
                   $response["message"] = "Update Successful";
            }
        }
        mysqli_free_result($sresult);

    }
    else
    {
        $response["message"] = "Error: No Parameters";
    }
    
    $conn->close();
    echo json_encode($response);
?>