$(window).load(function() {
setTimeout(function(){ employeelist(1) },100);
});
function employeelist(pagenum,emp_post){ 

	if(emp_post=='finance')
	{
		$('#mang').attr('class','btn-tab-02');
		$('#evry').attr('class','btn-tab-01');
		$('#sup').attr('class','btn-tab-01');
		$('#emps').attr('class','btn-tab-01');
	}
	else if(emp_post=='finance')
	{
		$('#sup').attr('class','btn-tab-02');
		$('#evry').attr('class','btn-tab-01');
		$('#mang').attr('class','btn-tab-01');
		$('#emps').attr('class','btn-tab-01');
	}
	else if(emp_post=='accounts')
	{
		$('#mang').attr('class','btn-tab-02');
		$('#evry').attr('class','btn-tab-01');
		$('#sup').attr('class','btn-tab-01');
		$('#emps').attr('class','btn-tab-01');
	}
	else if(emp_post=='accounts')
	{
		$('#sup').attr('class','btn-tab-02');
		$('#evry').attr('class','btn-tab-01');
		$('#mang').attr('class','btn-tab-01');
		$('#emps').attr('class','btn-tab-01');
	}	
	else if(emp_post=='marketing')
	{
		$('#mang').attr('class','btn-tab-02');
		$('#evry').attr('class','btn-tab-01');
		$('#sup').attr('class','btn-tab-01');
		$('#emps').attr('class','btn-tab-01');
	}
	else if(emp_post=='marketing')
	{
		$('#sup').attr('class','btn-tab-02');
		$('#evry').attr('class','btn-tab-01');
		$('#mang').attr('class','btn-tab-01');
		$('#emps').attr('class','btn-tab-01');
	}	
		
	else if(emp_post=='manager')
	{
		$('#mang').attr('class','btn-tab-02');
		$('#evry').attr('class','btn-tab-01');
		$('#sup').attr('class','btn-tab-01');
		$('#emps').attr('class','btn-tab-01');
	}
	else if(emp_post=='supervisor')
	{
		$('#sup').attr('class','btn-tab-02');
		$('#evry').attr('class','btn-tab-01');
		$('#mang').attr('class','btn-tab-01');
		$('#emps').attr('class','btn-tab-01');
	}else if(emp_post=='employee')
	{
		$('#emps').attr('class','btn-tab-02');
		$('#evry').attr('class','btn-tab-01');
		$('#mang').attr('class','btn-tab-01');
		$('#sup').attr('class','btn-tab-01');
	}else{
		$('#evry').attr('class','btn-tab-02');$('#emps').attr('class','btn-tab-01');
		$('#mang').attr('class','btn-tab-01');
		$('#sup').attr('class','btn-tab-01');
	}
		
	var imgs='';var dateformat='';var dateformat1=''; var dateformat2='';
	var jsonURL = serviceURLHR + "employeelist.php" ;
	//var jsonURL = serviceURLHR + "employeelist_sort.php" ;

	$('#busy').show();
	$.getJSON(jsonURL,{ 
	   page: pagenum,
	   emp_post: emp_post
	},function(data) {
		
		$('#busy').hide();
			$('#tablelist').html('');
			employeeslist = data.name;
			$('#headername').html('Cutlery List');
			var num=1;
			var i=1;var cnt=0;var name=''; 
			$.each(employeeslist, function(index, employeeslist) {
				if(employeeslist.e_id !=null){					  
					f_name=employeeslist.e_fname;	
					m_name=employeeslist.e_mname;	
					l_name=employeeslist.e_lname;	
					e_mail=employeeslist.e_mail;
					e_mailtitle=employeeslist.e_mail;
					e_mail = e_mail.substr(0,28);
					e_mob=employeeslist.e_mob;
					e_post=employeeslist.e_post;
					e_empid=employeeslist.employee_id;
					if(employeeslist.photo=='')
					{
						 photo='noimg.jpg';
					}
					else
					{ 
						if(employeeslist.e_gender=='Male' && employeeslist.photo=='noimg.jpg')
						{
							photo='noimg.jpg'; 
						}
						else if(employeeslist.e_gender=='Female' && employeeslist.photo=='noimg.jpg')
						{ 
							photo='Female.PNG'; 
						}
						else
						{
							photo=employeeslist.photo;	
						}
					}
					e_in=employeeslist.time_in;	
					e_out=employeeslist.time_out;	
					e_inloc=employeeslist.time_inloc;	
					e_outloc=employeeslist.time_outloc;	
		  
					employeename = f_name+' '+l_name;
					employeename = employeename.substr(0,20);
		  
					if(employeeslist.clock_status==1)
					{
						var btnval = 'Clocked-In';				 
						var bt_id=   'clockedout';
						var btncolor ='button-color-login';
						var emp_in=e_in;
						var empin_loc=e_inloc;
					}
					else
					{
						var btnval = 'Clocked-Out';
						var bt_id=   'clockedin';
						var btncolor ='button-color-logout';
						var emp_in=e_out;
						var empin_loc=e_outloc;
					}
					if(employeeslist.status==1)
					{
						var imgsts= 'images/bullet_green.png';
						var imgval= 1;
					}
					else
					{
						var imgsts= 'images/bullet_orange.png';
						var imgval= 0;
					}
		
					$('#tablelist').append('<div class="employee-cell"><div class="clocked-in"><span class="clocked-status" style="width:1px"> <input type="button" name="btn" class="btn_'+employeeslist.e_id+' '+btncolor+'" id="'+bt_id+'" nums="'+num+'" value="'+btnval+'" style="width:80px"  /><input type="hidden" name="txt"  class="txt_'+employeeslist.e_id+'" id="txt_id" value="'+employeeslist.clock_status+'"/></span><a href="timesheet-individual.html?employees='+employeeslist.e_id+'"><span class="clocked-time" data-times="'+emp_in+'" id="e_time'+num+'" >'+emp_in+'</span></a></div> <div class="employee-foto"><a href="profile-view.php?employ_id='+employeeslist.e_id+'"><img src="'+HRPATH+'upload/image/'+photo+'" alt="" border="0" width="170" height="172" /></a></div><div class="employee-designation">'+e_post+'</div><a class="findurl" href="profile-view.php?employ_id='+employeeslist.e_id+'" data-link="'+ employeeslist.e_id + '" id="emps_id'+num+'"><h2 style="font-size:15px; font-weight:bold;"><translate>'+employeename+'</translate></h2></a><h3 title="'+e_mailtitle+'">'+e_mail+'</h3><div style="float:left"><h4>Emp Code:'+e_empid+'</h4></div><div class="timesheet-nav"><a href="timesheet-individual.php?employees='+employeeslist.e_id+'"><translate>Timesheet</translate></a></div></div>');
					
				}
				
				num++;
				i++;
			});
	
			$.getJSON(serviceURLHR+"listing.php?loadtime=1",function(responce){
				var timeDetails = responce.timeDetails;											   
				$.each(timeDetails, function(index, timeDetails) {
					var empid = timeDetails.e_id;
					$('.btn_'+empid).attr('id','clockin');
					$('.btn_'+empid).value('clockout');
				});	
			});
			$('#submitid').css("display","none");
			$('#num').val(i);
			pagination(pagenum,0,'pagenum');
			
	});

}
function  pagination(pagenum,ingredient,divname)
{
	$.ajax({
	   url: serviceURLHR+"employeelistpagination.php?page="+pagenum+"&ingredient="+ingredient,
	   success: function(data){
		 
			  $("#"+divname+"" ).html(data);
	   }
	});	
}
loadBrchName();
function loadBrchName(){
	$.getJSON(serviceURLHR + 'branch.php?action=load',function(data){
		branch = data.branch;	
		$.each(branch, function(index, branch) {									 
			$('.SearchBrhLists').append('<option value="'+branch.branch_id+'">'+branch.branch_name+'</option>');
							
		});	
	});
}
loademployeelist();
function loademployeelist(){ 
	$.getJSON(serviceURLHR + 'employeename_list.php?action=load',function(data){
		empnamelist = data.empnamelist;	
		$.each(empnamelist, function(index, empnamelist) {									 
							$('.emp_list').append('<option value="'+empnamelist.e_id+'">'+empnamelist.e_fname+'</option>');
							$('.Searchemp_list').append('<option value="'+empnamelist.e_id+'">'+empnamelist.e_fname+'</option>');
							
		});	
	});
}

