<?php

if(stripos($_SERVER['SCRIPT_NAME'],"/admin/")===false?false:true){
	include_once("../connect.php");
	include_once("../session.php");
}else{
	include_once("connect.php");
	include_once("session.php");
}

$function = $_REQUEST['f'];
if($function == "getBillboardData"){
    $id = $_REQUEST['id'];
	getBillboardData($id);
}elseif($function == "saveBillboardData"){
   saveBillboardData($_REQUEST);
}elseif($function == "saveBillboardPosition"){
   saveBillboardPosition($_REQUEST);
}elseif($function == "getNavbarBillboardsCode"){
	getNavbarBillboardsCode();
}elseif($function == "deleteBillboard"){
	deleteBillboard($_REQUEST['bid']);
}
function getBillboardData($id){
    global $con;
	$query = "SELECT * FROM BILLBOARD WHERE ID = $id";
	
	$result = mysqli_query($con, $query);
	//var_dump($result);
	if($result){
		//echo $query;
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
		
		$row['StartDate'] = date("m/d/Y h:m A", strtotime($row['StartDate']));
		$row['EndDate'] = date("m/d/Y h:m A", strtotime($row['EndDate']));
		if($row['Content'] == "."){
			$row['Content'] = "./img/no_image_available.png";
		}
		echo json_encode($row);
	}else{
		echo "false";
		
	}
}
function saveBillboardPosition($data){
	global $con;
	$query = "UPDATE BILLBOARD SET Position = 0 WHERE ID = " . $data['obid'] . ";\n";
	$query .= "UPDATE BILLBOARD SET Position = " . $data['pos'] . " WHERE ID = " . $data['bid'] . ";";
	
	
	if(mysqli_multi_query($con, $query)){
		echo 'true';
	}else{
		echo 'false';
	}
}
function deleteBillboard($bid){
	global $con;
	$query = "DELETE FROM BILLBOARD WHERE ID = " . $bid . ";";
	ob_start();
	getBillboardData($bid);
	$billboardData = ob_get_contents();
	ob_end_clean();
	$billboardData = json_decode($billboardData);
	if(mysqli_query($con, $query)){
		
		event_log($_SESSION['Username'], 'Delete Billboard', "Billboard Data: \n" . print_r($billboardData, true));
		echo "true";
	}else{
		echo mysqli_error($con);
	}
}
function saveBillboardData($data){
	global $con;
	$return = array();
	
	if($data['IsNew'] == "0"){
		$query = "UPDATE BILLBOARD SET `Title` = '" . $data['Title'] . "', `Type` = '" . $data['Type'] . "', `Text` = '" . $data['BillboardText'] . "', `Link` = '" . $data['BillboardLink'] . "', `Content` = '" . $data['Content'] . "', `Button` = '" . $data['Button'] . "', `StartDate` = '" . date("Y-m-d H:i",strtotime($data['StartDate'])) . "', `EndDate` = '" . date("Y-m-d H:i",strtotime($data['EndDate'])) . "', `Active` = " . $data['Active'] . " WHERE ID = " . $data['BillboardID'];
	}else{
		$query = "INSERT INTO BILLBOARD (`Title`, `Type`, `Content`, `Text`, `Link`, `Button`, `StartDate`, `EndDate`, `Active`) VALUES ('" . $data['Title'] . "', '" . $data['Type'] . "', '" . $data['Content'] . "', '" . $data['BillboardText'] . "', '" . $data['BillboardLink'] . "', '" . $data['Button'] . "', '" . date("Y-m-d H:i",strtotime($data['StartDate'])) . "', '" . date("Y-m-d H:i",strtotime($data['EndDate'])) . "', " . $data['Active'] . ");";
	}
	//var_dump(mysqli_f($con, $query));
	
	if(mysqli_query($con, $query)){
		event_log($_SESSION['Username'], 'Add/Edit Billboard', "Billboard Data: \n" . print_r($data, true));
		$return['result'] = true;
	}else{
		$return['result'] = false;
		$return['error'] = mysqli_error($con);
		$return['query']= $query;
	}
	echo json_encode($return);
	return $return;
	
}
function getActiveBillboards($return = false){
	global $con;
	$billboards = array();
	$query = "SELECT ID FROM BILLBOARD WHERE Active = True;";
	$result = mysqli_query($con, $query);
	echo $query;
	while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
		array_push($billboards, new Billboard($row['ID']));
	}
	if($return){
		echo json_encode($billboards);
	}
	return  $billboards;
}
function getNavbarBillboardsCode(){
	global $con;
	$query = "SELECT * FROM BILLBOARD WHERE Position != 0 LIMIT 4";
	
	$result = mysqli_query($con, $query);
	$positions[3] = '450px';
	$positions[2] = '630px';
	$positions[1] = '800px';
	while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
		$billboard = new Billboard($row['ID']);
		$fromDate = new DateTime($billboard->StartDate, new DateTimeZone("America/Puerto_Rico"));
		$toDate = new DateTime($billboard->EndDate, new DateTimeZone("America/Puerto_Rico"));
		$accountNumber = isset($_SESSION['AccountNumber'])?$_SESSION['AccountNumber']:"";
		if(runBetweenDateRange($fromDate, $toDate)  && $accountNumber != "empleado"){
			if($row['Position'] == -1){
				if(isset($_REQUEST['splash']) && $_REQUEST['splash'] == "true"){
					echo $billboard->getHTML() . "\n";	
				}
				
			}else{
				echo '<li><a href="#"><img id="PromoButton' . $row['Position'] . '" class="grow" onclick="showNewPromo(' . $row['ID'] . ')" style="height: 70px; margin-top: 0px; position: absolute; right: ' . $positions[$row['Position']] . ';" src="' . $billboard->Button . '" alt="" /></a></li>' . "\n";
				echo $billboard->getHTML() . "\n";
				echo "\n";	
			}
			
		}
		
	}
	
	
}
?>