<?php


function clean($string) {
   $string = str_replace('', '-', $string); // Replaces all spaces with hyphens.
   
      $string = str_replace('?', '-', $string); // Replaces all spaces with hyphens.

   return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}


function getjobs() {

	$output = "";

	$output = $output . "[";

	$day_count = 1;

	include("configi.php");

	$outputarray = array();



//$outlet_id = 1;

$outlet_id =  1;

//	$job_compid = 1;

	$count = 0;

	$query=	"SELECT sms_last_reply, sms_rem_sent, full_name, client.uid_client, st.booking_alert, st.st_id, st.sms_last_reply_actioned as sms_last_reply_actioned, staff.uid_staff, st.booking_date, sts.time_from, sts.sts_booktype_colour, sts.sts_id as sts_id, sts.time_to, sts.sales_person_id, st.st_outlet_id, sts.booking_type AS displaytext, st.rem_flag, st.st_id, st.status, client.uid_client, client.comp_name, client.last_name, staff.staff_alloc_num, sts.booking_type, client.uid_client AS client_id, sts.booking_type, sts.booking_type, booktype.book_colour, first_name  AS displaytext2, client.total_paid_bookings, client.alert
FROM (" . $_COOKIE['phxcrmclient_id'] . "_st as st LEFT JOIN " . $_COOKIE['phxcrmclient_id'] . "_client as client ON st.uid_client = client.uid_client) LEFT JOIN ((" . $_COOKIE['phxcrmclient_id'] . "_sts as sts LEFT JOIN " . $_COOKIE['phxcrmclient_id'] . "_staff as staff ON sts.sales_person_id = staff.uid_staff) LEFT JOIN " . $_COOKIE['phxcrmclient_id'] . "_booktype as booktype ON sts.booking_type = booktype.book_type) ON st.st_id = sts.st_id
WHERE booking_date = '" . $_GET['start'] . "' and st.status<>'cancelled' and st.status<>'no show' and st.status<>'Discarded' and st.st_outlet_id = " . $outlet_id . " ORDER BY sts.sales_person_id, sts.time_from, sts.st_id;";
		
//echo $query;

$result=mysqli_query($conn, $query);

$num_rows = mysqli_num_rows($result);
$i = 1;

 while($row=mysqli_fetch_array($result)) {
 

 $sclient = $row['uid_client'];

 
 
 $queryclient="select * from " . $_COOKIE['phxcrmclient_id'] . "_client where uid_client = " . $sclient;
//echo $queryclient;
 $result2=mysqli_query($conn, $queryclient);


 while($row2=mysqli_fetch_array($result2)) {
 $scomp_name = $row2['comp_name'];
 }
$linkcol = "Red";

$bgtabcolor = "gray";

$textcolor = "black";

  if ($row['status'] == "Pencil"){ 
  
	if ($row['sms_rem_sent'] === NULL){
		$bgtabcolor = "#FF6600";
		$linkcol = "Orange";
	}else{
		if ($row['sms_last_reply'] === NULL){
			$bgtabcolor = "darkblue";
			$textcolor = "white";
		}else{
			$bgtabcolor = "red";
			$textcolor = "white";
		
		}
	}

}elseif ($row['status'] == "Arrived"){ 
		$bgtabcolor = "violet";	
		$linkcol = "violet";		
 }elseif ($row['status'] == "Confirmed"){  
 		$bgtabcolor = "limegreen";
		$linkcol = "limegreen";
 }elseif ($row['status'] == "Fully Paid"){  
		$bgtabcolor = "darkgreen";
		$linkcol = "darkgreen";
 }elseif ($row['status'] == "Paid"){  
		$bgtabcolor = "darkgreen";
		$linkcol = "darkgreen";
$textcolor = "white";
		}else{
  		$linkcol = "black";
 }
 

	
//	$job_descr = $row['job_descr'];

	//	$job_descr_html = $job_descr;	
	//	$job_descr_html = htmlspecialchars($job_descr, ENT_NOQUOTES);
		
	//	$job_descr_html = preg_replace('/[^a-zA-Z0-9_%\[().\]\\/-]/s', ' ', $job_descr);



//old
	//	$job_descr_html = str_replace("+"," ",$row['job_descr']);
		//		$job_descr_html = str_replace(","," ",$job_descr_html);
			//	$job_descr_html = str_replace('"',' ',$job_descr_html);
				//				$job_descr_html = str_replace(';',' ',$job_descr_html);
					//										$job_descr_html = str_replace('#',' ',$job_descr_html);
	//	substr($job_descr,0,5)
	//	$job_descr_html = clean("hello");
	///	$job_descr_html = clean($job_descr);
		
	//	echo $job_descr;
	
	$booking_type = $row['booking_type'];
	$booking_date = $row['booking_date'];

	
	$sel_color = "Red";

//	$ltitle 				= "dave" . "(" . $row['st_id'] . ") " . $scomp_name;
//echo $ltitle;
		$lurl 					= "javascript: showbooking(" . $row['st_id'] . ")";
	//$lurl = "dave";
//	$lstart_date			= $row['job_date_start'];
	//$lend_date				= $row['job_date_end'];
	
	
	$booking_date = $row['booking_date'];

	$staff_alloc_num = $row["staff_alloc_num"];
	$resource = "resource" . $staff_alloc_num;
	$time_from = $row["time_from"];
	$time_to = $row["time_to"];	
	$displaytext = $row["comp_name"] . " " . $row["booking_type"] . " " . $row["rem_flag"];
	$status = $row["status"];
	$sales_person_id = $row["sales_person_id"];
	$booking_type = $row["booking_type"];
	$book_colour = $row["sts_booktype_colour"];
	$sts_booktype_colour = $row["sts_booktype_colour"];
	$displaytext2 = $row["displaytext2"];
	$total_paid_bookings = $row["total_paid_bookings"];
	$alert = "dave" . $row["alert"];
	$st_outlet_id = $row["st_outlet_id"];
	$rem_flag = $row["rem_flag"];
	$staff_id = $row["uid_staff"];
	$uid_staff = $row["uid_staff"];
	$st_id = $row["st_id"];
	$sms_last_reply_actioned = $row["sms_last_reply_actioned"];
	$sts_id = $row["sts_id"];
	$full_name = $row["full_name"];
	$displaytext = trim($row["displaytext"]);
	$booking_alert = $row["booking_alert"];
	
	$ltitle 				= trim("(" . $row['st_id'] . ") " . $full_name . " " . $displaytext);
//	$lurl 					= "";//"javascript: showjobdetails(" . $row['st_id'] . ")";
//	$lstart_date			= $row['booking_date'];
//	$lend_date				= $row['booking_date'];
	
	$lstart_date = "2015-01-26T09:00:00+00:00";
		$lend_date = "2015-01-26T10:15:00+00:00";
		
		$lstart_date = $booking_date . "T" . $time_from . "+00:00";
		$lend_date = $booking_date . "T" . $time_to . "+00:00";
			$job_descr_html = "";	
			
			
			$booking_alert_len = strlen($booking_alert);

			$alert_flag = "no";
			
				if ($booking_alert_len > 1){
					 $alert_flag = "yes";
					 $bordercolor = "red";
				}else{
					 $bordercolor = "";

				}

			$job_descr_html = $full_name . " Event Name: hello22";
	//	 $bgtabcolor = "red";
	//	 $linkcol = "green";
		 $resource = "resource".$staff_alloc_num;
		 $allday = false;
		 

$btcolor = "white";

 $querybtc="select * from " . $_COOKIE['phxcrmclient_id'] . "_booktype where book_type = '" . $booking_type . "'";

// echo $querybtc;
 
//echo $queryclient;
 $resultbtc=mysqli_query($conn, $querybtc);
 while($rowbtc=mysqli_fetch_array($resultbtc)) {
	 $btcolor = $rowbtc['book_colour'];
 }
//$btcolor = "Pink";
	
	$outputarray[] = array('resources' => $resource, 'title' => $ltitle, 'start' => $lstart_date, 'end' => $lend_date, 'description' => $job_descr_html, 'color' => $bgtabcolor, 'st_id' => $row['st_id'], 'url' => $lurl, 'borderColor' => $linkcol, 'sales_id' => $row['st_id'], 'textColor' => $textcolor, 'sts_id' => $sts_id, 'borderColor' => $bordercolor, 'btcolor' => $btcolor, 'alert_flag' => $alert_flag);

//	$outputarray[] = array(utf8_encode('title') => utf8_encode($ltitle), utf8_encode('start') => utf8_encode($booking_date), utf8_encode('end') => utf8_encode($booking_date), utf8_encode('description') => utf8_encode($booking_type), 'color' => utf8_encode($linkcol), 'st_id' => utf8_encode($row['st_id']), 'url' => utf8_encode($lurl),'staff_alloc_num' => utf8_encode($staff_alloc_num),	'time_from' => utf8_encode($time_from), 'time_to' => utf8_encode($time_to), 'status' => utf8_encode($status), 'sales_person_id' => utf8_encode($sales_person_id), 'booking_type' => utf8_encode($booking_type), 'book_colour' => utf8_encode($sts_booktype_colour),'displaytext2' => utf8_encode($displaytext2), 'total_paid_bookings' => utf8_encode($total_paid_bookings), 'alert' => utf8_encode($alert), 'st_outlet_id' => utf8_encode($st_outlet_id), 'rem_flag' => utf8_encode($rem_flag), 'staff_id' => utf8_encode($uid_staff), 'st_id' => utf8_encode($st_id), 'sms_last_reply_actioned' => utf8_encode($sms_last_reply_actioned), 'sts_id' => utf8_encode($sts_id), 'id' => utf8_encode($sts_id), 'resources' => utf8_encode($resource), 'allDay' => 'false');
 //$outputarray[] = array(utf8_encode('title') => 'hello', utf8_encode('start') => utf8_encode($booking_date), utf8_encode('end') => utf8_encode($booking_date)	);
	
	$i = $i + 1;
	
	}


//	print_r($outputarray);
	
//	$encodedArray = array_map(utf8_encode, $outputarray);
	
		
//		$encodedArray = array_map(utf8_encode, $outputarray);
$output2 = json_encode($outputarray);
	
//	$output2 = $outputarray;
	
//	echo "output2".$output2;
	
	return $output2;

	
}

