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

    $query = $db->query("SELECT * FROM acente", PDO::FETCH_ASSOC);
    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $acente[] = array(
            	"acId" => $q["acId"],
            	"acCustomerId" => $q["acCustomerId"],
            	"acExporterId" => $q["acExporterId"],
            	"acProductId" => $q["acProductId"],
            	"acPersonelId" => $q["acPersonelId"],
            	"acCreatedAt" => $q["acCreatedAt"],
            	"acUpdatedAt" => $q["acUpdatedAt"],
                "acSyncAt" => $q["acUpdatedAt"]
            );
        }
        $result = json_encode($acente, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $acente[] = array(
            	"acId" => $q["acId"],
            	"acCustomerId" => $q["acCustomerId"],
            	"acExporterId" => $q["acExporterId"],
            	"acProductId" => $q["acProductId"],
            	"acPersonelId" => $q["acPersonelId"],
            	"acCreatedAt" => $q["acCreatedAt"],
            	"acUpdatedAt" => $q["acUpdatedAt"],
                "acSyncAt" => $q["acUpdatedAt"]
            );
        }
        $result = json_encode($acente, JSON_UNESCAPED_UNICODE);
        echo $result;

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

    $query = $db->query("SELECT * FROM acente WHERE acCustomerId='".$_GET['customer_id']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $acente[] = array(
                "acId" => $q["acId"],
                "acCustomerId" => $q["acCustomerId"],
                "acExporterId" => $q["acExporterId"],
                "acProductId" => $q["acProductId"],
                "acPersonelId" => $q["acPersonelId"],
                "acCreatedAt" => $q["acCreatedAt"],
                "acUpdatedAt" => $q["acUpdatedAt"],
                "acSyncAt" => $q["acUpdatedAt"]
            );
        }
        $result = json_encode($acente, JSON_UNESCAPED_UNICODE);
        echo $result;

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

    $query = $db->query("SELECT * FROM acente WHERE acExporterId='".$_GET['exporter_id']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $acente[] = array(
                "acId" => $q["acId"],
                "acCustomerId" => $q["acCustomerId"],
                "acExporterId" => $q["acExporterId"],
                "acProductId" => $q["acProductId"],
                "acPersonelId" => $q["acPersonelId"],
                "acCreatedAt" => $q["acCreatedAt"],
                "acUpdatedAt" => $q["acUpdatedAt"],
                "acSyncAt" => $q["acUpdatedAt"]
            );
        }
        $result = json_encode($acente, JSON_UNESCAPED_UNICODE);
        echo $result;

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

    $query = $db->query("SELECT * FROM acente WHERE acProductId='".$_GET['product_id']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $acente[] = array(
                "acId" => $q["acId"],
                "acCustomerId" => $q["acCustomerId"],
                "acExporterId" => $q["acExporterId"],
                "acProductId" => $q["acProductId"],
                "acPersonelId" => $q["acPersonelId"],
                "acCreatedAt" => $q["acCreatedAt"],
                "acUpdatedAt" => $q["acUpdatedAt"],
                "acSyncAt" => $q["acUpdatedAt"]
            );
        }
        $result = json_encode($acente, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $acente[] = array(
                "acId" => $q["acId"],
                "acCustomerId" => $q["acCustomerId"],
                "acExporterId" => $q["acExporterId"],
                "acProductId" => $q["acProductId"],
                "acPersonelId" => $q["acPersonelId"],
                "acCreatedAt" => $q["acCreatedAt"],
                "acUpdatedAt" => $q["acUpdatedAt"],
                "acSyncAt" => $q["acUpdatedAt"]
            );
        }
        $result = json_encode($acente, 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 WHERE acSyncAt >= ".$_GET['sync']." AND acPersonelId !='".$_GET['sync_personel']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $acente[] = array(
                "acId" => $q["acId"],
                "acCustomerId" => $q["acCustomerId"],
                "acExporterId" => $q["acExporterId"],
                "acProductId" => $q["acProductId"],
                "acPersonelId" => $q["acPersonelId"],
                "acCreatedAt" => $q["acCreatedAt"],
                "acUpdatedAt" => $q["acUpdatedAt"],
                "acSyncAt" => $q["acUpdatedAt"]
            );
        }
        $result = json_encode($acente, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "POST") {
    $currentDate =  time();
    $query = $db->prepare("INSERT INTO acente SET
		acId=:acId,
		acCustomerId=:acCustomerId,
		acExporterId=:acExporterId,
		acProductId=:acProductId,
		acPersonelId=:acPersonelId,
		acCreatedAt=:acCreatedAt,
		acUpdatedAt=:acUpdatedAt,
        acSyncAt=:acSyncAt
	");
    $insert=$query->execute(array(
		'acId'=>$data['acId'],
		'acCustomerId'=>$data['acCustomerId'],
		'acExporterId'=>$data['acExporterId'],
		'acProductId'=>$data['acProductId'],
		'acPersonelId'=>$data['acPersonelId'],
		'acCreatedAt'=>$data['acCreatedAt'],
		'acUpdatedAt'=>$data['acUpdatedAt'],
        'acSyncAt'=>$currentDate."000"

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

    } else {
        echo json_encode("error");
    }
} else if ($method == "PUT") {
    $currentDate =  time();
    $query = $db->prepare("UPDATE acente SET
		acCustomerId=:acCustomerId,
		acExporterId=:acExporterId,
		acProductId=:acProductId,
		acPersonelId=:acPersonelId,
		acCreatedAt=:acCreatedAt,
		acUpdatedAt=:acUpdatedAt,
        acSyncAt=:acSyncAt
		WHERE acId=:acId
	");
    $update=$query->execute(array(
		'acCustomerId'=>$data['acCustomerId'],
		'acExporterId'=>$data['acExporterId'],
		'acProductId'=>$data['acProductId'],
		'acPersonelId'=>$data['acPersonelId'],
		'acCreatedAt'=>$data['acCreatedAt'],
		'acUpdatedAt'=>$data['acUpdatedAt'],
        'acSyncAt'=>$currentDate."000",
		'acId'=>$data['acId']
    ));
    if ($update) {
        echo json_encode("ok");

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

    $query_a2 = $db->prepare("DELETE FROM acente_s_content WHERE ascAcenteId=:ascAcenteId");
    $query_a2->execute(array(
        'ascAcenteId'=>$_GET['id']
    ));

    $query = $db->prepare("DELETE FROM acente WHERE acId=:acId");
    $update=$query->execute(array(
		'acId'=>$_GET['id']
    ));
    if ($update) {
        echo json_encode("ok");

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


?>