<?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['report_id']) AND !isset($_GET['sync'])) {

    $query = $db->query("SELECT * FROM samplingsheet", PDO::FETCH_ASSOC);
    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $samplingsheet[] = array(
            	"smId" => $q["smId"],
            	"smTitle" => $q["smTitle"],
            	"smImg" => $q["smImg"],
                "smReportId" => $q["smReportId"],
            	"smPersonelId" => $q["smPersonelId"],
            	"smCreatedAt" => $q["smCreatedAt"],
            	"smUpdatedAt" => $q["smUpdatedAt"],
                "smSyncAt" => $q["smSyncAt"]
            );
        }
        $result = json_encode($samplingsheet, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $samplingsheet[] = array(
            	"smId" => $q["smId"],
                "smTitle" => $q["smTitle"],
                "smImg" => $q["smImg"],
                "smReportId" => $q["smReportId"],
                "smPersonelId" => $q["smPersonelId"],
                "smCreatedAt" => $q["smCreatedAt"],
                "smUpdatedAt" => $q["smUpdatedAt"],
                "smSyncAt" => $q["smSyncAt"]
            );
        }
        $result = json_encode($samplingsheet, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $samplingsheet[] = array(
                "smId" => $q["smId"],
                "smTitle" => $q["smTitle"],
                "smImg" => $q["smImg"],
                "smReportId" => $q["smReportId"],
                "smPersonelId" => $q["smPersonelId"],
                "smCreatedAt" => $q["smCreatedAt"],
                "smUpdatedAt" => $q["smUpdatedAt"],
                "smSyncAt" => $q["smSyncAt"]
            );
        }
        $result = json_encode($samplingsheet, JSON_UNESCAPED_UNICODE);
        echo $result;

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

    $query = $db->query("SELECT * FROM samplingsheet WHERE smReportId='".$_GET['report_id']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $samplingsheet[] = array(
                "smId" => $q["smId"],
                "smTitle" => $q["smTitle"],
                "smImg" => $q["smImg"],
                "smReportId" => $q["smReportId"],
                "smPersonelId" => $q["smPersonelId"],
                "smCreatedAt" => $q["smCreatedAt"],
                "smUpdatedAt" => $q["smUpdatedAt"],
                "smSyncAt" => $q["smSyncAt"]
            );
        }
        $result = json_encode($samplingsheet, 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 samplingsheet WHERE smSyncAt >= ".$_GET['sync']." AND smPersonelId !='".$_GET['sync_personel']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $samplingsheet[] = array(
                "smId" => $q["smId"],
                "smTitle" => $q["smTitle"],
                "smImg" => $q["smImg"],
                "smReportId" => $q["smReportId"],
                "smPersonelId" => $q["smPersonelId"],
                "smCreatedAt" => $q["smCreatedAt"],
                "smUpdatedAt" => $q["smUpdatedAt"],
                "smSyncAt" => $q["smSyncAt"]
            );
        }
        $result = json_encode($samplingsheet, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "POST") {
    $currentDate =  time();
    $query = $db->prepare("INSERT INTO samplingsheet SET
		smId=:smId,
		smTitle=:smTitle,
		smImg=:smImg,
		smReportId=:smReportId,
        smPersonelId=:smPersonelId,
		smCreatedAt=:smCreatedAt,
		smUpdatedAt=:smUpdatedAt,
        smSyncAt=:smSyncAt
	");
    $insert=$query->execute(array(
		'smId'=>$data['smId'],
		'smTitle'=>$data['smTitle'],
		'smImg'=>$data['smImg'],
		'smReportId'=>$data['smReportId'],
        'smPersonelId'=>$data['smPersonelId'],
		'smCreatedAt'=>$data['smCreatedAt'],
		'smUpdatedAt'=>$data['smUpdatedAt'],
        'smSyncAt'=>$currentDate."000"
    ));
    if ($insert) {
        echo json_encode("ok");

    } else {
        echo json_encode("error");
    }
} else if ($method == "PUT") {
    $currentDate =  time();
    $query = $db->prepare("UPDATE samplingsheet SET
		smTitle=:smTitle,
		smImg=:smImg,
		smReportId=:smReportId,
        smPersonelId=:smPersonelId,
		smCreatedAt=:smCreatedAt,
		smUpdatedAt=:smUpdatedAt,
        smSyncAt=:smSyncAt
		WHERE smId=:smId
	");
    $update=$query->execute(array(
		'smTitle'=>$data['smTitle'],
		'smImg'=>$data['smImg'],
		'smReportId'=>$data['smReportId'],
        'smPersonelId'=>$data['smPersonelId'],
		'smCreatedAt'=>$data['smCreatedAt'],
		'smUpdatedAt'=>$data['smUpdatedAt'],
        'smSyncAt'=>$currentDate."000",
		'smId'=>$data['smId']
    ));
    if ($update) {
        echo json_encode("ok");

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

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


?>