<?php
session_start();
require_once '../utils/errorhandler.php';
require_once '../utils/response.php';
require_once '../model/dbclass.php';
require_once '../model/model.php';
require_once '../model/organisationinfo.php';

if ($_SESSION['wuseremail'] === null || !isset($_SESSION['wuseremail'])) {
     exit(badRequest(204,'Invalid session data. Proceed to login'));
}

$framework = new OrganisationInfo();
$id = (isset($_POST["id"]) && $_POST["id"] > 0) ? $_POST["id"] : 0;

$role = "";
if(isset($_SESSION["role"])){
    $role = $_SESSION["role"];
}
$email = "%";
if(isset($_SESSION['wuseremail'])){
    $email = $_SESSION['wuseremail'];
}
if($id > 0){
    $results = $framework->retrieveByQuerySelector("SELECT * FROM organisation WHERE id = $id");
}else{
    $results = $framework->retrieveByQuerySelector("SELECT * FROM organisation ORDER BY organisationname ASC");
}   
//exit("Line break");
if($results){
    $dataarray = array();
    foreach($results as $result){
        $dataarray[] = array(
            "id" => $result["id"],
            "organisationname" => html_entity_decode($result["organisationname"],ENT_QUOTES),
            "phone" => html_entity_decode($result["phone"],ENT_QUOTES),
            "address" => html_entity_decode($result["address"],ENT_QUOTES),
            "email" => html_entity_decode($result["email"],ENT_QUOTES),
            "state" => html_entity_decode($result["state"],ENT_QUOTES),
            "country" => html_entity_decode($result["country"],ENT_QUOTES),
            "status" => $result["status"],
            "logo" => $result["logo"],
            "tlog" => $result["tlog"]
            );
            
        break;
    }
    echo success($dataarray,200, "Successful","Successful");        
}else{
	exit(badRequest(204,'Not successful. No data'));
}    


?>