<?php

// If this script is being called via Ajax
if (!isset($parentClients)) {
	$admin = 'project'; 
	$page = (isset($_POST['page']) ? $_POST['page'] : $admin);
	$parentClients = getPclientArray($con);
	$variables = getVariables('project',$page);
	$query = "SELECT * FROM projects AS p LEFT JOIN clients AS c ON (p.clientID = c.clientID) LEFT JOIN projectStatus AS ps on (p.projectStatus = ps.pstatID) LEFT JOIN projectType AS pt ON (p.projectType = pt.ptypeID) WHERE p.projectID=". $projectID;
	$result = $con->query($query);
	$row = $result->fetch_array(MYSQLI_ASSOC);
}

$adminLinks = ''; 

foreach($variables['fieldArray'] as $value) {
	$$value =  $row[$value];
}
$ptypeDesc = $row['ptypeDesc'];
$ptypeID = $row['ptypeID'];
$subClientOf = $row['subClientOf'];
$clientName = $row['clientName'];
$clientAbbr = $row['clientAbbr'];
			
// Company Name or Abbreviation
$client = $clientName;
if (!empty($clientAbbr)) 
	$client .= ' ('. $clientAbbr . ')';

// target Completion Date
if ($projectTargetComp == "0000-00-00")
	$compDate = "N/A";
else
	$compDate = Date::convert($projectTargetComp, 'Y-m-d', 'm/d/Y');
	
// Get Payment History For Project
$pmtQuery = 'SELECT paymentAmount, paymentStatus FROM payments WHERE projectID=' . $projectID .''; 
$pmtResult = $con->query($pmtQuery);
		
$amtPaid = '';
$amtInvoiced = ''; 
if ($pmtResult) {
	while ($pmt=$pmtResult->fetch_array(MYSQLI_ASSOC)) {
		
		
		if (is_numeric($amtInvoiced) && is_numeric($pmt['paymentAmount'])) {
			$amtInvoiced = $amtInvoiced + $pmt['paymentAmount'];
			if ($pmt['paymentStatus'] == 1) {
				if (is_numeric($amtPaid) && is_numeric($pmt['paymentAmount'])) {
					$amtPaid = $amtPaid + $pmt['paymentAmount'];
				}
			}
		}
	}
}
if (empty($amtPaid))
	$amtPaid = "0.00";
if (empty($amtInvoiced))
	$amtInvoiced = "0.00";

if ($projectDisp == 1) {
	foreach ($parentClients as $key => $value) {
		if (($subClientOf == $key) || ($clientID == $key)) 
			$tableName = cleanClientName($value);
	}
	if(empty($tableName))
		$tableName = 'other';
	
	$section = ((isset($op)) ? (($op == 'add') ? $tableName : $section) : (isset($section) ? $section : ''));
	
	$adminLinks .= ' <a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="'. $admin .'" data-adminact="delete" data-page="'. $page .'" data-section="'. $tableName .'" data-editid="' . $projectID . '" title="Delete Project"><i class="fa fa-trash"></i></a> ';
} else { 
	$tableName = "projectsInactive";
	$adminLinks .= ' <a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="'. $admin .'" data-adminact="react" data-page="'. $page .'" data-section="'. $tableName .'" data-editid="' . $projectID . '" title="Reactivate Project"><i class="fa fa-refresh"></i></a> ';
}

$adminLinks = ' <a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="'. $admin .'" data-adminact="edit" data-page="'. $page .'" data-section="'. $tableName .'" data-editid="' . $projectID . '" title="Edit Project"><i class="fa fa-pencil-square-o"></i></a> '. $adminLinks;

$adminLinks = ' <a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="project-time" data-adminact="" data-page="'. $page .'" data-section="'. $tableName .'" data-editid="' . $projectID . '" title="Hours"><i class="fa fa-clock-o"></i></a> '. $adminLinks; 

$adminLinks = ' <a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="project-task" data-adminact="" data-page="'. $page .'" data-section="'. $tableName .'" data-editid="' . $projectID . '" title="Tasks"><i class="fa fa-check-square-o"></i></a> '. $adminLinks;

if ($ptypeID == 1) // Project based
	$adminLinks = ' <a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="project-payment" data-page="'. $_REQUEST['type'] .'" data-section="'. $tableName .'" data-adminact="" data-editid="' . $projectID . '" title="Payments"><i class="fa fa-money"></i></a> '. $adminLinks; 

// Table Rows
$listItem = '<tr id="record-'. $projectID .'">
				<td>' .$client . '</td>
				<td><a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="'. $admin .'" data-adminact="edit" data-page="'. $page .'" data-section="'. $tableName .'" data-editid="' . $projectID . '">' . $projectName . '</a></td>
				<td>' . $ptypeDesc . '</td>
				<td>' . $compDate . '</td>
				<td>&#36;' . number_format($projectAmount,2,'.','') . '</td>
				<td>&#36;' . number_format($amtInvoiced, 2,'.','') . '</td>
				<td>&#36;'. number_format($amtPaid, 2,'.','') .'</td>
				<td class="adminActions">'. $adminLinks .'</td>
			</tr>';

?>