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

    $query = $db->query("SELECT * FROM acente_a_content", PDO::FETCH_ASSOC);
    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $acente_a_content[] = array(
            	"aacId" => $q["aacId"],
            	"aacName" => $q["aacName"],
            	"aacValue" => $q["aacValue"],
            	"aacVTE" => $q["aacVTE"],
            	"aacVEE" => $q["aacVEE"],
            	"aacAcenteId" => $q["aacAcenteId"],
            	"aacPersonelId" => $q["aacPersonelId"],
            	"aacCreatedAt" => $q["aacCreatedAt"],
                "aacUpdatedAt" => $q["aacUpdatedAt"],
                "aacSyncAt" => $q["aacSyncAt"]
            );
        }
        $result = json_encode($acente_a_content, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $acente_a_content[] = array(
            	"aacId" => $q["aacId"],
            	"aacName" => $q["aacName"],
            	"aacValue" => $q["aacValue"],
            	"aacVTE" => $q["aacVTE"],
            	"aacVEE" => $q["aacVEE"],
            	"aacAcenteId" => $q["aacAcenteId"],
            	"aacPersonelId" => $q["aacPersonelId"],
            	"aacCreatedAt" => $q["aacCreatedAt"],
                "aacUpdatedAt" => $q["aacUpdatedAt"],
                "aacSyncAt" => $q["aacSyncAt"]
            );
        }
        $result = json_encode($acente_a_content, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $acente_a_content[] = array(
                "aacId" => $q["aacId"],
                "aacName" => $q["aacName"],
                "aacValue" => $q["aacValue"],
                "aacVTE" => $q["aacVTE"],
                "aacVEE" => $q["aacVEE"],
                "aacAcenteId" => $q["aacAcenteId"],
                "aacPersonelId" => $q["aacPersonelId"],
                "aacCreatedAt" => $q["aacCreatedAt"],
                "aacUpdatedAt" => $q["aacUpdatedAt"],
                "aacSyncAt" => $q["aacSyncAt"]
            );
        }
        $result = json_encode($acente_a_content, JSON_UNESCAPED_UNICODE);
        echo $result;

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

    $query = $db->query("SELECT * FROM acente_a_content WHERE aacAcenteId='".$_GET['acente_id']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $acente_a_content[] = array(
                "aacId" => $q["aacId"],
                "aacName" => $q["aacName"],
                "aacValue" => $q["aacValue"],
                "aacVTE" => $q["aacVTE"],
                "aacVEE" => $q["aacVEE"],
                "aacAcenteId" => $q["aacAcenteId"],
                "aacPersonelId" => $q["aacPersonelId"],
                "aacCreatedAt" => $q["aacCreatedAt"],
                "aacUpdatedAt" => $q["aacUpdatedAt"],
                "aacSyncAt" => $q["aacSyncAt"]
            );
        }
        $result = json_encode($acente_a_content, 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 acente_a_content WHERE aacSyncAt >= ".$_GET['sync']." AND aacPersonelId !='".$_GET['sync_personel']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $acente_a_content[] = array(
                "aacId" => $q["aacId"],
                "aacName" => $q["aacName"],
                "aacValue" => $q["aacValue"],
                "aacVTE" => $q["aacVTE"],
                "aacVEE" => $q["aacVEE"],
                "aacAcenteId" => $q["aacAcenteId"],
                "aacPersonelId" => $q["aacPersonelId"],
                "aacCreatedAt" => $q["aacCreatedAt"],
                "aacUpdatedAt" => $q["aacUpdatedAt"],
                "aacSyncAt" => $q["aacSyncAt"]
            );
        }
        $result = json_encode($acente_a_content, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "POST") {
    $currentDate =  time();
    $query = $db->prepare("INSERT INTO acente_a_content SET
		aacId=:aacId,
        aacName=:aacName,
		aacValue=:aacValue,
		aacVTE=:aacVTE,
		aacVEE=:aacVEE,
		aacAcenteId=:aacAcenteId,
		aacPersonelId=:aacPersonelId,
		aacCreatedAt=:aacCreatedAt,
        aacUpdatedAt=:aacUpdatedAt,
        aacSyncAt=:aacSyncAt
	");
    $insert=$query->execute(array(
        'aacId'=>$data['aacId'],
		'aacName'=>$data['aacName'],
		'aacValue'=>$data['aacValue'],
		'aacVTE'=>$data['aacVTE'],
		'aacVEE'=>$data['aacVEE'],
		'aacAcenteId'=>$data['aacAcenteId'],
		'aacPersonelId'=>$data['aacPersonelId'],
		'aacCreatedAt'=>$data['aacCreatedAt'],
        'aacUpdatedAt'=>$data['aacUpdatedAt'],
        'aacSyncAt'=>$currentDate."000"
    ));
    if ($insert) {
        echo json_encode("ok");

    } else {
        echo json_encode("error");
    }
} else if ($method == "PUT") {
    $currentDate =  time();
    $query = $db->prepare("UPDATE acente_a_content SET
		aacName=:aacName,
		aacValue=:aacValue,
		aacVTE=:aacVTE,
		aacVEE=:aacVEE,
		aacAcenteId=:aacAcenteId,
		aacPersonelId=:aacPersonelId,
		aacCreatedAt=:aacCreatedAt,
        aacUpdatedAt=:aacUpdatedAt,
        aacSyncAt=:aacSyncAt
		WHERE aacId=:aacId
	");
    $update=$query->execute(array(
		'aacName'=>$data['aacName'],
		'aacValue'=>$data['aacValue'],
		'aacVTE'=>$data['aacVTE'],
		'aacVEE'=>$data['aacVEE'],
		'aacAcenteId'=>$data['aacAcenteId'],
		'aacPersonelId'=>$data['aacPersonelId'],
		'aacCreatedAt'=>$data['aacCreatedAt'],
        'aacUpdatedAt'=>$data['aacUpdatedAt'],
        'aacSyncAt'=>$currentDate."000",
		'aacId'=>$data['aacId']
    ));
    if ($update) {
        echo json_encode("ok");

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

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


?>