<?php

$admin = 'project'; 

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

$adminAct = $_REQUEST['adminAct']; 

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

if ($adminAct == 'new') {
	$op = 'add';
	$editInpID = '<input type="hidden" name="op" value="'. $op .'" />'; 
	foreach($variables['fieldArray'] as $value) { 
		if ($value == 'projectDisp')
			$$value = 1;
		elseif ($value == 'clientID')
			$client = ''; 
		else 	
			$$value = '';
	}
	$subBtn = 'Add Project'; 
	$modalTitle = 'Create New Project';
} else {
	$op = '';
	$editID = $_REQUEST['editID']; 
	$editInpID = '<input type="hidden" name="editID" value="'. $editID .'" />';
	
	$query = 'SELECT * FROM projects WHERE projectID=' . $editID; 
	$result = $con->query($query);
	
	while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
		
		foreach($variables['fieldArray'] as $value) {
			if ($value == 'clientID')
				$client = $row[''. $value .'']; 
			else
				$$value =  $row[''. $value .''];
		}
		if ($adminAct == 'react') {
			$op = 'react';
			$delConfirm = ""; 
			$subBtn = 'Reactivate Project';
			$modalTitle = 'Reactivate Project: '. $projectName;
		}
		elseif ($adminAct == 'edit') {
			$op = 'edit';
			$delConfirm = ""; 
			$subBtn = 'Save Changes';
			$modalTitle = 'Edit Project: '. $projectName;
		}
		elseif ($adminAct == 'delete') {
			$op = 'delete';
			$delConfirm = ' onSubmit="return sure()"';
			$subBtn = 'Delete Project';
			$modalTitle = 'Delete Project: '. $projectName . '?';
		}
	}
	
	// Get Payment History For Project
	$pmtQuery = 'SELECT paymentAmount, paymentStatus FROM payments WHERE projectID=' . $projectID .''; 
	$pmtResult = $con->query($pmtQuery);
	$amtPaid = '';
	$amtInvoiced = ''; 
	while ($pmt=$pmtResult->fetch_array(MYSQLI_ASSOC)) {
		$amtInvoiced = $amtInvoiced + $pmt['paymentAmount'];
		if ($pmt['paymentStatus'] == 1)
			$amtPaid = $amtPaid + $pmt['paymentAmount'];
	}
	if (empty($amtPaid))
		$amtPaid = "0.00";
	if (empty($amtInvoiced))
		$amtInvoiced = "0.00";
	$outstandingAmount = $projectAmount-$amtPaid;
}
	
$projectClient = getCompany($client,$con);
$projectStatus = getProjStatus($projectStatus,$con);
$projectType = getProjType($projectType,$con);
$projectCat = getProjCat($projectCategory,$con);
$projectPriority = getProjPriority($priority,$con);
$projectEstimate = getEstimates($estID,$con);
$billRate = getRates($currentRateID,$con);	
	
if ($projectCurrent == 1)
	$currentCheck = ' checked';
else
  	$currentCheck = '';
	
if ($projectMaint == 1)
	$maintCheck = ' checked';
else
  	$maintCheck = '';

include('form/project.php'); 

?>