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

    $query = $db->query("SELECT * FROM report", PDO::FETCH_ASSOC);
    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $report[] = array(
            	"rpId" => $q["rpId"],
            	"rpDate" => $q["rpDate"],
            	"rpSubject" => $q["rpSubject"],
            	"rpOrder" => $q["rpOrder"],
            	"rpCustomerId" => $q["rpCustomerId"],
            	"rpExporterId" => $q["rpExporterId"],
            	"rpProductId" => $q["rpProductId"],
            	"rpQuantity" => $q["rpQuantity"],
            	"rpSampling" => $q["rpSampling"],
            	"rpAnalysisNote" => $q["rpAnalysisNote"],
            	"rpNotes" => $q["rpNotes"],
            	"rpPersonelId" => $q["rpPersonelId"],
            	"rpCreatedAt" => $q["rpCreatedAt"],
            	"rpUpdatedAt" => $q["rpUpdatedAt"],
                "rpSyncAt" => $q["rpSyncAt"]
            );
        }
        $result = json_encode($report, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $report[] = array(
            	"rpId" => $q["rpId"],
            	"rpDate" => $q["rpDate"],
            	"rpSubject" => $q["rpSubject"],
            	"rpOrder" => $q["rpOrder"],
            	"rpCustomerId" => $q["rpCustomerId"],
            	"rpExporterId" => $q["rpExporterId"],
            	"rpProductId" => $q["rpProductId"],
            	"rpQuantity" => $q["rpQuantity"],
            	"rpSampling" => $q["rpSampling"],
            	"rpAnalysisNote" => $q["rpAnalysisNote"],
            	"rpNotes" => $q["rpNotes"],
            	"rpPersonelId" => $q["rpPersonelId"],
            	"rpCreatedAt" => $q["rpCreatedAt"],
            	"rpUpdatedAt" => $q["rpUpdatedAt"],
                "rpSyncAt" => $q["rpSyncAt"]
            );
        }
        $result = json_encode($report, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $report[] = array(
            	"rpId" => $q["rpId"],
            	"rpDate" => $q["rpDate"],
            	"rpSubject" => $q["rpSubject"],
            	"rpOrder" => $q["rpOrder"],
            	"rpCustomerId" => $q["rpCustomerId"],
            	"rpExporterId" => $q["rpExporterId"],
            	"rpProductId" => $q["rpProductId"],
            	"rpQuantity" => $q["rpQuantity"],
            	"rpSampling" => $q["rpSampling"],
            	"rpAnalysisNote" => $q["rpAnalysisNote"],
            	"rpNotes" => $q["rpNotes"],
            	"rpPersonelId" => $q["rpPersonelId"],
            	"rpCreatedAt" => $q["rpCreatedAt"],
            	"rpUpdatedAt" => $q["rpUpdatedAt"],
                "rpSyncAt" => $q["rpSyncAt"]
            );
        }
        $result = json_encode($report, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $report[] = array(
            	"rpId" => $q["rpId"],
            	"rpDate" => $q["rpDate"],
            	"rpSubject" => $q["rpSubject"],
            	"rpOrder" => $q["rpOrder"],
            	"rpCustomerId" => $q["rpCustomerId"],
            	"rpExporterId" => $q["rpExporterId"],
            	"rpProductId" => $q["rpProductId"],
            	"rpQuantity" => $q["rpQuantity"],
            	"rpSampling" => $q["rpSampling"],
            	"rpAnalysisNote" => $q["rpAnalysisNote"],
            	"rpNotes" => $q["rpNotes"],
            	"rpPersonelId" => $q["rpPersonelId"],
            	"rpCreatedAt" => $q["rpCreatedAt"],
            	"rpUpdatedAt" => $q["rpUpdatedAt"],
                "rpSyncAt" => $q["rpSyncAt"]
            );
        }
        $result = json_encode($report, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $report[] = array(
            	"rpId" => $q["rpId"],
            	"rpDate" => $q["rpDate"],
            	"rpSubject" => $q["rpSubject"],
            	"rpOrder" => $q["rpOrder"],
            	"rpCustomerId" => $q["rpCustomerId"],
            	"rpExporterId" => $q["rpExporterId"],
            	"rpProductId" => $q["rpProductId"],
            	"rpQuantity" => $q["rpQuantity"],
            	"rpSampling" => $q["rpSampling"],
            	"rpAnalysisNote" => $q["rpAnalysisNote"],
            	"rpNotes" => $q["rpNotes"],
            	"rpPersonelId" => $q["rpPersonelId"],
            	"rpCreatedAt" => $q["rpCreatedAt"],
            	"rpUpdatedAt" => $q["rpUpdatedAt"],
                "rpSyncAt" => $q["rpSyncAt"]
            );
        }
        $result = json_encode($report, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $report[] = array(
            	"rpId" => $q["rpId"],
            	"rpDate" => $q["rpDate"],
            	"rpSubject" => $q["rpSubject"],
            	"rpOrder" => $q["rpOrder"],
            	"rpCustomerId" => $q["rpCustomerId"],
            	"rpExporterId" => $q["rpExporterId"],
            	"rpProductId" => $q["rpProductId"],
            	"rpQuantity" => $q["rpQuantity"],
            	"rpSampling" => $q["rpSampling"],
            	"rpAnalysisNote" => $q["rpAnalysisNote"],
            	"rpNotes" => $q["rpNotes"],
            	"rpPersonelId" => $q["rpPersonelId"],
            	"rpCreatedAt" => $q["rpCreatedAt"],
            	"rpUpdatedAt" => $q["rpUpdatedAt"],
                "rpSyncAt" => $q["rpSyncAt"]
            );
        }
        $result = json_encode($report, 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 report WHERE rpSyncAt >= ".$_GET['sync']." AND  rpPersonelId !='".$_GET['sync_personel']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $report[] = array(
                "rpId" => $q["rpId"],
                "rpDate" => $q["rpDate"],
                "rpSubject" => $q["rpSubject"],
                "rpOrder" => $q["rpOrder"],
                "rpCustomerId" => $q["rpCustomerId"],
                "rpExporterId" => $q["rpExporterId"],
                "rpProductId" => $q["rpProductId"],
                "rpQuantity" => $q["rpQuantity"],
                "rpSampling" => $q["rpSampling"],
                "rpAnalysisNote" => $q["rpAnalysisNote"],
                "rpNotes" => $q["rpNotes"],
                "rpPersonelId" => $q["rpPersonelId"],
                "rpCreatedAt" => $q["rpCreatedAt"],
                "rpUpdatedAt" => $q["rpUpdatedAt"],
                "rpSyncAt" => $q["rpSyncAt"]
            );
        }
        $result = json_encode($report, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "POST") {
    $currentDate =  time();
    $query = $db->prepare("INSERT INTO report SET
		rpId=:rpId,
		rpDate=:rpDate,
		rpSubject=:rpSubject,
		rpOrder=:rpOrder,
		rpCustomerId=:rpCustomerId,
		rpExporterId=:rpExporterId,
		rpProductId=:rpProductId,
		rpQuantity=:rpQuantity,
		rpSampling=:rpSampling,
		rpAnalysisNote=:rpAnalysisNote,
		rpNotes=:rpNotes,
		rpPersonelId=:rpPersonelId,
		rpCreatedAt=:rpCreatedAt,
		rpUpdatedAt=:rpUpdatedAt,
        rpSyncAt=:rpSyncAt
	");
    $insert=$query->execute(array(
		'rpId'=>$data['rpId'],
		'rpDate'=>$data['rpDate'],
		'rpSubject'=>$data['rpSubject'],
		'rpOrder'=>$data['rpOrder'],
		'rpCustomerId'=>$data['rpCustomerId'],
		'rpExporterId'=>$data['rpExporterId'],
		'rpProductId'=>$data['rpProductId'],
		'rpQuantity'=>$data['rpQuantity'],
		'rpSampling'=>$data['rpSampling'],
		'rpAnalysisNote'=>$data['rpAnalysisNote'],
		'rpNotes'=>$data['rpNotes'],
		'rpPersonelId'=>$data['rpPersonelId'],
		'rpCreatedAt'=>$data['rpCreatedAt'],
		'rpUpdatedAt' => $data['rpUpdatedAt'],
        'rpSyncAt' => $currentDate."000"
    ));
    if ($insert) {
        echo json_encode("ok");

    } else {
        echo json_encode("error");
    }
} else if ($method == "PUT") {
    $currentDate =  time();
    $query = $db->prepare("UPDATE report SET
		rpDate=:rpDate,
		rpSubject=:rpSubject,
		rpOrder=:rpOrder,
		rpCustomerId=:rpCustomerId,
		rpExporterId=:rpExporterId,
		rpProductId=:rpProductId,
		rpQuantity=:rpQuantity,
		rpSampling=:rpSampling,
		rpAnalysisNote=:rpAnalysisNote,
		rpNotes=:rpNotes,
		rpPersonelId=:rpPersonelId,
		rpCreatedAt=:rpCreatedAt,
		rpUpdatedAt=:rpUpdatedAt,
        rpSyncAt=:rpSyncAt
		WHERE rpId=:rpId
	");
    $update=$query->execute(array(
		'rpDate'=>$data['rpDate'],
		'rpSubject'=>$data['rpSubject'],
		'rpOrder'=>$data['rpOrder'],
		'rpCustomerId'=>$data['rpCustomerId'],
		'rpExporterId'=>$data['rpExporterId'],
		'rpProductId'=>$data['rpProductId'],
		'rpQuantity'=>$data['rpQuantity'],
		'rpSampling'=>$data['rpSampling'],
		'rpAnalysisNote'=>$data['rpAnalysisNote'],
		'rpNotes'=>$data['rpNotes'],
		'rpPersonelId'=>$data['rpPersonelId'],
		'rpCreatedAt'=>$data['rpCreatedAt'],
		'rpUpdatedAt' => $data['rpUpdatedAt'],
        'rpSyncAt' => $currentDate."000",
		'rpId'=>$data['rpId']
    ));
    if ($update) {
        echo json_encode("ok");

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

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

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

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


?>