<?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';

date_default_timezone_set('Africa/Lagos');

if ($_SESSION['wuseremail'] === null || !isset($_SESSION['wuseremail'])) {
    $useremail = filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) ? $_POST["email"] : "BAD";
    if($useremail === "BAD"){
        exit(badRequest(204,'Bad email'));
    }
//     exit(badRequest(204,'Invalid session data. Proceed to login'));
}else{
    $useremail = $_SESSION['wuseremail'];
}

if($_FILES["logo"]["name"] !== "" && $_FILES["logo"]["name"] != null && $_FILES["logo"]["name"] !== "-"){
	$filenamearray = explode(".",$_FILES["logo"]["name"]); 
	$filename1 = $filenamearray[0] . mt_rand(10,111111111) . "." . end($filenamearray);
}else { $filename1 = "-"; }

$returnvalue = '';
if($_FILES["logo"]["name"] !== null && $_FILES["logo"]["name"] !== "-"){	
	$allowedExts = array("gif", "jpeg", "jpg","pdf","png");
	$temp = explode(".", $_FILES["logo"]["name"]);
	$returnvalue .= '' . $_FILES["logo"]["name"];
	$extension = end($temp);
	if ((($_FILES["logo"]["type"] === "image/gif") || ($_FILES["logo"]["type"] === "image/jpeg") || ($_FILES["logo"]["type"] === "image/jpg") || ($_FILES["logo"]["type"] === "image/pjpeg") || ($_FILES["logo"]["type"] === "image/png") || ($_FILES["logo"]["type"] === "application/pdf")) && in_array($extension, $allowedExts)){
		if ($_FILES["logo"]["error"] > 0){
			$returnvalue .= ' | error saving photo'; //echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
		}else{
			/*
			if (file_exists("../images/borrowers/" . $_FILES["userphotoname"]["name"])){
				$returnvalue .= ' | the file exists'; //echo $_FILES["userphoto"]["name"] . " already exists. ";
  		    }else{ */
				move_uploaded_file($_FILES["logo"]["tmp_name"],"../images/" . $filename1);
				$returnvalue .= "Upload Successful";
  		    //}
		}
	}else{
         $returnvalue .= ' | Invalid file type';	    
	}
}
//exit("File error: $returnvalue");

$organisationname = ((isset($_POST["organisationname"]) && $_POST["organisationname"] !== "") ? htmlentities($_POST["organisationname"],ENT_QUOTES) : "-" );
$email = ((isset($_POST["email"]) && $_POST["email"] !== "") ? htmlentities($_POST["email"],ENT_QUOTES) : "-" );

if($organisationname === "-" || $email === "-"){
    exit(badRequest(204, "Not successful. Invalid Organisation name or email"));
}
$framework = new OrganisationInfo();
//if(isset($_POST['id'])){
    $res = $framework->getOrganisationInfo("id > 0 ");
//}
if($res){
    $id = $res['id'];
    $query = "
        phone = '" . ((isset($_POST["phone"]) && $_POST["phone"] !== "") ? htmlentities($_POST["phone"],ENT_QUOTES) : $res['phone'] ) . "',
        
        email = '" . ((isset($_POST["email"]) && $_POST["email"] !== "") ? htmlentities($_POST["email"],ENT_QUOTES) : $res['email'] ) . "',
        organisationname = '" . ((isset($_POST["organisationname"]) && $_POST["organisationname"] !== "") ? htmlentities($_POST["organisationname"],ENT_QUOTES) : $res['organisationname'] ) . "',
        state = '" . ((isset($_POST["state"]) && $_POST["state"] !== "") ? htmlentities($_POST["state"],ENT_QUOTES) : $res['state'] ) . "',
        country = '" . ((isset($_POST["country"]) && $_POST["country"] !== "") ? htmlentities($_POST["country"],ENT_QUOTES) : $res['country'] ) . "',
        logo = '" . ((isset($filename1) && $filename1 !== "-") ? $filename1 : $res['logo'] ) . "',
        address = '" . ((isset($_POST["address"]) && $_POST["address"] !== "") ? htmlentities($_POST["address"],ENT_QUOTES) : $res['address'] ) . "'";

    $result = $framework->updateOrganisationDetails($query, $id);

    if ($result) {
        echo success($result, 200, "Successful", "Successful");
    }else {
        echo badRequest(204, "Not successful");
    }
    
}else{
    
    // fields names
    $fields = "logo,phone,address,email,organisationname,state,country,status,tlog,user";
    $values = "
    '$filename1',
    '" . ((isset($_POST["phone"]) && $_POST["phone"] !== "") ? htmlentities($_POST["phone"],ENT_QUOTES) : "-" ) . "',
    '" . ((isset($_POST["address"]) && $_POST["address"] !== "") ? htmlentities($_POST["address"],ENT_QUOTES) : "-" ) . "',
    '" . ((isset($_POST["email"]) && $_POST["email"] !== "") ? htmlentities($_POST["email"],ENT_QUOTES) : "-" ) . "',
    '" . ((isset($_POST["organisationname"]) && $_POST["organisationname"] !== "") ? htmlentities($_POST["organisationname"],ENT_QUOTES) : "-" ) . "',
    '" . ((isset($_POST["state"]) && $_POST["state"] !== "") ? htmlentities($_POST["state"],ENT_QUOTES) : "-" ) . "',
    '" . ((isset($_POST["country"]) && $_POST["country"] !== "") ? htmlentities($_POST["country"],ENT_QUOTES) : "-" ) . "',
    'OPEN',
    '" . $useremail . " | " . date("Y-m-d H:i:s") . "','$useremail'";

    $result = $framework->registerOrganisation($fields, $values);
    if($result){
        echo success($result, 200, "Successful", "Successful");
    }else{
        echo badRequest(204, "Not successful");
    }
}
