<?php

// If this script is being called via Ajax
if (!isset($parentClients)) {
	$parentClients = getPclientArray($con);
	$variables = getVariables('client',$page);
	$sql = 'SELECT c.*, sc.clientName AS subName, sc.clientAbbr AS subAbbr FROM clients AS c LEFT JOIN clients AS sc ON (c.subClientOf = sc.clientID) WHERE c.clientID='. $editID;
	$res = $con->query($sql);
	$r = $res->fetch_array(MYSQLI_ASSOC);
	
	foreach($variables['displayArray'] as $value) {
		if ($value == 'clientID')
			$$value = $editID;
		else
			$$value =  $r[$value];
	}
}

$adminLinks = ''; 
			
if ($clientStatus == 2) {
				
	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="client" data-adminact="delete" data-page="client" data-section="'. $tableName .'" data-editid="' . $clientID . '" title="Delete CLient"><i class="fa fa-trash"></i></a> '; 
} else { 
	$tableName = "clientsInactive";
	$adminLinks .= ' <a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="client" data-adminact="react" data-page="client" data-section="'. $tableName .'" data-editid="' . $clientID . '" title="Reactivate Client"><i class="fa fa-refresh"></i></a> '; 
}
			
$adminLinks = ' <a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="client" data-adminact="edit" data-page="client" data-section="'. $tableName .'" data-editid="' . $clientID . '" title="Edit Client"><i class="fa fa-pencil-square-o"></i></a> '. $adminLinks;
			
// Format Website
if (!empty($clientWebsite))
	$clientWebsite = '<p><a href="http://' . $clientWebsite . '" target="_blank">' . $clientWebsite . '</a></p>';
else
	$clientWebsite = '';	
			
// Format Client Email
if (!empty($row['clientContactEmail']))
	$email = '<a href="mailto:' . $clientContactEmail . '">' . $clientContactEmail . '</a><br>';
else
	$email = '';
			
// Format Client Address
$address = '';
if (!empty($clientAddressStreet))
	$address .= $clientAddressStreet . '<br>';
if (!empty($clientAddress2))
	$address .= $clientAddress2 . '<br>';
if (!empty($clientAddressPO))
	$address .= $clientAddressPO . '<br>';
if (!empty($clientAddressCity))
	$address .= $clientAddressCity;
if ((!empty($clientAddressCity)) && (!empty($clientState)) )
	$address .= ', ';
if (!empty($clientState))
	$address .= $clientState;
if ((!empty($clientState)) && (!empty($clientAddressZip)) )
	$address .= ', ';
if (!empty($clientAddressZip))
	$address .= $clientAddressZip;

// Client Abbreviation
if (!empty($clientAbbr))
	$abbr = ' ('. $clientAbbr .')';
else
	$abbr = '';

// Sub-Client Of
if (!empty($subClientOf)) {
	if (!empty($subAbbr))	
		$subof = $subAbbr;
	else
		$subof = $subName;
}
else
	$subof = '';
			
$listItem = '<tr id="record-'. $clientID .'">
					<td><a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="client" data-adminact="edit" data-page="client" data-section="'. $tableName .'" data-editid="' . $clientID . '"><strong>' . $clientName . $abbr . '</strong></a>' . $clientWebsite .'</td>
					<td>'. $subof .'</td>
					<td>' . $clientContactName . '<br>
						' . $email;
if (!empty($clientContactPhone))
	$listItem .= $clientContactPhone .'<br>';
if (!empty($clientContactMobile))
	$listItem .= $clientContactMobile .' (mobile)';
$listItem .= '</td>
				<td>' . $address . '
				</td>
				<td>' . Date::convert($clientSince, 'Y-m-d', 'm/d/Y') . '</td>
				<td class="adminActions">'. $adminLinks .'</td>
			</tr>';
?>