<?php
	
	$phpself .= '?op=marketing&type=contacts';
	$pageName = 'Contact Administration';
	$tableInfo = ''; 
	
	$adminBtns .= '<a href="#" class="admin btn btn-default" data-toggle="modal" data-target="#adminModal" data-admintype="'. $_REQUEST['type'] .'" data-adminact="new" data-editid=""><i class="fa fa-plus-square"></i> Add a NEW '. ucfirst($_REQUEST['type']) .'</a>';
	
	include('pages/inc/page-header.php');
	
	$query = 'SELECT * FROM marketing_contacts AS c LEFT JOIN prospect_source AS s ON (c.contact_source = s.sourceid) LEFT JOIN marketing_campaigns AS cmp ON (c.last_contact_campaign = cmp.campid) WHERE c.display=1 ORDER BY c.lastname ASC, c.firstname ASC';
	$result = $con->query($query);

	if ($result) {
		$linkVars = '';
		$tableFieldNames = '<th>Name</th>
								<th>Company</th>
								<th>Email</th>
								<th>Source</th>
								<th>Last</th>
								<th>Campaign</th>
								<th>&nbsp;</th>';
		
		include('inc/data-tables/table-top.php');
		include('inc/data-tables/table-bottom.php');
		
		$sortColumns = 7;
		$noSortColumns = 6; // Column Number from left (column 1=0) 
		$scrollHeight = '500px';
		$tabTable = false;
		 
		while ($row=$result->fetch_array(MYSQLI_ASSOC)) {

			$adminLinks = ''; 
			$adminLinks .= ' <a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="'. $_REQUEST['type'] .'" data-adminact="edit" data-page="'. $_REQUEST['type'] .'" data-section="" data-editid="' . $row['contactID'] . '" title="Edit '. ucfirst($_REQUEST['type']) .'"><i class="fa fa-pencil-square-o"></i></a> ';
				
			$adminLinks .= ' <a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="'. $_REQUEST['type'] .'" data-adminact="delete" data-page="'. $_REQUEST['type'] .'" data-section="" data-editid="' . $row['contactID'] . '" title="Delete '. ucfirst($_REQUEST['type']) .'"><i class="fa fa-trash"></i></a> ';
				
			if ($row['last_contact'] == '0000-00-00')
				$last_contact = '';
			else 
				$last_contact = Date::convert($row['last_contact'], 'Y-m-d', 'm/d/Y');
							
			$tableInfo .= '<tr id="record-'. $row['contactID'] .'">';
			$tableInfo .= '<td><a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="'. $_REQUEST['type'] .'" data-adminact="edit" data-page="'. $_REQUEST['type'] .'" data-section="" data-editid="' . $row['contactID'] . '" title="Edit '. ucfirst($_REQUEST['type']) .'">'. $row['lastname'] .','. $row['firstname'] .'</a><br><em>'. $row['title'] .'</em></td>';
			$tableInfo .= '<td>';
			if (!empty($row['website'])) 
				$tableInfo .=	'<a href="http://'. $row['website'] .'" target="_blank">';
			$tableInfo .= $row['company'];
			if (!empty($row['website'])) 
				$tableInfo .=	'</a>';
			$tableInfo .= '</td>';
			$tableInfo .= '<td><a href="mailto:'. $row['email'] .'">'. $row['email'] .'</a></td>';
			$tableInfo .= '<td>';
			if (!empty($row['source_website'])) 
				$tableInfo .=	'<a href="http://'. $row['source_website'] .'" target="_blank">';
			if (!empty($row['source_abb'])) 
				$tableInfo .= $row['source_abb'] . '&nbsp;';
			else 
				$tableInfo .= $row['source_name'];
			if (!empty($row['source_website'])) 
				$tableInfo .=	'</a>';
			$tableInfo .=	'</td>';
			
			$tableInfo .= '<td>'. $last_contact .'</td>';
			$tableInfo .= '<td>'. $row['camp_name'] .'</td>';
			$tableInfo .= '<td class="adminActions">'. $adminLinks .'</td>';
			$tableInfo .= '</tr>';
		}
		$content .= '<div class="row"><div class="col-sm-12">';
		$content .= $tableHeader . '<tbody id="contacts">' . $tableInfo . '</tbody>'. $tableFooter;
		$content .= '</div></div>';

		include('inc/data-tables/dataTables.php');
	}
?>