<?php

include 'config.inc.php';


$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$whatTo = $request->whatToGet;

if ($whatTo == "comenzi") {

$sql = "
	SELECT 
com.ID,
usr.Nume,
usr.Telefon,
usr.Greseli,
caf.denumire,
com.Variatie,
com.LapteSoya,
com.ExtraShot,
com.NivelLapte,
com.NivelSpuma,
com.Latitude,
com.Longitude,
com.Estimation,
com.Total,
com.Status,
com.DataAdd,
com.OraAdd
FROM tblComenzi com  
LEFT JOIN tblCafea caf
ON caf.ID = com.IDCafea
LEFT JOIN tblUtilizatori usr
ON usr.ID = com.IDClient
ORDER BY com.ID DESC
	";



$result = mysqli_query($conn,$sql);

$response = array();



//array_push($response,array("Version"=>$ver));

while ($row = mysqli_fetch_array($result))
{

    $est = explode(" ", $row[12]);
    $dbAddTime = strtotime($row[16]);
    $total = (((int)$est) * 60) + $dbAddTime;

    $current_timestamp = strtotime("now");
    $danger = "";
    if (($current_timestamp  > $total ) && (int)$row[14] == 0){
        $danger = "text-danger";
    }


    ///$current_timestamp_by_mktime = mktime(date("H"),date("i"),date("s"),date("m"),date("d"),date("Y"));

    array_push($response,
        array(
            "ID"=>$row[0],
            "Nume"=>$row[1],
            "Telefon"=>$row[2],
            "Greseli"=>$row[3],
            "denumire"=>$row[4],
            "Variatie"=>$row[5],
            "LapteSoya"=>$row[6],
            "ExtraShot"=>$row[7],
            "NivelLapte"=>$row[8],
            "NivelSpuma"=>$row[9],
            "Latitude"=>$row[10],
            "Longitude"=>$row[11],
            "Estimation"=>$row[12],
            "Total"=>$row[13],
            "Status"=>$row[14],
            //"DataAdd"=>$row[15],
            "DataAdd"=>$row[15],
            "OraAdd"=>$row[16],
            "Danger"=>$danger
        ));
}


} // end if comenzi

if ($whatTo == "users") {

    $sql = "
	SELECT 
*
FROM tblUtilizatori usr
ORDER BY usr.ID ASC
	";

    $result = mysqli_query($conn,$sql);

    $response = array();

    while ($row = mysqli_fetch_array($result))
    {
        array_push($response,
            array(
                "ID"=>$row[0],
                "Nume"=>$row[1],
                "Telefon"=>$row[2],
                "Greseli"=>$row[3],
                "Ban"=>$row[4]
            ));
    }

} // end if users



if ($whatTo == "coffees") {

    $sql = "SELECT c.id,
c.denumire,
c.origine,
v.IDcafea,
GROUP_CONCAT(v.Variatie) AS Variatie,
GROUP_CONCAT(v.Pret) AS Pret
FROM `tblCafea` c
LEFT JOIN `tblVariatii` v
ON c.`id` = v.`IDCafea`
GROUP BY v.IDCafea
ORDER BY c.id
";

    $result = mysqli_query($conn,$sql);

    $response = array();

    while ($row = mysqli_fetch_array($result))
    {
        array_push($response,
            array(
                "ID"=>$row[0],
                "Denumire"=>$row[1],
                "Origine"=>$row[2],
                "Variatie"=>$row[4],
                "Pret"=>$row[5]
            ));
    }

} // end if users






/*    echo "<pre>";
	echo json_encode(array("response"=>$response), JSON_PRETTY_PRINT);
    echo "</pre>";*/

echo json_encode(array("response"=>$response));

//echo json_encode($response);


mysqli_close($conn);