<?php
session_start();
include('../../globals/error-reporting.php');
global $ROOT, $PATH, $phpself;
$ROOT = $_SERVER['DOCUMENT_ROOT'];
$PATH = '../../';  
$phpself = basename(__FILE__);
require_once($PATH  .'globals/globals.php');
$admin = 'project';
$updateString = ''; 
$udID = ''; 

$page = (isset($_POST['page']) ? $_POST['page'] : '');
$section = (isset($_POST['section']) ? $_POST['section'] : '');
$variables = getVariables('project',$page);

if (!empty($_POST['op'])) {
	$op = $_POST['op'];
	
	if ($op == 'delete') {
		$editID = $_POST['editID'];
		$query = 'UPDATE projects SET projectDisp=0 WHERE projectID='. $editID;
		$result = $con->query($query);
	} else {
		$fieldString = '';
		$valueString = '';
		foreach($_POST as $key => $value) {
			if (in_array($key,$variables['excludeArray'])) {
				// Do Not Include in query
			} elseif ($key == 'estID') {
				$fieldString .= $key .',';
				$valueString .= '"'. $value .'",';
				if (!empty($value))
					$projectTimeEstimate = getEstimateTotal($value,$con);
				else 
					$projectTimeEstimate = $_POST['projectTimeEstimate'];
				$fieldString .= 'projectTimeEstimate,';
				$valueString .= '"'. $projectTimeEstimate .'",';
			} elseif ($key == 'op') {
				if ($op == "delete") {
					$fieldString .= 'projectDisp,';
					$valueString .= '"0",';
				} else  {
					if ($op == "react") {
						$fieldString .= 'projectDisp,';
						$valueString .= '"1",';
					} else {
						$fieldString .= 'projectDisp,';
						$valueString .= '"'. $_POST['projectDisp'] .'",';
					}
				}
			} else {
				if ($key == 'currentRateID') {
					$fieldString .= $key .',';
					$valueString .= '"'. $value .'",';
					$existRate = $_POST['existRate'];
					$rateHistExist = $_POST['rateHist']; 
					if (!empty($rateHistExist))
						$rateHistExist = explode(",",$rateHistExist);
					else 
						$rateHistExist = array();
					
					$rateHist = '';  
					if ($value != $existRate) {
						$rateHistExist[] = $value;
						$rateHist = implode(',',array_filter($rateHistExist));
						$fieldString .= '	rateHistory,';
						$valueString .= '"'. $rateHist .'",';		
					}
				} else {
					if ($key == 'editID') {
						$key = 'projectID';
						$udID = $value;
					}
					elseif ($key == 'compID')
						$key = 'clientID';
					$fieldString .= $key .',';
					$valueString .= '"'. $value .'",';
				}
			}
		}
		// Remove Last comma from string to avoid mySQL errors
		$fieldString = substr($fieldString,0,strlen($fieldString)-1);
		$valueString = substr($valueString,0,strlen($valueString)-1);
		
		$query = 'REPLACE INTO projects ('. $fieldString .') VALUES ('. $valueString .')';
		//echo $query;
		$result = $con->query($query);
		
		//echo '<br>'. $_POST['op']; 
		$editID = (!empty($udID) ? $udID : $con->insert_id); 
	}
	
	if ($op != 'delete') {
		if (($page == 'dash')  && ((!isset($_POST['projectCurrent']) || empty($_POST['projectCurrent'])))) {
			$op = 'delete';
		} else {	
			$updateInfo = ''; 
			if (($_POST['op'] == 'add') || ($_POST['op'] == 'edit')) {
				foreach($variables['displayArray'] as $value) {
					if ($value == 'projectID')
						$$value = $editID;
					elseif ($value == 'clientID')
						$$value = $_POST['compID'];	
					else
						$$value =  $_POST[$value];
					//echo $value .' = '. $$value .'<br>';
				}
				$client = getClientName($clientID,$con);	
				if ( (!empty($projectTargetComp)) || ($projectTargetComp != '0000-00-00') )	
					$projDue = Date::convert($projectTargetComp, 'Y-m-d', 'n/d/Y');
				else 
					$projDue = '--/--/----';
				if ($page == 'dash') {
					$projectsCurrent = ''; 
					include('../../pages/dashboard/inc/list-item/project.php');
					$updateString .= $projectsCurrent;
				} elseif ($page == 'project') {
					$projectsCurrent = ''; 
					if ($op == 'add')
						$projectID = $editID;
					include('../list/list-item/project.php');
					$updateString .= $listItem;
				}
			}
		}
		
		$existEstID = $_REQUEST['existEstimateID'];
		if (!empty($estID)) {
			$query = 'UPDATE estimates SET projectID='. $_REQUEST['editID'] .' WHERE estID='. $estID;
			$result = $con->query($query);
	
			if ( (!empty($existEstID)) && ($existEstID != $estID) ) {
				$query = 'UPDATE estimates SET projectID="" WHERE estID='. $existEstID;
				$result = $con->query($query);
			}
		} else {
			if (!empty($existEstID)) {
				$query = 'UPDATE estimates SET projectID="" WHERE estID='. $existEstID;
				$result = $con->query($query);
			}
		}
		//$updateID = (($page == 'dash') ? 'project-'. $editID : 'record-'. $editID);	
	}
	$updateID = (($page == 'dash') ? 'project-'. $editID : 'record-'. $editID);
	$output = (($result) ? array('update'=>$updateID,'updateOP'=>$op,'updateString'=>$updateString,'page'=>$page,'section'=>$section) : 'UPDATE ERROR');
} else {
	$output = 'ERROR 2'; 	
}

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

?>