<?php
session_start();  
include_once("../class/class.MySQL.php");

$db	=new MySQL();

$limit=$page_rows;
$page = $_GET['page'];   
	
if($page) 
	$start = ($page - 1) * $limit;  //first item to display on this page
else
	$start = 0;	
	
	
$emp_sel_statss = mysql_query("SELECT * FROM emp_status where (emply_stat='Terminated' || emply_stat='Resigned') ");
while($emp_sel_statsq=mysql_fetch_array($emp_sel_statss))
{	
$em_statq[]=$emp_sel_statsq['id'];
}

$em_statqs=join(',',$em_statq);	
if(empty($_GET['emp_status'])){
$where="where 1 and A.e_id!=1 and A.emp_status not in($em_statqs)";
}
else
{
$where="where 1 and A.e_id!=1 ";
}
if(!empty($_GET['branch_id']) &&  empty($_GET['emp_status']))
{
	$where.=" and A.branchid=".$_GET['branch_id']."  and A.emp_status not in($em_statqs) ";
}
if(!empty($_GET['emp_id']) &&  empty($_GET['emp_status']))
{
	$where.=" and A.e_id=".$_GET['emp_id']."  and A.emp_status not in($em_statqs) ";
}
if(!empty($_GET['dept_id']) &&  empty($_GET['emp_status']))

{
	$where.=" and A.departid=".$_GET['dept_id']."   and A.emp_status not in($em_statqs)";
}
if(!empty($_GET['emp_status']))
{
	$where.=" and A.emp_status=".$_GET['emp_status']." ";
}
if(!empty($_GET['branch_id']) &&  !empty($_GET['emp_status']))
{
	$where.=" and A.branchid=".$_GET['branch_id']."  ";
}
if(!empty($_GET['dept_id']) &&  !empty($_GET['emp_status']))
{
	$where.=" and A.departid=".$_GET['dept_id']."  ";
}
if(!empty($_GET['emp_id']) &&  !empty($_GET['emp_status']))
{
	$where.=" and A.e_id=".$_GET['emp_id']."  ";
}
//echo 'select A.e_fname, A.e_mname, A.e_gender, A.e_lname, A.e_mail, A.e_mob, A.e_post, A.e_id, A.employee_id, A.clock_status, A.status, B.photo, C.e_in, C.e_out, C.time_in, C.time_inloc, C.time_out, C.time_outloc, D.desig_name from employee as A left join file as B on A.e_id=B.e_id left join time as C on A.e_id=C.e_id left join designation as D on A.e_post=D.desig_id '.$where.' group by A.e_id order by C.time_in asc limit '.$start.','.$limit.' ';
$name = $db->fetchQuery('select A.e_fname, A.e_mname, A.e_gender, A.e_lname, A.e_mail, A.e_mob, A.e_post, A.e_id, A.employee_id, A.clock_status, A.status, B.photo, C.e_in, C.e_out, C.time_in, C.time_inloc, C.time_out, C.time_outloc, D.desig_name from employee as A left join file as B on A.e_id=B.e_id left join time as C on A.e_id=C.e_id left join designation as D on A.e_post=D.desig_id '.$where.' group by A.e_id   order by  A.e_fname asc     limit '.$start.','.$limit.' ');
$arr=array();
$i=1;
   foreach($name as $name)
   {
   $em_id=$name['em_id'];
   $e_id=$name['e_id'];
   /*$join1="c.id='". $em_id."'";
   $join1.=' and a.e_id=b.e_id and a.e_id=c.e_id and a.e_post=d.desig_id' ;
   $ordr='c.time_in '; 
   $qry = $db->fetchAlljoineachrows('`employee` as a,`file` as b,`time` as c,`designation` as d', $where,'','a.e_id',$limit,$cols='a.e_fname,a.e_mname,a.e_gender,a.e_lname,a.e_mail,a.e_mob,a.e_post,a.e_id,a.employee_id,a.clock_status,a.status,b.photo,c.e_in,c.e_out,c.time_in,c.time_inloc,c.time_out,c.time_outloc,d.desig_name' ,' desc',$join1,$start );*/
   
   $e_fname=$name['e_fname'];
   $e_mname=$name['e_mname'];
   $e_gender=$name['e_gender'];
   $e_lname=$name['e_lname'];
   $e_mail=$name['e_mail'];
   $e_mob=$name['e_mob'];
   $e_post=$name['desig_name'];
   $e_id=$name['e_id'];
   $clock_status=$name['clock_status'];
   $status=$name['status'];
   $photo=$name['photo'];
   $e_in=$name['e_in'];
   $e_out=$name['e_out'];
   $time_in=$name['time_in'];
   $time_inloc=$name['time_inloc'];
   $time_out=$name['time_out'];
   $time_outloc=$name['time_outloc'];
   $e_employee_id=$name['employee_id'];
   $arr[$i]['e_fname']= $e_fname;
   $arr[$i]['e_mname']= $e_mname;
   $arr[$i]['e_gender']= $e_gender;
   $arr[$i]['e_lname']= $e_lname;
   $arr[$i]['e_mail']= $e_mail;
   $arr[$i]['e_mob']= $e_mob;
	$arr[$i]['e_post']= $e_post;
	$arr[$i]['e_id']= $e_id;
	$arr[$i]['clock_status']= $clock_status;
	$arr[$i]['status']= $status;
	$arr[$i]['photo']= $photo;   
	$arr[$i]['e_in']= $e_in;  
	$arr[$i]['e_out']= $e_out;
	$arr[$i]['time_in']= $time_in;
	$arr[$i]['time_inloc']= $time_inloc;
	$arr[$i]['time_out']= $time_out;
	$arr[$i]['time_outloc']= $time_outloc;
	$arr[$i]['employee_id']= $e_employee_id;     
	$i++;
   }
	
	echo '{"name":'.json_encode($arr).'}';
?>