<?php
	include "db_connection/db_connection.php";
	include "config/config.php";
	$page   = isset($_GET['page'])     ? intval($_GET['page']) : 1;
	$limit  = isset($_GET['limit'])    ? intval($_GET['limit']) : 100;
	$search = isset($_GET['search'])   ? mysqli_real_escape_string($connect, $_GET['search']) : "";
	$sortBy = isset($_GET['sortBy'])   ? $_GET['sortBy'] : "emp_det_id";
	$sortDir= isset($_GET['sortDir'])  ? $_GET['sortDir'] : "DESC";

	$offset = ($page - 1) * $limit;

	$allowedCols = ['emp_det_id','emp_name','email1','email2','mbl1','mbl2'];
	if(!in_array($sortBy, $allowedCols)) $sortBy = 'emp_det_id';
	$sortDir = ($sortDir == "ASC") ? "ASC" : "DESC";


	$where = "";

	if ($search != "") {

    $search = mysqli_real_escape_string($connect, $search);

    $where = "WHERE 
        dept LIKE '%$search%' 
        OR email1 LIKE '%$search%'
        OR email2 LIKE '%$search%'
		OR mbl1 LIKE '%$search%'
		OR mbl2 LIKE '%$search%'
		";
}


	$totalQuery = mysqli_query($connect, "SELECT COUNT(*) AS cnt FROM emp_det $where");
	$total = mysqli_fetch_assoc($totalQuery)['cnt'];
	$pages = ceil($total / $limit);

	 $SLCT = "SELECT * FROM emp_det $where 
			ORDER BY $sortBy $sortDir 
			LIMIT $limit OFFSET $offset";

$page_id= base64_encode(base64_encode(base64_encode(2))); //THIS IS FOR PAGE IDENTIFICATION IN BL FILE for active inactive
	$data = "";
	$loop=1;
			//echo $SLCT;
			$QURY = mysqli_query($connect,$SLCT);
			$loop=1;
			if(mysqli_num_rows($QURY)>0)
			{
	while($FTCH = mysqli_fetch_object($QURY)){
		$emp_id	=	base64_encode(base64_encode(base64_encode($FTCH->emp_det_id)));

				$SLCT_d = "SELECT GROUP_CONCAT(master_dept_name  SEPARATOR ', ') AS master_dept_name FROM master_dept WHERE master_dept_id IN($FTCH->master_dept_id)";
				$QURY_d = mysqli_query($connect,$SLCT_d);
				$FTCH_d = mysqli_fetch_object($QURY_d);
				
				if($loop%2 != "0")
						$bg = "#ffffff";
					else
						$bg = "#f2f2f2";
					
				$name = '';
				$emp_disp ='';
 				$name_count = strlen($FTCH->name);
				$name='';
				if($name_count>15){
					$name = substr($FTCH->name,0,15)."...";
				} else {
					$name = $FTCH->name;
				} 
			 if($name_count>15){ 
			$emp_disp .='<div class="tooltip1">'.$name.'
				<span class="tooltiptext1">'.$FTCH->name.'</span>
			</div>';
			  } else {  
			$emp_disp .=$name;
			 } 
			 
				$name_count = strlen($FTCH_d->master_dept_name);
				$name='';
				$dep_name ='';
				if($name_count>20){
					$name = substr($FTCH_d->master_dept_name,0,20)."...";
				} else {
					$name = $FTCH_d->master_dept_name;
				} 
			 if($name_count>20){ 
			$dep_name.='<div class="tooltip1">'.$name.'
				<span class="tooltiptext1">'.$FTCH_d->master_dept_name.'</span>
			</div>';
			  } else {  
			$dep_name.=$name; 
			 }  
			 
			 $status_html='';
			$page_id1='1';
		   if($FTCH->statu_s=="active"){  
		   
		   $status_html .='<a href="bl/employee_BL.php?emp_id='.$emp_id.'&page_id='.$page_id.'&statu_s=active">Active</a>';
		    }else{ 
			 $status_html .='<a href="bl/employee_BL.php?emp_id='.$emp_id.'&page_id='.$page_id.'&statu_s=inactive">Inactive</a>';
			} 
	   $data .= '<tr style="background-color:'.$bg.'">
		<td style="text-align:center;">'.$loop.'</td>
		<td>'.$emp_disp.'</td>
		<td>'.$dep_name.'</td>
		<td>'.$FTCH->email_id.'</td>
		<td>'.$FTCH->mobile.'</td>
		<td><a href="employee_view.php?emp_id='.$emp_id.'" id="viewlink'.$loop.'" onfocus="foc(this.id)" onblur="stnon(this.id)">View</a></td>
		<td>
		<a href="employee_upd.php?emp_id='.$emp_id.'" id="viewlink'.$loop.'" onfocus="foc(this.id)" onblur="stnon(this.id)">Update</a></td>
		</td>
		<td>'.$status_html.'</td>
	</tr>';
	  $loop ++;
	}
	} else {
		$data .= "<tr><td colspan='7' style='text-align:center;'>There are no records in the category</td></tr>";
	}
	$response = [
		"rows"   => $data,
		"pages"  => $pages,
		"total"  => $total
	];

	echo json_encode($response);
