<?php

if (!isset($PathPrefix)) {
	$PathPrefix='../';
}

include($PathPrefix . 'includes/session.php');
$Title=_('Main Menu');
include('includes/header.php');

$sql = "SELECT id, name, contactno, lastappointment
		FROM lms_leads
		WHERE lastappointment > '" . date('Y-m-d 00:00:00') . "' AND lms_leads.owner = '" . $_SESSION['UserID'] . "'
		ORDER BY lastappointment
		LIMIT 10";

$result = DB_query($sql);
while($myrow = DB_fetch_array($result))
{
	$appointments[] = array('id' => $myrow['id'],
							'name' => $myrow['name'],
							'contactno' => $myrow['contactno'],
							'date' => $myrow['lastappointment']);
}


$sql = "SELECT lms_leads.id, lms_leads.name, lms_leads.contactno, lms_status.label, COUNT(lms_viewstat.view) AS views
		FROM lms_leads
		LEFT JOIN lms_status
		ON lms_leads.status = lms_status.id
		LEFT JOIN lms_viewstat
        ON lms_leads.id = lms_viewstat.lead_id
		WHERE lms_leads.watch = 1 AND lms_leads.owner = '" . $_SESSION['UserID'] . "'
		GROUP BY lms_leads.id";

$result = DB_query($sql);
while($myrow = DB_fetch_array($result))
{
	$watchlist[] = array('id' => $myrow['id'],
							'name' => $myrow['name'],
							'contactno' => $myrow['contactno'],
							'status' => $myrow['label'],
							'views' => $myrow['views']);
}


$sql = "SELECT count(*) as stat, lms_status.label
		FROM lms_leads
		LEFT JOIN lms_status
		ON lms_leads.status = lms_status.id
		WHERE lms_status.probability > 0 AND lms_status.probability < 1 AND lms_leads.owner = '" . $_SESSION['UserID'] . "'
		GROUP BY lms_leads.status
		ORDER BY lms_status.probability";

$result = DB_query($sql);
while($myrow = DB_fetch_array($result))
{
	$status[] = array('stat' => $myrow['stat'],
					'status' => $myrow['label']);

	$data_array .= ",['".$myrow['label']."',".$myrow['stat']."]";
}
?>
<div class="row">
	<div class="col-sm-6">
			<a href="lms_LeadsReport.php<?php echo ($detect->isMobile() ? '?MinView=1' : '') ?>" class="btn btn-info">Contacts List</a>
	</div>
	<div class="col-sm-6">
	</div>
</div>

<div class="row">
	<div class="col-sm-6">
		<h3>Appointments <span class="glyphicon glyphicon-time"></span></h3>
		<table class="table table-striped table-condensed">
			<thead>
				<tr>
					<th>Name</th>
					<th>Contact</th>
					<th>Date</th>
					<th></th>
				</tr>
			</thead>
			<tbody>
				<?php
				if(count($appointments) > 0)
				{
					foreach($appointments as $appointment)
					{
						echo '<tr>
								<td>'.$appointment['name'].'</td>
								<td><a href="tel:'.$appointment['contactno'].'">'.$appointment['contactno'].'</a></td>
								<td>'.date('D, jS M g:ia', strtotime($appointment['date'])).'</td>
								<td>
									<form class="form-inline" method="post" action="lms_LeadsReportViews.php" target="_blank">
										<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />
										<input type="hidden" name="lead" value="' . $appointment['id'] . '" />
										<button type="submit" name="view" class="btn btn-default"><span class="glyphicon glyphicon-stats"></span></button>
									</form>
								</td>
							</tr>';
					}
				}
				else
				{
					echo '<tr>
							<td colspan="4">No Appointments(s) Found!</td>
						</tr>';
				}
				?>
			</tbody>
		</table>
	</div>
	<div class="col-sm-6">
		<h3>Watch List <span class="glyphicon glyphicon-heart"></span></h3>
		<table class="table table-striped table-condensed">
			<thead>
				<tr>
					<th class="ascending">Name</th>
					<th class="ascending">Status</th>
					<th class="ascending">Views</th>
					<th></th>
				</tr>
			</thead>
			<tbody>
				<?php
				if(count($watchlist) > 0)
				{
					foreach($watchlist as $watch)
					{
						echo '<tr>
								<td>'.$watch['name'].'</td>
								<td>'.$watch['status'].'</td>
								<td class="number">'.$watch['views'].'</td>
								<td>
									<form class="form-inline" method="post" action="lms_LeadsReportViews.php" target="_blank">
										<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />
										<input type="hidden" name="lead" value="' . $watch['id'] . '" />
										<button type="submit" name="view" class="btn btn-default"><span class="glyphicon glyphicon-stats"></span></button>
									</form>
								</td>
							</tr>';
					}
				}
				else
				{
					echo '<tr>
							<td colspan="5">Watch list is empty!</td>
						</tr>';
				}
				?>
			</tbody>
		</table>
	</div>
</div>

<div class="row">
	<div class="col-sm-6">
		<?php
		if(count($status) > 0)
		{
			echo '<h3>Active Leads</h3>
					<div id="donutchart"></div>';

			echo '<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
				<script type="text/javascript">
					google.charts.load("current", {packages:["corechart"]});
					google.charts.setOnLoadCallback(drawChart);
					function drawChart() {
					var data = google.visualization.arrayToDataTable([
						["Status", "Count"]
						'.$data_array.'
					]);
				
					var options = {
						pieHole: 0.3,
						chartArea: {
							width: "100%"
						},
					};
				
					var chart = new google.visualization.PieChart(document.getElementById("donutchart"));
					chart.draw(data, options);
					}
				</script>';
		}
		?>
	</div>
	<div class="col-sm-6"></div>
</div>

<?php
include('includes/footer.php');
?>