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

    $query = $db->query("SELECT * FROM product", PDO::FETCH_ASSOC);
    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $product[] = array(
            	"ptId" => $q["ptId"],
            	"ptName" => $q["ptName"],
            	"ptProductCategory" => $q["ptProductCategory"],
            	"ptPersonelId" => $q["ptPersonelId"],
            	"ptCreatedAt" => $q["ptCreatedAt"],
            	"ptUpdatedAt" => $q["ptUpdatedAt"],
                "ptSyncAt" => $q["ptSyncAt"]
            );
        }
        $result = json_encode($product, JSON_UNESCAPED_UNICODE);
        echo $result;

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

    $query = $db->query("SELECT * FROM product WHERE ptProductCategory='".$_GET['product_category']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $product[] = array(
            	"ptId" => $q["ptId"],
            	"ptName" => $q["ptName"],
            	"ptProductCategory" => $q["ptProductCategory"],
            	"ptPersonelId" => $q["ptPersonelId"],
            	"ptCreatedAt" => $q["ptCreatedAt"],
            	"ptUpdatedAt" => $q["ptUpdatedAt"],
                "ptSyncAt" => $q["ptSyncAt"]
            );
        }
        $result = json_encode($product, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $product[] = array(
            	"ptId" => $q["ptId"],
                "ptName" => $q["ptName"],
                "ptProductCategory" => $q["ptProductCategory"],
                "ptPersonelId" => $q["ptPersonelId"],
                "ptCreatedAt" => $q["ptCreatedAt"],
                "ptUpdatedAt" => $q["ptUpdatedAt"],
                "ptSyncAt" => $q["ptSyncAt"]
            );
        }
        $result = json_encode($product, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $product[] = array(
                "ptId" => $q["ptId"],
                "ptName" => $q["ptName"],
                "ptProductCategory" => $q["ptProductCategory"],
                "ptPersonelId" => $q["ptPersonelId"],
                "ptCreatedAt" => $q["ptCreatedAt"],
                "ptUpdatedAt" => $q["ptUpdatedAt"],
                "ptSyncAt" => $q["ptSyncAt"]
            );
        }
        $result = json_encode($product, 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 WHERE ptSyncAt >= ".$_GET['sync']." AND  ptPersonelId !='".$_GET['sync_personel']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $product[] = array(
                "ptId" => $q["ptId"],
                "ptName" => $q["ptName"],
                "ptProductCategory" => $q["ptProductCategory"],
                "ptPersonelId" => $q["ptPersonelId"],
                "ptCreatedAt" => $q["ptCreatedAt"],
                "ptUpdatedAt" => $q["ptUpdatedAt"],
                "ptSyncAt" => $q["ptSyncAt"]
            );
        }
        $result = json_encode($product, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "POST") {
    $currentDate =  time();
    $query = $db->prepare("INSERT INTO product SET
		ptId=:ptId,
		ptName=:ptName,
		ptProductCategory=:ptProductCategory,
		ptPersonelId=:ptPersonelId,
		ptCreatedAt=:ptCreatedAt,
		ptUpdatedAt=:ptUpdatedAt,
        ptSyncAt=:ptSyncAt
	");
    $insert=$query->execute(array(
		'ptId'=>$data['ptId'],
		'ptName'=>$data['ptName'],
		'ptProductCategory'=>$data['ptProductCategory'],
		'ptPersonelId'=>$data['ptPersonelId'],
		'ptCreatedAt'=>$data['ptCreatedAt'],
		'ptUpdatedAt'=>$data['ptUpdatedAt'],
        'ptSyncAt'=>$currentDate."000"
    ));
    if ($insert) {
        echo json_encode("ok");

    } else {
        echo json_encode("error");
    }
} else if ($method == "PUT") {
    $currentDate =  time();
    $query = $db->prepare("UPDATE product SET
		ptName=:ptName,
		ptProductCategory=:ptProductCategory,
		ptPersonelId=:ptPersonelId,
		ptCreatedAt=:ptCreatedAt,
		ptUpdatedAt=:ptUpdatedAt,
        ptSyncAt=:ptSyncAt
		WHERE ptId=:ptId
	");
    $update=$query->execute(array(
		'ptName'=>$data['ptName'],
		'ptProductCategory'=>$data['ptProductCategory'],
		'ptPersonelId'=>$data['ptPersonelId'],
		'ptCreatedAt'=>$data['ptCreatedAt'],
		'ptUpdatedAt'=>$data['ptUpdatedAt'],
        'ptSyncAt'=>$currentDate."000",
		'ptId'=>$data['ptId']
    ));
    if ($update) {
        echo json_encode("ok");

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

    $query_p2 = $db->prepare("DELETE FROM product_analysis WHERE ptaProductId=:ptaProductId");
    $query_p2->execute(array(
        'ptaProductId'=>$_GET['id']
    ));

    $query_p3 = $db->prepare("DELETE FROM product_sampling WHERE ptsProductId=:ptsProductId");
    $query_p3->execute(array(
        'ptsProductId'=>$_GET['id']
    ));

    $query_p4 = $db->prepare("DELETE FROM report WHERE rpProductId=:rpProductId");
    $query_p4->execute(array(
        'rpProductId'=>$_GET['id']
    ));

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

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