<?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_s_content", PDO::FETCH_ASSOC);
    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $acente_s_content[] = array(
            	"ascId" => $q["ascId"],
            	"ascName" => $q["ascName"],
            	"ascAcenteId" => $q["ascAcenteId"],
            	"ascPersonelId" => $q["ascPersonelId"],
            	"ascCreatedAt" => $q["ascCreatedAt"],
                "ascUpdatedAt" => $q["ascUpdatedAt"],
                "ascSyncAt" => $q["ascSyncAt"]
            );
        }
        $result = json_encode($acente_s_content, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $acente_s_content[] = array(
            	"ascId" => $q["ascId"],
            	"ascName" => $q["ascName"],
            	"ascAcenteId" => $q["ascAcenteId"],
            	"ascPersonelId" => $q["ascPersonelId"],
            	"ascCreatedAt" => $q["ascCreatedAt"],
                "ascUpdatedAt" => $q["ascUpdatedAt"],
                "ascSyncAt" => $q["ascSyncAt"]
            );
        }
        $result = json_encode($acente_s_content, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $acente_s_content[] = array(
                "ascId" => $q["ascId"],
                "ascName" => $q["ascName"],
                "ascAcenteId" => $q["ascAcenteId"],
                "ascPersonelId" => $q["ascPersonelId"],
                "ascCreatedAt" => $q["ascCreatedAt"],
                "ascUpdatedAt" => $q["ascUpdatedAt"],
                "ascSyncAt" => $q["ascSyncAt"]
            );
        }
        $result = json_encode($acente_s_content, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $acente_s_content[] = array(
                "ascId" => $q["ascId"],
                "ascName" => $q["ascName"],
                "ascAcenteId" => $q["ascAcenteId"],
                "ascPersonelId" => $q["ascPersonelId"],
                "ascCreatedAt" => $q["ascCreatedAt"],
                "ascUpdatedAt" => $q["ascUpdatedAt"],
                "ascSyncAt" => $q["ascSyncAt"]
            );
        }
        $result = json_encode($acente_s_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_s_content WHERE ascSyncAt >= ".$_GET['sync']."  AND ascPersonelId !='".$_GET['sync_personel']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $acente_s_content[] = array(
                "ascId" => $q["ascId"],
                "ascName" => $q["ascName"],
                "ascAcenteId" => $q["ascAcenteId"],
                "ascPersonelId" => $q["ascPersonelId"],
                "ascCreatedAt" => $q["ascCreatedAt"],
                "ascUpdatedAt" => $q["ascUpdatedAt"],
                "ascSyncAt" => $q["ascSyncAt"]
            );
        }
        $result = json_encode($acente_s_content, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "POST") {
    $currentDate =  time();
    $query = $db->prepare("INSERT INTO acente_s_content SET
		ascId=:ascId,
        ascName=:ascName,
		ascAcenteId=:ascAcenteId,
		ascPersonelId=:ascPersonelId,
		ascCreatedAt=:ascCreatedAt,
        ascUpdatedAt=:ascUpdatedAt,
        ascSyncAt=:ascSyncAt
	");
    $insert=$query->execute(array(
        'ascId'=>$data['ascId'],
		'ascName'=>$data['ascName'],
		'ascAcenteId'=>$data['ascAcenteId'],
		'ascPersonelId'=>$data['ascPersonelId'],
		'ascCreatedAt'=>$data['ascCreatedAt'],
        'ascUpdatedAt'=>$data['ascUpdatedAt'],
        'ascSyncAt'=>$currentDate."000"

    ));
    if ($insert) {
       echo json_encode("ok");

    } else {
        echo json_encode("error");
    }
} else if ($method == "PUT") {
    $currentDate =  time();
    $query = $db->prepare("UPDATE acente_s_content SET
		ascName=:ascName,
		ascAcenteId=:ascAcenteId,
		ascPersonelId=:ascPersonelId,
		ascCreatedAt=:ascCreatedAt,
		ascUpdatedAt=:ascUpdatedAt,
        ascSyncAt=:ascSyncAt
		WHERE ascId=:ascId
	");
    $update=$query->execute(array(
		'ascName'=>$data['ascName'],
		'ascAcenteId'=>$data['ascAcenteId'],
		'ascPersonelId'=>$data['ascPersonelId'],
		'ascCreatedAt'=>$data['ascCreatedAt'],
		'ascUpdatedAt'=>$data['ascUpdatedAt'],
        'ascSyncAt'=>$currentDate."000",
		'ascId'=>$data['ascId']
    ));
    if ($update) {
       echo json_encode("ok");

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

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


?>