<?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 analysis_data", PDO::FETCH_ASSOC);
    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $analysis_data[] = array(
            	"adId" => $q["adId"],
            	"adTitle" => $q["adTitle"],
            	"adPersonelId" => $q["adPersonelId"],
            	"adCreatedAt" => $q["adCreatedAt"],
            	"adUpdatedAt" => $q["adUpdatedAt"],
                "adSyncAt" => $q["adSyncAt"]
            );
        }
        $result = json_encode($analysis_data, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $analysis_data[] = array(
            	"adId" => $q["adId"],
            	"adTitle" => $q["adTitle"],
            	"adPersonelId" => $q["adPersonelId"],
            	"adCreatedAt" => $q["adCreatedAt"],
            	"adUpdatedAt" => $q["adUpdatedAt"],
                "adSyncAt" => $q["adSyncAt"]
            );
        }
        $result = json_encode($analysis_data, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $analysis_data[] = array(
                "adId" => $q["adId"],
                "adTitle" => $q["adTitle"],
                "adPersonelId" => $q["adPersonelId"],
                "adCreatedAt" => $q["adCreatedAt"],
                "adUpdatedAt" => $q["adUpdatedAt"],
                "adSyncAt" => $q["adSyncAt"]
            );
        }
        $result = json_encode($analysis_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 analysis_data WHERE adSyncAt >= ".$_GET['sync']." AND adPersonelId !='".$_GET['sync_personel']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $analysis_data[] = array(
                "adId" => $q["adId"],
                "adTitle" => $q["adTitle"],
                "adPersonelId" => $q["adPersonelId"],
                "adCreatedAt" => $q["adCreatedAt"],
                "adUpdatedAt" => $q["adUpdatedAt"],
                "adSyncAt" => $q["adSyncAt"]
            );
        }
        $result = json_encode($analysis_data, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "POST") {
    $currentDate =  time();
    $query = $db->prepare("INSERT INTO analysis_data SET
		adId=:adId,
		adTitle=:adTitle,
		adPersonelId=:adPersonelId,
		adCreatedAt=:adCreatedAt,
		adUpdatedAt=:adUpdatedAt,
        adSyncAt=:adSyncAt
	");
    $insert=$query->execute(array(
		'adId'=>$data['adId'],
		'adTitle'=>$data['adTitle'],
		'adPersonelId'=>$data['adPersonelId'],
		'adCreatedAt'=>$data['adCreatedAt'],
		'adUpdatedAt'=>$data['adUpdatedAt'],
        'adSyncAt'=>$currentDate."000"
    ));
    if ($insert) {
        echo json_encode("ok");

    } else {
        echo json_encode("error");
    }
} else if ($method == "PUT") {
    $currentDate =  time();
    $query = $db->prepare("UPDATE analysis_data SET
		adTitle=:adTitle,
		adPersonelId=:adPersonelId,
		adCreatedAt=:adCreatedAt,
		adUpdatedAt=:adUpdatedAt
		WHERE adId=:adId
	");
    $update=$query->execute(array(
		'adTitle'=>$data['adTitle'],
		'adPersonelId'=>$data['adPersonelId'],
		'adCreatedAt'=>$data['adCreatedAt'],
		'adUpdatedAt'=>$data['adUpdatedAt'],
        'adSyncAt'=>$currentDate."000",
		'adId'=>$data['adId']
    ));
    if ($update) {
        echo json_encode("ok");

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

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


?>