<?php

$admin = 'estimate'; 

include ($PATH .'globals/globals.php');

$adminAct = $_REQUEST['adminAct']; 

$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';
	
	$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 ."'";
	
	// 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 estimates ('. $fieldString .') VALUES ('. $valueString .')';
	$result = mysql_query ($query) or die('Invalid Estimate '. $op .' query: ' . mysql_error());
	
	$existProjectID = $_REQUEST['existProjID'];
	$estID = $_REQUEST['editID'];
	if (!empty($projectID)) {
		$query = "UPDATE projects SET estID=". $estID .", projectTimeEstimate='". $estTotalHours ."' WHERE projectID=". $projectID;
		//echo $query . '<br>';
		$result = mysql_query ($query) or die('Invalid Estimate '. $op .' query: ' . mysql_error());
		//exit;
		
		if ( (!empty($existProjectID)) && ($existProjectID != $projectID) ) {
			$query = "UPDATE projects SET estID='', projectTimeEstimate='' WHERE projectID=". $existProjectID;
			$result = mysql_query ($query) or die('Invalid Estimate '. $op .' query: ' . mysql_error());
			//echo $query . '<br>';
		}
		
	}
	else {
		if (!empty($existProjectID)) {		 
			$query = "UPDATE projects SET estID='', projectTimeEstimate='' WHERE projectID=". $existProjectID;
			$result = mysql_query ($query) or die('Invalid Estimate '. $op .' query: ' . mysql_error());
			//echo $query . '<br>';
		}
	}
	
	if ($result) {
		//header("Location: http://billing.tangiblelearning.com/admin/close.php");
		echo '<script type="text/javascript">
				parent.location.reload();
				parent.Shadowbox.close();
			</script>';
	}
}
else {
*/
if ($adminAct == 'new') {
	$op = 'add';
	$editInpID = '<input type="hidden" name="op" value="'. $op .'" />'; 
	foreach($fieldArray as $value) { 
		if ($value == 'estDisp')
			$$value = 1;
		elseif ($value == 'clientID')
			$client = ''; 
		else 	
			$$value = '';
	}
	$estDisplay = 1;
	$estDate = $today;
	$subBtn = 'Add Estimate'; 
	$modalTitle = 'Create New Estimate';
} else {
	$op = '';
	$editID = $_REQUEST['editID']; 
	$editInpID = '<input type="hidden" name="editID" value="'. $editID .'" />';
		
	$query = 'SELECT * FROM estimates WHERE estID=' . $editID; 
	$result = $con->query($query);
	
	while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
		$client = $row['clientID'];
		foreach($fieldArray as $value) 
			$$value =  $row[''. $value .''];
		if ($adminAct == 'react') {
			$op = 'react';
			$delConfirm = ""; 
			$subBtn = 'Reactivate Estimate';
			$modalTitle = 'Reactivate Estimate: '. $estName;
		} elseif ($adminAct == 'edit') {
			$op = 'edit';
			$delConfirm = ""; 
			$subBtn = 'Save Changes';
			$modalTitle = 'Edit Estimate: '. $estName;
		} elseif ($adminAct == 'delete') {
			$op = 'delete';
			$delConfirm = ' onSubmit="return sure()"';
			$subBtn = 'Delete Estimate';
			$modalTitle = 'Delete Estimate: '. $estName . '?';
		}
	}
}
$projFilter = (($op=='add') ? '1,4' : ''); 	
$estimateClient = getCompany($client,$con);
$estimateStatus = getEstStatus($estStatus,$con);
$estimateProject = getProjectLists($projectID,$projFilter,$con);
if ($estDate == '0000-00-00') {	$estDate = '';	} 
if ($estSubmitDate == '0000-00-00') {	$estSubmitDate = '';	} 
if ($estAcceptDate == '0000-00-00') {	$estAcceptDate = '';	} 
if ($estDeclineDate == '0000-00-00') {	$estDeclineDate = '';	}
$selHostYes = '';
$selHostNo = ''; 
if ($estHaveHost == 1)
	$selHostYes = ' checked';
else
	$selHostNo = ' checked';

include('form/estimate.php');

?>