<?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 }

$statRs = mysqli_query($con, "SELECT DISTINCT status FROM tbl_job WHERE status != 'temp' AND is_project != 'yes'");

if(isset($_GET['action']) && $_GET['action'] == 'delete') {
	/* Delete User */
	if(isset($_GET['del_id']) && $_GET['del_id']!=''){
		//delete all extra tech requests
		mysqli_query($con, "DELETE FROM tbl_job_extra_tech WHERE job_id = '".$_GET['del_id']."' AND technician_id = '0'");		
		$delete = mysqli_query($con,'DELETE FROM tbl_job WHERE id='.$_GET['del_id']);
		if($delete) {
			$success = 'Deleted the job';
			?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>&p=<?php echo $p;?>';
			</script>
	        <?php
		}
	}
}

if(isset($_GET['action']) && $_GET['action'] == 'discard') {	
	if(isset($_GET['id']) && $_GET['id']!=''){
		$taskId = trim($_GET['id']);
		$update = mysqli_query($con,"UPDATE tbl_job SET status = 'discarded' WHERE id = '".$taskId."'");
		if($update) {
			//remove all payments of the task
			mysqli_query($con, "DELETE FROM tbl_technician_payments WHERE job_id = '".$taskId."'");
			$success = 'Discarded the job';
			?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>&p=<?php echo $p;?>';
			</script>
	        <?php
		}
	}
}

if(isset($_GET['action']) && $_GET['action'] == 'copy') {
	//take a copy of the task for the same user (only date and time changes to current one)	
	if(isset($_GET['id']) && $_GET['id']!=''){
		$taskId = trim($_GET['id']);
		//insert into my_table( col1, col2 ) select col1, col2 from my_table where pk_id=?;
		$taskRec = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM tbl_job WHERE id = '".$taskId."'"));
		$isProject = $taskRec['is_project'];		
		$userId = $taskRec['user_id'];
		$categoryId = $taskRec['category_id'];		
		$address_id= $taskRec['address_id'];
		$details = $taskRec['details'];
		$image = $taskRec['image'];
		$date = date('Y-m-d H:i:s');
		$taskDate = date('Y-m-d');
		$taskTime = date('H:i:s');
		$type = $taskRec['type'];
		$dispatch_type = $taskRec['dispatch_type'];
		$status = 'New';
		$house_name = $taskRec['house_name'];
		$street = $taskRec['street'];
		$place = $taskRec['place'];
		$pin = $taskRec['pin'];
		$city = $taskRec['city'];
		$latitude = $taskRec['latitude'];
		$longitude = $taskRec['longitude'];		
		
		//insert as a new task
		$insert = mysqli_query($con, "INSERT INTO tbl_job (is_project, user_id, category_id, address_id, details, image, date, task_date, task_time, type, dispatch_type, status, house_name, street, place, pin, city, latitude, longitude) VALUES 
		('".$isProject."' ,'".$userId."' ,'".$categoryId."' ,'".$address_id."' ,'".$details."' ,'".$image."' ,'".$date."' ,'".$taskDate."' ,'".$taskTime."' ,
		'".$type."' ,'".$dispatch_type."' ,'".$status."' ,'".$house_name."' ,'".$street."' ,'".$place."' ,'".$pin."' ,'".$city."' ,'".$latitude."' ,'".$longitude."')");
		if($insert) {
			$copyId = mysqli_insert_id($con);				
			$success = 'Replicated the task to ID - '.$copyId;
			?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>';
			</script>
	        <?php
		}
	}
}

$pageVars = '';

// Get All User details to list 
$where = '';

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

if(isset($_REQUEST['task_type']) && strlen($_REQUEST['task_type'])>0) {
	$taskType = $_REQUEST['task_type'];
	if($taskType == 'task'){
		$where .= " AND is_project!='yes' ";
	} else if($taskType == 'project'){
		$where .= " AND is_project='yes' ";
	} else if($taskType == 'service'){
		$where .= " AND is_service='yes' ";
	}	
	$pageVars.= 'task_type='.$taskType.'&';
}

if(isset($_REQUEST['tech_id']) && $_REQUEST['tech_id']!='') {
	$taskTechId = trim($_REQUEST['tech_id']);
	$where .= " AND technician_id = '".$taskTechId."' ";
	$pageVars.= '&tech_id='.$taskTechId.'&';
}

if(isset($_REQUEST['task_id']) && $_REQUEST['task_id']!='') {
	$taskId = trim($_REQUEST['task_id']);
	if(strpos($taskId, '#') !== false){
		$taskId = ltrim($taskId, '#');
	}
	$where .= " AND id = '".$taskId."' ";
	$pageVars.= '&task_id='.$taskId.'&';
}

if(isset($_REQUEST['date_to']) && strlen($_REQUEST['date_to'])>0) {
		$to_date = trim($_REQUEST['date_to']);
		if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$to_date)) {
		    //ok - yyy-mm-dd
		} else {
		    $dateArr = explode('-', $to_date);
			$to_date = $dateArr[2].'-'.$dateArr[1].'-'.$dateArr[0];
		}
} else {
		$to_date = '';
}

if(isset($_REQUEST['from_date']) && strlen($_REQUEST['from_date'])>0) {
		$from_date = trim($_REQUEST['from_date']);
		if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$from_date)) {
		    //ok - yyy-mm-dd
		} else {
		    $dateArr = explode('-', $from_date);
			$from_date = $dateArr[2].'-'.$dateArr[1].'-'.$dateArr[0];
		}
} else {
		$from_date = '';
}

$curDate = date('Y-m-d');

if($from_date!='' && $to_date!=''){
	$where .= " AND task_date BETWEEN '".$from_date."' AND '".$to_date."' ";
	$pageVars.= '&from_date='.$_REQUEST['from_date'].'&date_to='.$_REQUEST['date_to'].'&';
} else if($from_date!=''){	
	$where .= " AND task_date BETWEEN '".$from_date."' AND '".$curDate."' ";
	$pageVars.= '&from_date='.$_REQUEST['from_date'].'&';
} else if($to_date!=''){	
	$where .= " AND task_date BETWEEN '".$to_date."' AND '".$curDate."' ";
	$pageVars.= '&date_to='.$_REQUEST['date_to'].'&';
} 


$select = "SELECT * FROM tbl_job WHERE status <> 'temp' AND user_id IN (SELECT DISTINCT id FROM tbl_user WHERE status != 'Deleted') ".$where." ORDER BY id DESC";
/*if($_SERVER['REMOTE_ADDR']=='202.88.237.77'){
	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);
?>