<?php

$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';

if ($contentType === "application/json") {
    //Receive the RAW post data.
    $content = trim(file_get_contents("php://input"));

    $decodedData = json_decode($content, true);

    //If json_decode failed, the JSON is invalid.
    if (!is_array($decodedData)) {
        echo json_encode($decodedData);
    } else {
        $obj= new getUserFTADetails();
        $obj->getEmployeeFTAInfo($decodedData);
        //$obj->getEmployeeFTAInfo('200454', '2020-12-02');
    }
}


class getUserFTADetails
{

    function __construct()
    {
        require_once dirname(__DIR__) . "../../php/DB_Connect.php";
        $dbClassObj = new DB_conncect();
        $this->db = $dbClassObj->connectToDB();
        //$this->getEmployeeFTAInfo();
    }

    function getEmployeeFTAInfo($decodedData)
    {
        $empId=$decodedData["empId"];
        $siteVisitDate = $decodedData["siteVisitDate"];
        $detailsType =  $decodedData["detailsType"] ;


        if($detailsType ==="fullInfo"){
            $siteVisitOrder= $decodedData["siteVisitOrder"] ;
            $query = $this->db->prepare("SELECT `EMP_ID`, `NAME`, `SITE_VISIT_DATE`, `SITE_ID`, `SITE_VISITED_ORDER`,`SITE_ID_LATITUDE`, `SITE_ID_LONGITUDE`,  `DISTANCE_(KMS)`, `USER_LAT`, `USER_LNG`, `USER_LOCATION_ADDRESS` FROM `tesf` WHERE  `EMP_ID`=? AND  `SITE_VISIT_DATE`=? AND `SITE_VISITED_ORDER`=?");
            $query->bind_param('ssi', $empId, $siteVisitDate, $siteVisitOrder);

        }else{
            $query = $this->db->prepare("SELECT `EMP_ID`, `NAME`, `SITE_VISIT_DATE`, `SITE_ID`, `SITE_VISITED_ORDER`,`SITE_ID_LATITUDE`, `SITE_ID_LONGITUDE`,  `DISTANCE_(KMS)`, `USER_LAT`, `USER_LNG`, `USER_LOCATION_ADDRESS` FROM `tesf` WHERE  `EMP_ID`=? AND  `SITE_VISIT_DATE`=?");
            $query->bind_param('ss', $empId, $siteVisitDate);
        }
       // echo ($empId."=".$siteVisitDate);
       // $query = $this->db->prepare("SELECT `NAME`, 'SITE_ID', `SITE_VISITED_ORDER` FROM `tesf` WHERE  `EMP_ID`='200454' AND  `SITE_VISIT_DATE`='2020-12-02' ");
        //$query->bind_param('ss', "200454", "2020-12-02");
        
        //$query = $this->db->prepare("SELECT `EMP_ID`, `NAME`, `SITE_VISIT_DATE`, `SITE_ID`, `SITE_VISITED_ORDER`,`SITE_ID_LATITUDE`, `SITE_ID_LONGITUDE`,  `DISTANCE_(KMS)`, `USER_LAT`, `USER_LNG`, `USER_LOCATION_ADDRESS` FROM `tesf` WHERE  `EMP_ID`=? AND  `SITE_VISIT_DATE`=?");
        //$query->bind_param('ss', $empId, $siteVisitDate);
        $query->execute();
        $query->bind_result($empId, $name, $siteVisitedDate, $siteId, $siteVisitOrder, $siteLat, $siteLng, $distance, $userLat, $userLng, $userLctn);
        $query->store_result();
        $arr_FTA_Info = array();
        if ($query->num_rows() > 0) {
            while ($query->fetch()) {
                $arr_FTA_Info[] =
                    array(
                        "empId" => $empId,
                        "name" => $name,
                        "siteId" => $siteId,
                        "siteVisitOrder" => $siteVisitOrder,
                        "siteVisitedDate" => $siteVisitedDate,
                        "siteLat" => $siteLat,
                        "siteLng" => $siteLng,
                        "distance" => $distance,
                        "userLat" => $userLat,
                        "userLng" => $userLng,
                        "userLctn" => $userLctn
                    );
            }
            echo json_encode($arr_FTA_Info);
        
        }else{
       echo json_encode("results not found");
        }
    }
}
