<?php

session_start();
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
global $ROOT, $PATH, $phpself;
$ROOT = $_SERVER['DOCUMENT_ROOT'];
$PATH = '../../';  
$phpself = basename(__FILE__);
require_once($PATH  .'globals/globals.php');
$admin = 'task-time';
$updateString = ''; 
$udID = ''; 
$page = $_POST['page'];

if (!empty($_POST['op'])) {
	
	$op = $_POST['op'];
	$insertFields = '';
	$insertValues = '';
	
	foreach($_POST as $key => $value) {
		if (($key == 'op')||($key == 'submit')||($key == 'adminType')||(substr($key,0,5) == "time_")||(substr($key,0,4) == "dat_")||(substr($key,0,5) == "desc_")||(substr($key, 0, 4) == "inv_")||(substr($key, 0, 6) == "ppInv_")||(substr($key, 0, 7) == "ppPaid_")||(substr($key, 0, 5) == "bill_")||($key == 'currentStatus')||($key == 'currentPriority')||($key == 'currentProgress')||($key == 'page')) {
			// Do not include in update string
		} elseif (($key == 'taskStatus')||($key == 'priority')||($key == 'taskProgress')) {
			if (($key == 'taskStatus') && ($value != $_POST['currentStatus'])) {
				$taskUpdate['taskStatus'] = $value; 
				$tdisp = checkTaskDisplay($value,$con);
				if ($tdisp == 0) {
					$udID = $_POST['taskID']; 
					$op = 'delete';
				}
			} elseif (($key == 'priority') && ($value != $_POST['currentPriority'])) 
				$taskUpdate['taskStatus'] = $value; 
			elseif (($key == 'taskProgress') && ($value != $_POST['currentProgress'])) 
				$taskUpdate['taskProgress'] = $value; 
		} elseif (substr($key,0,7) == "delete_") {
			$query = 'UPDATE projectTime SET timeDisp=0 WHERE timeID=' . $updateID;
			$result = $con->query($query);
			//echo '<p>'. $query . '</p>';
		} elseif ( (substr($key,0,11) == "datCurrent_")||(substr($key, 0, 12) == "timeCurrent_" )||(substr($key, 0, 12) == "descCurrent_")||(substr($key,0,12) == "billCurrent_")||(substr($key,0,11) == "invCurrent_")||(substr($key,0,13) == "ppInvCurrent_")||(substr($key,0,14) == "ppPaidCurrent_") ) {
			
			$updateArray = explode('_',$key);
			list($var,$updateID) = $updateArray;
			$query = '';
			
			if (substr($key, 0, 11) == "datCurrent_") {
				$newVar = 'dat_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = '';
				if ($newVal != $value) 
					$query = 'UPDATE projectTime SET timeDate="'. $newVal .'" WHERE timeID=' . $updateID;
			} elseif (substr($key, 0, 12) == "timeCurrent_") {
				$newVar = 'time_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = '';
				if ($newVal != $value) 
					$query = 'UPDATE projectTime SET timeAmount='. $newVal .' WHERE timeID=' . $updateID;
			} elseif (substr($key, 0, 12) == "descCurrent_") {
				$newVar = 'desc_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = '';
				if ($newVal != $value) 
					$query = 'UPDATE projectTime SET timeDesc="'. $newVal .'" WHERE timeID=' . $updateID;
			} elseif (substr($key, 0, 12) == "billCurrent_") {
				$newVar = 'bill_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = 0;
				if ($newVal != $value) 
					$query = 'UPDATE projectTime SET billable='. $newVal .' WHERE timeID=' . $updateID;
			} elseif (substr($key, 0, 11) == "invCurrent_") {
				$newVar = 'inv_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = 0;
				if ($newVal != $value) 
					$query = 'UPDATE projectTime SET timeInvoiced='. $newVal .' WHERE timeID=' . $updateID;
			} elseif (substr($key, 0, 13) == "ppInvCurrent_") {
				$newVar = 'ppInv_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = 0;
				if ($newVal != $value) 
					$query = 'UPDATE projectTime SET ppInv='. $newVal .' WHERE timeID=' . $updateID;
			} elseif (substr($key, 0, 14) == "ppPaidCurrent_") {
				$newVar = 'ppPaid_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = 0;
				if ($newVal != $value) 
					$query = 'UPDATE projectTime SET ppPaid='. $newVal .' WHERE timeID=' . $updateID;
			}
			
			if (!empty($query)) {
				$result = $con->query($query);
				//echo '<p>'. $query . '</p>';
			}
		} else {
			$insertFields .= $key .',';
			$insertValues .= '"' . $value .'",';
		}
	}
	
	// Update Task 
	if ($taskUpdate) {
		$query = "UPDATE tasks SET "; 
		foreach ($taskUpdate as $key => $value) {
			$query .= $key ."='". $value ."',";
		}
		$query = substr($query, 0, strlen($query)-1);
		$query .= " WHERE taskID=". $_POST['taskID'];
		//echo $query;
		$taskResult = $con->query($query);
	}
	
	// Remove Last comma from string to avoid mySQL errors
	$insertFields = substr($insertFields, 0, strlen($insertFields)-1);
	$insertValues = substr($insertValues, 0, strlen($insertValues)-1);
	
	$insertTest = str_replace('"','',$insertValues);
	$insertTest = str_replace(',','',$insertTest);
	//echo $insertTest;
	
	if (!is_numeric($insertTest)) {
		$query = 'INSERT INTO projectTime ('. $insertFields .') VALUES (' . $insertValues . ')';
		//echo $query; 
		$timeResult = $con->query($query);
	}
	
	$updateID = (($page == 'dash') ? 'task-'. $udID : 'record-'. $udID);
	$output = (($taskResult || $timeResult) ? array('update'=>$updateID,'updateOP'=>$op,'updateString'=>$updateString) : 'UPDATE ERROR');
} else {
	$output = 'ERROR'; 	
}

if ($con) $con->close();
header('Content-Type: application/json; charset=utf-8', true); 
echo json_encode(array("output"=>$output));

?>