<?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/FrameWork.php';
date_default_timezone_set('Africa/Lagos');

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

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

$returnvalue = '';
if($_POST["photofilename"] !== null && $_POST["photofilename"] !== "-"){	
	$allowedExts = array("gif", "jpeg", "jpg","pdf");
	$temp = explode(".", $_FILES["userphotoname"]["name"]);
	$returnvalue .= '' . $_FILES["userphotoname"]["name"];
	$extension = end($temp);
	if ((($_FILES["userphotoname"]["type"] === "image/gif") || ($_FILES["userphotoname"]["type"] === "image/jpeg") || ($_FILES["userphotoname"]["type"] === "image/jpg") || ($_FILES["userphotoname"]["type"] === "image/pjpeg") || ($_FILES["userphotoname"]["type"] === "image/png") || ($_FILES["userphotoname"]["type"] === "application/pdf")) && in_array($extension, $allowedExts)){
		if ($_FILES["userphotoname"]["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["userphotoname"]["tmp_name"],"../images/" . $filename1);
				$returnvalue .= "Upload Successful";
  		    //}
		}
	}else{
         $returnvalue .= ' | Invalid file type';	    
	}
}

$framework = new FrameWork();
$res = $framework->logoInfo("id = " . intval($_POST['id']));

if($res){
    $id = $res['id'];
    $query = "
        logoname = '". ((isset($_POST["logoname"]) && $_POST["logoname"] !== "") ? htmlentities( $_POST["logoname"],ENT_QUOTES) : $res['logoname'])."',
        url = '" . (($filename1 !== '-') ? $filename1 : $res["url"]) . "',
        owner = '" . ((isset($_POST["owner"]) && $_POST["owner"] !== "") ? $_POST["owner"] : $res['owner'] ) . "',
        status = '" . ((isset($_POST["status"]) && $_POST["status"] !== "") ? $_POST["status"] : $res['status'] ) . "'";

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

    if ($result) {
        echo success($result, 200, "Successful", "Successful");
    }else {
        echo badRequest(204, "Not successful");
    }
    
}else{
    
    // fields names
    $fields = "logoname,url,owner,status,user,tlog";
    $values = "
    '" . (isset($_POST["logoname"]) && $_POST["logoname"] !== "" ? htmlentities( $_POST["logoname"],ENT_QUOTES) : "-") . "',
    '" . $filename1 . "',
    '" . ((isset($_POST["owner"]) && $_POST["owner"] !== "") ? $_POST["owner"] : "TRAVELS AND TOURS" ) . "',
    '" . ((isset($_POST["status"]) && $_POST["status"] !== "") ? $_POST["status"] : "DISPLAY" ) . "',
    '" . $_SESSION["elfuseremail"] . "'
    '" . $_SESSION["elfuseremail"] . " | " . date("Y-m-d H:i:s") . "'";

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