<?php
 
session_start();

require_once ("connection_sql.php");

 
date_default_timezone_set('Asia/Colombo');  
$created_at = date('Y-m-d H:i:s');  
 

 
    
    $latitude = $_POST['lat'];
    $longitude = $_POST['lon'];

    // Get company from session (assuming it is the username)
    $user = $_SESSION['CURRENT_USER'];
    $ip = $_SERVER['REMOTE_ADDR'];
    
    // Prepare SQL query to insert data
    $sql = "INSERT INTO s_locations (latitude, longitude, user_name,ip, created_at) VALUES (:latitude, :longitude, :user_name, :ip, :created_at)";
    
    
    // Prepare the statement
    $stmt = $conn->prepare($sql);
    
    // Bind parameters correctly
    $stmt->bindParam(':latitude', $latitude);
    $stmt->bindParam(':longitude', $longitude);
    $stmt->bindParam(':user_name', $user);
    $stmt->bindParam(':ip', $ip);
    $stmt->bindParam(':created_at', $created_at);
    
    // Execute the statement
    if ($stmt->execute()) { 
        $response = array(
            "status" => "success",
            "message" => "Location saved successfully."
        );
    } else { 
        $response = array(
            "status" => "error",
            "message" => "Error saving location."
        );
    }

    // Return JSON response
    echo json_encode($response);

 
?>
