<?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_sampling", PDO::FETCH_ASSOC);
    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $product_sampling[] = array(
            	"ptsId" => $q["ptsId"],
            	"ptsName" => $q["ptsName"],
            	"ptsProductId" => $q["ptsProductId"],
            	"ptsPersonelId" => $q["ptsPersonelId"],
            	"ptsCreatedAt" => $q["ptsCreatedAt"],
            	"ptsUpdatedAt" => $q["ptsUpdatedAt"],
                "ptsSyncAt" => $q['ptsSyncAt']
            );
        }
        $result = json_encode($product_sampling, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $product_sampling[] = array(
            	"ptsId" => $q["ptsId"],
                "ptsName" => $q["ptsName"],
                "ptsProductId" => $q["ptsProductId"],
                "ptsPersonelId" => $q["ptsPersonelId"],
                "ptsCreatedAt" => $q["ptsCreatedAt"],
                "ptsUpdatedAt" => $q["ptsUpdatedAt"],
                "ptsSyncAt" => $q['ptsSyncAt']
            );
        }
        $result = json_encode($product_sampling, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $product_sampling[] = array(
                "ptsId" => $q["ptsId"],
                "ptsName" => $q["ptsName"],
                "ptsProductId" => $q["ptsProductId"],
                "ptsPersonelId" => $q["ptsPersonelId"],
                "ptsCreatedAt" => $q["ptsCreatedAt"],
                "ptsUpdatedAt" => $q["ptsUpdatedAt"],
                "ptsSyncAt" => $q['ptsSyncAt']
            );
        }
        $result = json_encode($product_sampling, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $product_sampling[] = array(
                "ptsId" => $q["ptsId"],
                "ptsName" => $q["ptsName"],
                "ptsProductId" => $q["ptsProductId"],
                "ptsPersonelId" => $q["ptsPersonelId"],
                "ptsCreatedAt" => $q["ptsCreatedAt"],
                "ptsUpdatedAt" => $q["ptsUpdatedAt"],
                "ptsSyncAt" => $q['ptsSyncAt']
            );
        }
        $result = json_encode($product_sampling, 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_sampling WHERE ptsSyncAt >= ".$_GET['sync']." AND ptsPersonelId !='".$_GET['sync_personel']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $product_sampling[] = array(
                "ptsId" => $q["ptsId"],
                "ptsName" => $q["ptsName"],
                "ptsProductId" => $q["ptsProductId"],
                "ptsPersonelId" => $q["ptsPersonelId"],
                "ptsCreatedAt" => $q["ptsCreatedAt"],
                "ptsUpdatedAt" => $q["ptsUpdatedAt"],
                "ptsSyncAt" => $q['ptsSyncAt']
            );
        }
        $result = json_encode($product_sampling, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "POST") {
    $currentDate =  time();
    $query = $db->prepare("INSERT INTO product_sampling SET
		ptsId=:ptsId,
		ptsName=:ptsName,
		ptsProductId=:ptsProductId,
		ptsPersonelId=:ptsPersonelId,
		ptsCreatedAt=:ptsCreatedAt,
		ptsUpdatedAt=:ptsUpdatedAt,
        ptsSyncAt=:ptsSyncAt
	");
    $insert=$query->execute(array(
		'ptsId'=>$data['ptsId'],
		'ptsName'=>$data['ptsName'],
		'ptsProductId'=>$data['ptsProductId'],
		'ptsPersonelId'=>$data['ptsPersonelId'],
		'ptsCreatedAt'=>$data['ptsCreatedAt'],
		'ptsUpdatedAt'=>$data['ptsUpdatedAt'],
        'ptsSyncAt'=>$currentDate."000"
    ));
    if ($insert) {
        echo json_encode("ok");

    } else {
        echo json_encode("error");
    }
} else if ($method == "PUT") {
    $currentDate =  time();
    $query = $db->prepare("UPDATE product_sampling SET
		ptsName=:ptsName,
		ptsProductId=:ptsProductId,
		ptsPersonelId=:ptsPersonelId,
		ptsCreatedAt=:ptsCreatedAt,
		ptsUpdatedAt=:ptsUpdatedAt,
        ptsSyncAt=:ptsSyncAt
		WHERE ptsId=:ptsId
	");
    $update=$query->execute(array(
		'ptsName'=>$data['ptsName'],
		'ptsProductId'=>$data['ptsProductId'],
		'ptsPersonelId'=>$data['ptsPersonelId'],
		'ptsCreatedAt'=>$data['ptsCreatedAt'],
		'ptsUpdatedAt'=>$data['ptsUpdatedAt'],
        'ptsSyncAt'=>$currentDate."000",
		'ptsId'=>$data['ptsId']
    ));
    if ($update) {
        echo json_encode("ok");

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

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


?>