loademployeeStatus();
function loademployeeStatus(){ 
	$.getJSON(serviceURLHR + 'employeename_list.php?action=load_emp_status',function(data){
		empnamelist = data.empnamelist;	
		$.each(empnamelist, function(index, empnamelist) {									 
			$('#filter_emp_status').append('<option value="'+empnamelist.id+'">'+empnamelist.emply_stat+'</option>');
		});	
	});
}
loadDeptName();
function loadDeptName() {
	$.getJSON(serviceURLHR + "getdepartment.php?action=load",function(departdata) { 
		$('#busy').hide();
		$('#tabletabheadlist').html('');
		departnameList = departdata.department;
		$.each(departnameList,function(index,departnameList){
			$('#seldpt').append('<option value="'+departnameList.depart_id+'">'+departnameList.depart_name+'</option>');
			$('#filter_dept').append('<option value="'+departnameList.depart_id+'">'+departnameList.depart_name+'</option>');
		})	
	});
}
function load_emp_under_brnch_modified(branch_val)
{
	loadEmpBrnch_modified(branch_val);	
}
function loadEmpBrnch_modified(branch_val){
	if($('#filter_dept').val())
	{
		var dept_id=$('#filter_dept').val();	
	}
	else
	{
		var dept_id='';	
	}
	$.getJSON(serviceURLHR + 'employeename_list.php?action=loademp_branch_modified&branch='+branch_val+'&dept_id='+dept_id,function(data){
		empnamelist = data.empnamelist;	
		$('.Searchemp_list').html('<option value="">Employee name</option>');
		$.each(empnamelist, function(index, empnamelist) {									 
			$('.Searchemp_list').append('<option value="'+empnamelist.e_id+'">'+empnamelist.e_fname+'</option>');
		});	
	});			
}
function load_emp_under_dept_modified(dept_val){
	if($('#filter_branch').val())
	{
		var branch_id=$('#filter_branch').val();	
	}
	else
	{
		var branch_id='';	
	}
	$.getJSON(serviceURLHR + 'employeename_list.php?action=loademp_dept_modified&branch='+branch_id+'&dept_id='+dept_val,function(data){
		empnamelist = data.empnamelist;	
		$('.Searchemp_list').html('<option value="">Employee name</option>');
		$.each(empnamelist, function(index, empnamelist) {									 
			$('.Searchemp_list').append('<option value="'+empnamelist.e_id+'">'+empnamelist.e_fname+'</option>');
		});	
	});			
}
function modified_search_employee_list()
{
	var userid=$('.userid').val();
	if(userid == 1)
	{
		var branchid = $('#filter_branch').val();	
	}
	else
	{
		var branchid=$('.sessionbranch_id').val();
	}	
	var emp_id = $('#filter_emp_id').val();	
	var dept_id=$('#filter_dept').val();
	var emp_status=$('#filter_emp_status').val();
	console.log('hai');
	$.getJSON(serviceURLHR + "employeesearchlist.php?branch_id="+branchid+"&emp_id="+emp_id+"&dept_id="+dept_id+"&emp_status="+emp_status,
		function(data) { 
			$('#busy').hide();
  			$('#tablelist').html('');
			employeeslist = data.name;
		 	$('#headername').html('Cutlery List');
			var num=1;
			var i=1;var cnt=0;var name='';
			$.each(employeeslist, function(index, employeeslist) {
				f_name=employeeslist.e_fname;	
				m_name=employeeslist.e_mname;	
				l_name=employeeslist.e_lname;	
				e_mail=employeeslist.e_mail;	
				e_mailtitle=employeeslist.e_mail;
				e_mail = e_mail.substr(0,28);
				e_mob=employeeslist.e_mob;
				e_post=employeeslist.e_post;
				e_empid=employeeslist.employee_id;
				
				if(employeeslist.photo=='')
				{ 
					photo='noimg.jpg';
				}
				else
				{
					if(employeeslist.e_gender=='Male' && employeeslist.photo=='noimg.jpg')
				    {
						photo='noimg.jpg'; 
					}
				    else if(employeeslist.e_gender=='Female' && employeeslist.photo=='noimg.jpg')
				    { 
						photo='Female.PNG'; 
					}
					else
					{
			        	photo=employeeslist.photo;	
					}
			   	}
			
			   	e_in=employeeslist.time_in;	
			  	e_out=employeeslist.time_out;	
			 	e_inloc=employeeslist.time_inloc;	
			  	e_outloc=employeeslist.time_outloc;	
			  	employeename = f_name+' '+l_name;
			  	employeename = employeename.substr(0,20);
			  
			   	if(employeeslist.clock_status==1)
                {
			    	var btnval = 'Clocked-In';
                 	var bt_id=   'clockedout';
				 	var btncolor ='button-color-login';
				 	var emp_in=e_in;
				 	var empin_loc=e_inloc;
           		}
				else
				{
            		var btnval = 'Clocked-Out';
				 	var bt_id=   'clockedin';
				 	var btncolor ='button-color-logout';
				  	var emp_in=e_out;
		 			var empin_loc=e_outloc;
				}
				   
				if(employeeslist.status==1)
                {
				    var imgsts= 'images/bullet_green.png';
					var imgval= 1;
				}
				else
				{
					var imgsts= 'images/bullet_orange.png';
					var imgval= 0;
				}

			  
				$('#tablelist').append('<div class="employee-cell"><div class="clocked-in"><span class="clocked-status" style="width:1px"> <input type="button" name="btn" class="btn_'+employeeslist.e_id+' '+btncolor+'" id="'+bt_id+'" nums="'+num+'" value="'+btnval+'" style="width:80px" /><input type="hidden" name="txt"  class="txt_'+employeeslist.e_id+'" id="txt_id" value="'+employeeslist.clock_status+'"/></span><a href="timesheet-individual.html?employees='+employeeslist.e_id+'"><span class="clocked-time" data-times="'+emp_in+'" id="e_time'+num+'" >'+emp_in+" "+empin_loc+'</span></a></div> <div class="employee-foto"><a href="profile-view.php?employ_id='+employeeslist.e_id+'"><img src="'+HRPATH+'upload/image/'+photo+'" alt="" border="0" width="170" height="172" /></a></div><div class="employee-designation">'+e_post+'</div><a class="findurl" href="profile-view.php?employ_id='+employeeslist.e_id+'" data-link="'+ employeeslist.e_id + '" id="emps_id'+num+'"><h2 style="font-size:15px; font-weight:bold;"><translate>'+employeename+'</translate></h2></a><h3 title="'+e_mailtitle+'">'+e_mail+'</h3><div style="float:left"><h4>Emp Code:'+e_empid+'</h4></div><div class="timesheet-nav"><a href="timesheet-individual.php?employees='+employeeslist.e_id+'"><translate>Timesheet</translate></a></div></div>');
				num++;
				i++;
			});
		
			$.getJSON(serviceURLHR+"listing.php?loadtime=1",function(responce){
				var timeDetails = responce.timeDetails											   
				$.each(timeDetails, function(index, timeDetails) {
					var empid = timeDetails.e_id;
					$('.btn_'+empid).attr('id','clockin');
					$('.btn_'+empid).value('clockout');
				});	
			});
			
			$('#submitid').css("display","none");
		  	$('#num').val(i);
		   	pagination(pagenum,0,'pagenum');
		});
}