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

    $query = $db->query("SELECT * FROM product_analysis", PDO::FETCH_ASSOC);
    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $product_analysis[] = array(
            	"ptaId" => $q["ptaId"],
            	"ptaName" => $q["ptaName"],
            	"ptaProductId" => $q["ptaProductId"],
            	"ptaPersonelId" => $q["ptaPersonelId"],
            	"ptaCreatedAt" => $q["ptaCreatedAt"],
            	"ptaUpdatedAt" => $q["ptaUpdatedAt"],
                "ptaSyncAt" => $q["ptaSyncAt"]
            );
        }
        $result = json_encode($product_analysis, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $product_analysis[] = array(
            	"ptaId" => $q["ptaId"],
                "ptaName" => $q["ptaName"],
                "ptaProductId" => $q["ptaProductId"],
                "ptaPersonelId" => $q["ptaPersonelId"],
                "ptaCreatedAt" => $q["ptaCreatedAt"],
                "ptaUpdatedAt" => $q["ptaUpdatedAt"],
                "ptaSyncAt" => $q["ptaSyncAt"]
            );
        }
        $result = json_encode($product_analysis, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $product_analysis[] = array(
                "ptaId" => $q["ptaId"],
                "ptaName" => $q["ptaName"],
                "ptaProductId" => $q["ptaProductId"],
                "ptaPersonelId" => $q["ptaPersonelId"],
                "ptaCreatedAt" => $q["ptaCreatedAt"],
                "ptaUpdatedAt" => $q["ptaUpdatedAt"],
                "ptaSyncAt" => $q["ptaSyncAt"]
            );
        }
        $result = json_encode($product_analysis, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $product_analysis[] = array(
                "ptaId" => $q["ptaId"],
                "ptaName" => $q["ptaName"],
                "ptaProductId" => $q["ptaProductId"],
                "ptaPersonelId" => $q["ptaPersonelId"],
                "ptaCreatedAt" => $q["ptaCreatedAt"],
                "ptaUpdatedAt" => $q["ptaUpdatedAt"],
                "ptaSyncAt" => $q["ptaSyncAt"]
            );
        }
        $result = json_encode($product_analysis, 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 product_analysis WHERE ptaSyncAt >= ".$_GET['sync']." AND  ptaPersonelId !='".$_GET['sync_personel']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $product_analysis[] = array(
                "ptaId" => $q["ptaId"],
                "ptaName" => $q["ptaName"],
                "ptaProductId" => $q["ptaProductId"],
                "ptaPersonelId" => $q["ptaPersonelId"],
                "ptaCreatedAt" => $q["ptaCreatedAt"],
                "ptaUpdatedAt" => $q["ptaUpdatedAt"],
                "ptaSyncAt" => $q["ptaSyncAt"]
            );
        }
        $result = json_encode($product_analysis, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "POST") {
    $currentDate =  time();
    $query = $db->prepare("INSERT INTO product_analysis SET
		ptaId=:ptaId,
		ptaName=:ptaName,
		ptaProductId=:ptaProductId,
		ptaPersonelId=:ptaPersonelId,
		ptaCreatedAt=:ptaCreatedAt,
		ptaUpdatedAt=:ptaUpdatedAt,
        ptaSyncAt=:ptaSyncAt
	");
    $insert=$query->execute(array(
		'ptaId'=>$data['ptaId'],
		'ptaName'=>$data['ptaName'],
		'ptaProductId'=>$data['ptaProductId'],
		'ptaPersonelId'=>$data['ptaPersonelId'],
		'ptaCreatedAt'=>$data['ptaCreatedAt'],
		'ptaUpdatedAt'=>$data['ptaUpdatedAt'],
        'ptaSyncAt'=>$currentDate."000"
    ));
    if ($insert) {
        echo json_encode("ok");

    } else {
        echo json_encode("error");
    }
} else if ($method == "PUT") {
    $currentDate =  time();
    $query = $db->prepare("UPDATE product_analysis SET
		ptaName=:ptaName,
		ptaProductId=:ptaProductId,
		ptaPersonelId=:ptaPersonelId,
		ptaCreatedAt=:ptaCreatedAt,
		ptaUpdatedAt=:ptaUpdatedAt,
        ptaSyncAt=:ptaSyncAt
		WHERE ptaId=:ptaId
	");
    $update=$query->execute(array(
		'ptaName'=>$data['ptaName'],
		'ptaProductId'=>$data['ptaProductId'],
		'ptaPersonelId'=>$data['ptaPersonelId'],
		'ptaCreatedAt'=>$data['ptaCreatedAt'],
		'ptaUpdatedAt'=>$data['ptaUpdatedAt'],
        'ptaSyncAt'=>$currentDate."000",
		'ptaId'=>$data['ptaId']
    ));
    if ($update) {
        echo json_encode("ok");

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

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


?>