<?php
require_once("conf.php");
require_once("method.php");

$method = $_SERVER["REQUEST_METHOD"];
method($method);

$data = json_decode(file_get_contents('php://input'), true);

if ($method == "GET" AND !isset($_GET['id']) AND !isset($_GET['personel_id']) AND !isset($_GET['sync'])) {

    $query = $db->query("SELECT * FROM sampling_data", PDO::FETCH_ASSOC);
    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $sampling_data[] = array(
            	"sdId" => $q["sdId"],
            	"sdTitle" => $q["sdTitle"],
            	"sdPersonelId" => $q["sdPersonelId"],
            	"sdCreatedAt" => $q["sdCreatedAt"],
            	"sdUpdatedAt" => $q["sdUpdatedAt"],
                "sdSyncAt" => $q["sdSyncAt"]
            );
        }
        $result = json_encode($sampling_data, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "GET" AND isset($_GET['id'])) {

    $query = $db->query("SELECT * FROM sampling_data WHERE sdId='".$_GET['id']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $sampling_data[] = array(
            	"sdId" => $q["sdId"],
            	"sdTitle" => $q["sdTitle"],
            	"sdPersonelId" => $q["sdPersonelId"],
            	"sdCreatedAt" => $q["sdCreatedAt"],
            	"sdUpdatedAt" => $q["sdUpdatedAt"],
                "sdSyncAt" => $q["sdSyncAt"]
            );
        }
        $result = json_encode($sampling_data, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "GET" AND isset($_GET['personel_id'])) {

    $query = $db->query("SELECT * FROM sampling_data WHERE sdPersonelId='".$_GET['personel_id']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $sampling_data[] = array(
                "sdId" => $q["sdId"],
                "sdTitle" => $q["sdTitle"],
                "sdPersonelId" => $q["sdPersonelId"],
                "sdCreatedAt" => $q["sdCreatedAt"],
                "sdUpdatedAt" => $q["sdUpdatedAt"],
                "sdSyncAt" => $q["sdSyncAt"]
            );
        }
        $result = json_encode($sampling_data, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "GET" AND isset($_GET['sync']) AND isset($_GET['sync_personel'])) {

    $query = $db->query("SELECT * FROM sampling_data WHERE sdSyncAt >= ".$_GET['sync']." AND sdPersonelId !='".$_GET['sync_personel']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $sampling_data[] = array(
                "sdId" => $q["sdId"],
                "sdTitle" => $q["sdTitle"],
                "sdPersonelId" => $q["sdPersonelId"],
                "sdCreatedAt" => $q["sdCreatedAt"],
                "sdUpdatedAt" => $q["sdUpdatedAt"],
                "sdSyncAt" => $q["sdSyncAt"]
            );
        }
        $result = json_encode($sampling_data, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "POST") {
    $currentDate =  time();
    $query = $db->prepare("INSERT INTO sampling_data SET
		sdId=:sdId,
		sdTitle=:sdTitle,
		sdPersonelId=:sdPersonelId,
		sdCreatedAt=:sdCreatedAt,
		sdUpdatedAt=:sdUpdatedAt,
        sdSyncAt=:sdSyncAt
	");
    $insert=$query->execute(array(
		'sdId'=>$data['sdId'],
		'sdTitle'=>$data['sdTitle'],
		'sdPersonelId'=>$data['sdPersonelId'],
		'sdCreatedAt'=>$data['sdCreatedAt'],
		'sdUpdatedAt'=>$data['sdUpdatedAt'],
        'sdSyncAt'=>$currentDate."000"
    ));
    if ($insert) {
        echo json_encode("ok");

    } else {
        echo json_encode("error");
    }
} else if ($method == "PUT") {
    $currentDate =  time();
    $query = $db->prepare("UPDATE sampling_data SET
		sdTitle=:sdTitle,
		sdPersonelId=:sdPersonelId,
		sdCreatedAt=:sdCreatedAt,
		sdUpdatedAt=:sdUpdatedAt,
        sdSyncAt=:sdSyncAt
		WHERE sdId=:sdId
	");
    $update=$query->execute(array(
		'sdTitle'=>$data['sdTitle'],
		'sdPersonelId'=>$data['sdPersonelId'],
		'sdCreatedAt'=>$data['sdCreatedAt'],
		'sdUpdatedAt'=>$data['sdUpdatedAt'],
        'sdSyncAt'=>$currentDate."000",
		'sdId'=>$data['sdId']
    ));
    if ($update) {
        echo json_encode("ok");

    } else {
        echo json_encode("error");
    }
} else if ($method == "DELETE") {
    $query = $db->prepare("DELETE FROM sampling_data WHERE sdId=:sdId");
    $update=$query->execute(array(
		'sdId'=>$_GET['id']
    ));
    if ($update) {
        echo json_encode("ok");

    } else {
        echo json_encode("error");
    }
}


?>