<?php
session_start();
global $ROOT, $PATH, $phpself;
$ROOT = $_SERVER['DOCUMENT_ROOT'];
$PATH = '../../';  
$phpself = basename(__FILE__);
require_once($PATH  .'globals/globals.php');
$adminAct = $_REQUEST['adminType']; 
$admin = 'estimate';
$updateString = ''; 
$udID = ''; 

$fieldArray = array('estID','estName','compID','projectID','estDate','estDesc','estProjType','estTech','estHaveHost','estHostInfo','estDesignDesc','estDesign','estProd','estProdDesc','estCodeDesc','estCode','estCMSdesc','estCMS','estDBdesc','estDB','estLogoDesc','estLogo','estGraphicDesc','estGraphic','estSEOdesc','estSEO','estMisc1Desc','estMisc1','estMisc2Desc','estMisc2','estPagesAmt','estPagesTime','estPagesTotal','estPagesDesc','estCosts','estCostsDesc','estRate','estTotalHours','estTotalCost','estUpdate','estStatus','estSubmitDate','estAcceptDate','estDeclineDate','estDisplay');

if (!empty($_POST['op'])) {

	$op = $_POST['op'];
	//$content = '<h2>'. $_POST['estName'] .' has been Successfully';
	
	
	if ($op == 'delete') {
		$editID = $_POST['editID'];
		$query = 'UPDATE estimates SET estDisplay=0 WHERE estID='. $editID;
		$result = $con->query($query);
	} else {
	
		$fieldString = '';
		$valueString = '';
		foreach($_POST as $key => $value) {
			if (($key == 'submit')||($key == 'estDisplay')||($key=='existProjID')||($key == 'estTotalHours')||($key == 'estTotalCosts')||($key == 'existProjID')||($key=='adminType')) {
				// Do Not Include in query
			}
			elseif ($key == 'op') {
				if ($op == "delete") {
					$fieldString .= 'estDisplay,';
					$valueString .= '"0",';
				} else  {
					if ($op == "react") {
						$fieldString .= 'estDisplay,';
						$valueString .= '"1",';
					} else {
						$fieldString .= 'estDisplay,';
						$valueString .= "'". $_POST['estDisplay'] ."',";
					}
				}
			} else {
				if ($key == 'editID')
					$key = 'estID';
				elseif ($key == 'compID')
					$key = 'clientID';
				$fieldString .= $key .',';
				$valueString .= '"'. $value .'",';
			}
		}
		$estTotalHours = $_POST['estDesign'] + $_POST['estProd'] + $_POST['estCode'] + $_POST['estCMS'] + $_POST['estDB'] + $_POST['estLogo'] + $_POST['estGraphic'] + $_POST['estSEO'] + $_POST['estMisc1'] + $_POST['estMisc2'] + $_POST['estPagesTotal'];
		
		$estTotalCost = $estTotalHours * $_POST['estRate'] + $_POST['estCosts'];
		
		$fieldString .= 'estTotalHours, estTotalCost';
		$valueString .= "'". $estTotalHours ."','". $estTotalCost ."'";
		
		$query = 'REPLACE INTO estimates ('. $fieldString .') VALUES ('. $valueString .')';
		$result = $con->query($query);
		$editID = (!empty($udID) ? $udID : $con->insert_id); 
		
		$existProjectID = $_REQUEST['existProjID'];
		$estID = $_REQUEST['editID'];
		if (!empty($projectID)) {
			$query = "UPDATE projects SET estID=". $estID .", projectTimeEstimate='". $estTotalHours ."' WHERE projectID=". $projectID;
			$result = $con->query($query);
			
			if ( (!empty($existProjectID)) && ($existProjectID != $projectID) ) {
				$query = "UPDATE projects SET estID='', projectTimeEstimate='' WHERE projectID=". $existProjectID;
				$result = $con->query($query);
			}
			
		} else {
			if (!empty($existProjectID)) {		 
				$query = "UPDATE projects SET estID='', projectTimeEstimate='' WHERE projectID=". $existProjectID;
				$result = $con->query($query);
			}
		}
	}
	$updateID = (($page == 'dash') ? 'estimate-'. $editID : 'record-'. $editID);
	$output = (($result) ? array('update'=>$updateID,'updateOP'=>$op,'updateString'=>$updateString) : '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));

?>