//--------------------------------------------------------------------------------------------------
// This script reads event data from a JSON file and outputs those events which are within the range
// supplied by the "start" and "end" GET parameters.
//
// An optional "timezone" GET parameter will force all ISO8601 date stings to a given timezone.
//
// Requires PHP 5.2.0 or higher.
//--------------------------------------------------------------------------------------------------

// Require our Event class and datetime utilities
require dirname(__FILE__) . '/utils.php';

// Short-circuit if the client did not give us a date range.
if (!isset($_GET['start']) || !isset($_GET['end'])) {
	die("Please provide a date range.");
}

// Parse the start/end parameters.
// These are assumed to be ISO8601 strings with no time nor timezone, like "2013-12-29".
// Since no timezone will be present, they will parsed as UTC.
$range_start = parseDateTime($_GET['start']);
$range_end = parseDateTime($_GET['end']);

// Parse the timezone parameter if it is present.
$timezone = null;
if (isset($_GET['timezone'])) {
	$timezone = new DateTimeZone($_GET['timezone']);
}

// Read and parse our events JSON file into an array of event data arrays.
//$json = file_get_contents(dirname(__FILE__) . '/../json/events.json');

$json = getjobs();

//echo $json;

$input_arrays = json_decode($json, true);

//print_r($input_arrays);
// Accumulate an output array of event data arrays.
$output_arrays = array();

foreach ($input_arrays as $array) {

	// Convert the input array into a useful Event object
	$event = new Event($array, $timezone);

	// If the event is in-bounds, add it to the output
	if ($event->isWithinDayRange($range_start, $range_end)) {
		$output_arrays[] = $event->toArray();
	}else{
		$output_arrays[] = $event->toArray();
		
	}
}

//print_r($output_arrays);

// Send JSON to the client.
echo trim(json_encode($input_arrays));
//echo  json_last_error();

//echo '[{"resources":"resource2","title":"(44464) off","start":"2015-01-26T09:15:00+00:00","end":"2015-01-26T10:15:00+00:00"}]';