<?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
require_once('./includes/push_functions.php');

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

if(isset($_REQUEST['tech_id']) && $_REQUEST['tech_id']!=''){
	$technicianId = trim($_REQUEST['tech_id']);
} else {
	?>
	  <script language="javascript" type="text/javascript">	
	    alert('Something went wrong!');		
		window.location='index.php?page=manage_technician';
	  </script>
	<?php
}

if(isset($_GET['action']) && $_GET['action'] == 'delete') {	
	if(isset($_GET['del_id']) && $_GET['del_id']!=''){
		$delete = mysqli_query($con,'DELETE FROM tbl_technician_category WHERE id='.$_GET['del_id']);
		if($delete) {
			$success = 'Deleted the category';	?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&tech_id=<?php echo $technicianId;?>&success=<?php echo $success; ?>';
			</script>
	        <?php
		}
	}
}
if(isset($_GET['action']) && $_GET['action'] == 'verify') {	
	if(isset($_GET['id']) && $_GET['id']!=''){
		$update = mysqli_query($con,"UPDATE tbl_technician_category SET verified = 'yes' WHERE id='".$_GET['id']."'");
		if($update) {
			
			$res_cat_id = mysqli_fetch_array(mysqli_query($con, 'SELECT category_id FROM tbl_technician_category WHERE id='.$_GET['id']));
			$res_cat_name = mysqli_fetch_array(mysqli_query($con, 'SELECT name FROM tbl_service_categories WHERE id ='.$res_cat_id['category_id']));
			
			$exec_token = mysqli_query($con, 'SELECT fcm_token, device FROM tbl_fcm_tokens WHERE tech_id='.$technicianId);
			if(isset($exec_token) && mysqli_num_rows($exec_token)>0) {
				while($res_token = mysqli_fetch_array($exec_token)) {	
					if($res_token['fcm_token']!='') {							
						$title = SITE_NAME;
						$body = 'Category: '.$res_cat_name['name'].' is approved';
						if($res_token['device']=='android') {								
							//sendGCM($title, $body, $res_token['fcm_token'], "tech");
							sendGCMCategory($title, $body, $res_token['fcm_token'], "category");
						}							
					}
				}
			}
			
			$success = 'Verified the category';	?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&tech_id=<?php echo $technicianId;?>&success=<?php echo $success; ?>';
			</script>
	        <?php
		}
	}
}

$pageVars = '&tech_id='.$technicianId.'&';


$select = "SELECT * FROM tbl_technician_category WHERE technician_id = '".$technicianId."'";
//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);



?>