<?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['email'])) {

    $query = $db->query("SELECT * FROM personel", PDO::FETCH_ASSOC);
    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $personel[] = array(
            	"prlId" => $q["prlId"],
            	"prlName" => $q["prlName"],
            	"prlLastName" => $q["prlLastName"],
            	"prlPhone" => $q["prlPhone"],
            	"prlEmail" => $q["prlEmail"],
            	"prlPassword" => $q["prlPassword"],
            	"prlYetki" => $q["prlYetki"],
            	"prlUserYetki" => $q["prlUserYetki"],
				"prlIsEnabled" => $q["prlIsEnabled"],
				"prlCreatedAt" => $q["prlCreatedAt"]
            );
        }
        $result = json_encode($personel, JSON_UNESCAPED_UNICODE);
        echo $result;

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

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

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $personel[] = array(
            	"prlId" => $q["prlId"],
            	"prlName" => $q["prlName"],
            	"prlLastName" => $q["prlLastName"],
            	"prlPhone" => $q["prlPhone"],
            	"prlEmail" => $q["prlEmail"],
            	"prlPassword" => $q["prlPassword"],
            	"prlYetki" => $q["prlYetki"],
				"prlUserYetki" => $q["prlUserYetki"],
				"prlIsEnabled" => $q["prlIsEnabled"],
            	"prlCreatedAt" => $q["prlCreatedAt"]
            );
        }
        $result = json_encode($personel, JSON_UNESCAPED_UNICODE);
        echo $result;

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

	$query = $db->query("SELECT * FROM personel WHERE prlEmail='".$_GET['email']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $personel[] = array(
            	"prlId" => $q["prlId"],
            	"prlName" => $q["prlName"],
            	"prlLastName" => $q["prlLastName"],
            	"prlPhone" => $q["prlPhone"],
            	"prlEmail" => $q["prlEmail"],
            	"prlPassword" => $q["prlPassword"],
            	"prlYetki" => $q["prlYetki"],
				"prlUserYetki" => $q["prlUserYetki"],
				"prlIsEnabled" => $q["prlIsEnabled"],
            	"prlCreatedAt" => $q["prlCreatedAt"]
            );
        }
        $result = json_encode($personel, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }

} else if ($method == "POST") {
    $query = $db->prepare("INSERT INTO personel SET
        prlId=:prlId,
		prlName=:prlName,
		prlLastName=:prlLastName,
		prlPhone=:prlPhone,
		prlEmail=:prlEmail,
		prlPassword=:prlPassword,
		prlYetki=:prlYetki,
		prlUserYetki=:prlUserYetki,
		prlIsEnabled=:prlIsEnabled,
		prlCreatedAt=:prlCreatedAt
	");
    $insert=$query->execute(array(
        'prlId'=>$data['prlId'],
		'prlName'=>$data['prlName'],
		'prlLastName'=>$data['prlLastName'],
		'prlPhone'=>$data['prlPhone'],
		'prlEmail'=>$data['prlEmail'],
		'prlPassword'=>$data['prlPassword'],
		'prlYetki'=>$data['prlYetki'],
		'prlUserYetki' => $data['prlUserYetki'],
		'prlIsEnabled' => $data['prlIsEnabled'],
		'prlCreatedAt'=>$data['prlCreatedAt']
    ));
    if ($insert) {
        echo json_encode("ok");

    } else {
        echo json_encode("error");
    }
} else if ($method == "PUT") {
    $query = $db->prepare("UPDATE personel SET
		prlName=:prlName,
		prlLastName=:prlLastName,
		prlPhone=:prlPhone,
		prlEmail=:prlEmail,
		prlPassword=:prlPassword,
		prlYetki=:prlYetki,
		prlUserYetki=:prlUserYetki,
		prlIsEnabled=:prlIsEnabled,
		prlCreatedAt=:prlCreatedAt
		WHERE prlId=:prlId
	");
    $update=$query->execute(array(
		'prlName'=>$data['prlName'],
		'prlLastName'=>$data['prlLastName'],
		'prlPhone'=>$data['prlPhone'],
		'prlEmail'=>$data['prlEmail'],
		'prlPassword'=>$data['prlPassword'],
		'prlYetki'=>$data['prlYetki'],
		'prlUserYetki' => $data['prlUserYetki'],
		'prlIsEnabled' => $data['prlIsEnabled'],
		'prlCreatedAt'=>$data['prlCreatedAt'],
		'prlId'=>$data['prlId']
    ));
    if ($update) {
        echo json_encode("ok");

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

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