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

    $query = $db->query("SELECT * FROM analysis", PDO::FETCH_ASSOC);
    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $analysis[] = array(
            	"asId" => $q["asId"],
            	"asName" => $q["asName"],
            	"asValue" => $q["asValue"],
            	"asActuel1" => $q["asActuel1"],
            	"asActuel2" => $q["asActuel2"],
            	"asAverage" => $q["asAverage"],
            	"asReportId" => $q["asReportId"],
            	"asPersonelId" => $q["asPersonelId"],
            	"asCreatedAt" => $q["asCreatedAt"],
            	"asUpdatedAt" => $q["asUpdatedAt"],
                "asSyncAt" => $q["asSyncAt"]
            );
        }
        $result = json_encode($analysis, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $analysis[] = array(
            	"asId" => $q["asId"],
            	"asName" => $q["asName"],
            	"asValue" => $q["asValue"],
            	"asActuel1" => $q["asActuel1"],
            	"asActuel2" => $q["asActuel2"],
            	"asAverage" => $q["asAverage"],
            	"asReportId" => $q["asReportId"],
            	"asPersonelId" => $q["asPersonelId"],
            	"asCreatedAt" => $q["asCreatedAt"],
            	"asUpdatedAt" => $q["asUpdatedAt"],
                "asSyncAt" => $q["asSyncAt"]
            );
        }
        $result = json_encode($analysis, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $analysis[] = array(
            	"asId" => $q["asId"],
            	"asName" => $q["asName"],
            	"asValue" => $q["asValue"],
            	"asActuel1" => $q["asActuel1"],
            	"asActuel2" => $q["asActuel2"],
            	"asAverage" => $q["asAverage"],
            	"asReportId" => $q["asReportId"],
            	"asPersonelId" => $q["asPersonelId"],
            	"asCreatedAt" => $q["asCreatedAt"],
            	"asUpdatedAt" => $q["asUpdatedAt"],
                "asSyncAt" => $q["asSyncAt"]
            );
        }
        $result = json_encode($analysis, JSON_UNESCAPED_UNICODE);
        echo $result;

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

    $query = $db->query("SELECT * FROM analysis WHERE asReportId='".$_GET['report_id']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $analysis[] = array(
            	"asId" => $q["asId"],
            	"asName" => $q["asName"],
            	"asValue" => $q["asValue"],
            	"asActuel1" => $q["asActuel1"],
            	"asActuel2" => $q["asActuel2"],
            	"asAverage" => $q["asAverage"],
            	"asReportId" => $q["asReportId"],
            	"asPersonelId" => $q["asPersonelId"],
            	"asCreatedAt" => $q["asCreatedAt"],
            	"asUpdatedAt" => $q["asUpdatedAt"],
                "asSyncAt" => $q["asSyncAt"]
            );
        }
        $result = json_encode($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 analysis WHERE asSyncAt >= ".$_GET['sync']." AND  asPersonelId !='".$_GET['sync_personel']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $analysis[] = array(
                "asId" => $q["asId"],
                "asName" => $q["asName"],
                "asValue" => $q["asValue"],
                "asActuel1" => $q["asActuel1"],
                "asActuel2" => $q["asActuel2"],
                "asAverage" => $q["asAverage"],
                "asReportId" => $q["asReportId"],
                "asPersonelId" => $q["asPersonelId"],
                "asCreatedAt" => $q["asCreatedAt"],
                "asUpdatedAt" => $q["asUpdatedAt"],
                "asSyncAt" => $q["asSyncAt"]
            );
        }
        $result = json_encode($analysis, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "POST") {
    $currentDate =  time();
    $query = $db->prepare("INSERT INTO analysis SET
		asId=:asId,
		asName=:asName,
		asValue=:asValue,
		asActuel1=:asActuel1,
		asActuel2=:asActuel2,
		asAverage=:asAverage,
		asReportId=:asReportId,
		asPersonelId=:asPersonelId,
		asCreatedAt=:asCreatedAt,
		asUpdatedAt=:asUpdatedAt,
        asSyncAt=:asSyncAt
	");
    $insert=$query->execute(array(
		'asId'=>$data['asId'],
		'asName'=>$data['asName'],
		'asValue'=>$data['asValue'],
		'asActuel1'=>$data['asActuel1'],
		'asActuel2'=>$data['asActuel2'],
		'asAverage'=>$data['asAverage'],
		'asReportId'=>$data['asReportId'],
		'asPersonelId'=>$data['asPersonelId'],
		'asCreatedAt'=>$data['asCreatedAt'],
		'asUpdatedAt'=>$data['asUpdatedAt'],
        'asSyncAt'=>$currentDate."000"
    ));
    if ($insert) {
        echo json_encode("ok");

    } else {
        echo json_encode("error");
    }
} else if ($method == "PUT") {
    $currentDate =  time();
    $query = $db->prepare("UPDATE analysis SET
		asName=:asName,
		asValue=:asValue,
		asActuel1=:asActuel1,
		asActuel2=:asActuel2,
		asAverage=:asAverage,
		asReportId=:asReportId,
		asPersonelId=:asPersonelId,
		asCreatedAt=:asCreatedAt,
		asUpdatedAt=:asUpdatedAt,
        asSyncAt=:asSyncAt
		WHERE asId=:asId
	");
    $update=$query->execute(array(
		'asName'=>$data['asName'],
		'asValue'=>$data['asValue'],
		'asActuel1'=>$data['asActuel1'],
		'asActuel2'=>$data['asActuel2'],
		'asAverage'=>$data['asAverage'],
		'asReportId'=>$data['asReportId'],
		'asPersonelId'=>$data['asPersonelId'],
		'asCreatedAt'=>$data['asCreatedAt'],
		'asUpdatedAt'=>$data['asUpdatedAt'],
        'asSyncAt'=>$currentDate."000",
		'asId'=>$data['asId']
    ));
    if ($update) {
        echo json_encode("ok");

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

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


?>