<?php

// Created by Ahmadjonov Ibrohimjon Ilhomjon o'g'li on 29.09.2021
require_once('connect.php');
  $list_region = array();
  $response =array();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    
    $_POST = json_decode(file_get_contents("php://input"),true);
    $agent_id=$_POST['agent_id'];
    $date1=$_POST['date1'];
    $date2=$_POST['date2'];

    mysqli_set_charset($conn,"utf8");
    $query = mysqli_query($conn,"SET NAMES utf8");
    $sqlStatement = "SELECT *, STR_TO_DATE(vaqti, '%d.%m.%Y %H:%i:%s') AS vaqti2 FROM agent_locations WHERE agent_id = ".$agent_id." AND STR_TO_DATE(vaqti, '%d.%m.%Y %H:%i:%s') BETWEEN '".$date1."' AND '".$date2."' ORDER BY STR_TO_DATE(vaqti, '%d.%m.%Y %H:%i:%s') ASC";
    $query = mysqli_query($conn, $sqlStatement);


    if ($query !== false && $query->num_rows > 0) {

        while($row = mysqli_fetch_assoc($query)){
            
            $agent_id=$row['Id'];
            $latitude=$row['latitude'];
            $longitude=$row['longitude'];
            $battery=$row['battery'];
            $zakaz_raqam=$row['zakaz_raqam'];
            $fio=$row['fio'];
            $telefon=$row['telefon'];
             $farqi=$row['farqi'];
            $time_update=$row['vaqti2'];
            $izox=$row['izox'];

            if($latitude != "0.0" && $longitude != "0.0"){
            array_push($list_region,
            array(
                    'Id' => $agent_id,
                    'lat' => $latitude,
                    'lng' => $longitude,
                    'battery' => $battery,
                    'zakaz_raqam' => $zakaz_raqam,
                    'telefon' => $telefon,
                    'time_update' => $time_update,
                    'fio' => $fio,
                    'farqi'=> $farqi,
                    'izox'=> $izox
                 )
                );
            }
        }

    }

}

echo json_encode($list_region);

$conn->close();
?>