<?php
/*
* date last modified 03-10-2017
* Edited by PI-Digi Logical Solutions
* HTML Files - manage_staff.html.php
* Mysql Tables - tbl_admin, tbl_user, tbl_admin_type
*/
require_once('./classes/class.noseo_pagination.php'); # pagination class
require_once('./includes/functions.php'); # general functions

if(isset($_REQUEST['p']) && $_REQUEST['p']!=''){
	$p = trim($_REQUEST['p']);
} else {
	$p = 1;
}

$pageVars = '';
$where = ' id > 0 ';

if(isset($_POST['clear'])) { ?>
<script language="javascript" type="text/javascript">			
	window.location='index.php?page=<?php echo $page;?>';
</script>
<?php }

if(isset($_GET['action']) && $_GET['action'] == 'delete') {	
	if(isset($_GET['del_id']) && $_GET['del_id']!=''){
		$delete = mysqli_query($con,'DELETE FROM tbl_new_leads WHERE id='.$_GET['del_id']);
		if($delete) {
			$success = 'Deleted the lead';
			?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>';
			</script>
	        <?php
		}
	}
}

if(isset($_GET['action']) && $_GET['action'] == 'accept') {	
	if(isset($_GET['id']) && $_GET['id']!=''){
		$update = mysqli_query($con,"UPDATE tbl_new_leads SET status = 'Accepted' WHERE id='".$_GET['id']."'");
		if($update) {
			$success = 'Accepted the lead';
			?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>';
			</script>
	        <?php
		}
	}
}

if(isset($_GET['action']) && $_GET['action'] == 'reject') {	
	if(isset($_GET['id']) && $_GET['id']!=''){
		$update = mysqli_query($con,"UPDATE tbl_new_leads SET status = 'Rejected' WHERE id='".$_GET['id']."'");
		if($update) {
			$success = 'Rejected the lead';
			?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>';
			</script>
	        <?php
		}
	}
}

if(isset($_GET['action']) && $_GET['action'] == 'processing') {	
	if(isset($_GET['id']) && $_GET['id']!=''){
		$update = mysqli_query($con,"UPDATE tbl_new_leads SET status = 'Processing' WHERE id='".$_GET['id']."'");
		if($update) {
			$success = 'The lead is under process';
			?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>';
			</script>
	        <?php
		}
	}
}

if(isset($_GET['action']) && $_GET['action'] == 'pending') {	
	if(isset($_GET['id']) && $_GET['id']!=''){
		$update = mysqli_query($con,"UPDATE tbl_new_leads SET status = 'Pending' WHERE id='".$_GET['id']."'");
		if($update) {
			$success = 'Changed to Pending status';
			?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>';
			</script>
	        <?php
		}
	}
}

if(isset($_GET['action']) && $_GET['action'] == 'completed') {	
	if(isset($_GET['id']) && $_GET['id']!=''){
		$update = mysqli_query($con,"UPDATE tbl_new_leads SET status = 'Completed' WHERE id='".$_GET['id']."'");
		if($update) {
			$success = 'The lead has been completed';
			?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>';
			</script>
	        <?php
		}
	}
}


//get all assigned admins
$assigneeRs = mysqli_query($con, "SELECT admin_id, ad_username FROM tbl_admin WHERE role != 'super_admin' AND status = 'Active' AND admin_id IN (SELECT DISTINCT assignee FROM tbl_new_leads) ORDER BY ad_username");


//get all statuses
$statusRs = mysqli_query($con, "SELECT DISTINCT status FROM tbl_new_leads");


if(isset($_REQUEST['searchvalue']) && strlen($_REQUEST['searchvalue'])>0) {
	$searchvalue = $_REQUEST['searchvalue'];
	$where .= " AND (client_name LIKE '%".$searchvalue."%' OR phone LIKE '%".$searchvalue."%' OR location LIKE '%".$searchvalue."%') ";
	$pageVars.= '&searchvalue='.$searchvalue.'&';
}

if(isset($_REQUEST['search_assignee']) && strlen($_REQUEST['search_assignee'])>0) {
	$assigneeId = $_REQUEST['search_assignee'];
	$where .= " AND assignee='".$assigneeId."' ";
	$pageVars.= '&search_assignee='.$assigneeId.'&';
}

if(isset($_REQUEST['search_status']) && strlen($_REQUEST['search_status'])>0) {
	$searchStatus = $_REQUEST['search_status'];
	$where .= " AND status='".$searchStatus."' ";
	$pageVars.= '&search_status='.$searchStatus.'&';
}	

if(isset($_REQUEST['sort']) && $_REQUEST['sort']=='asc'){
	$select = "SELECT * FROM tbl_new_leads WHERE ".$where." ORDER BY delivery_date";
} else {
	$select = "SELECT * FROM tbl_new_leads WHERE ".$where." ORDER BY delivery_date DESC";
}
//echo $select;
// Using pagination class
$pagination = new pagination_noseo($select);
$execDetails = $pagination->createPages(30);

$pageLnk = 'index.php?page='.$page.'&'.$pageVars;
$pageNums = $pagination->pageNumber(5, $pageLnk);
?>