<?php 
/********************************************************************************* 
 *  This file is part of Sentrifugo.
 *  Copyright (C) 2014 Sapplica
 *   
 *  Sentrifugo is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Sentrifugo is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Sentrifugo.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Sentrifugo Support <support@sentrifugo.com>
 ********************************************************************************/
?>

<?php sapp_Global::generateClientValidations($this->form);
//die;
?>
<Script type='text/javascript'>
	function getempscreeningdetails(flag)
	{	
		if(flag == 'perpage')
		{
		   $('#pageno').val('1');
		} 

       if(flag == 'button'){
			$('#pageno').val('1');
			specimen = $.trim($('#specimen').val());		
			if(specimen != '')
			{		
				specimentext = $("#specimen option:selected").text();	  
			  $('#specimenclear').show();
			}  
			
			empname = $.trim($('#empname').val());		
			if(empname != '')
			{			
				hiddenempname = $.trim($('#hiddenempname').val());	
				if(hiddenempname != '')
				{
					hiddenempnametext = $('#hiddenempnametext').val();
					$('#empname').val($('#hiddenempnametext').val());
					$('#empnameclear').show();
				}else{
					$('#empname').val('');
				}				
			}  
			
			agencyname = $.trim($('#agencyname').val());		
			if(agencyname != '')
			{			
				hiddenagencyname = $.trim($('#hiddenagencyname').val());	
				if(hiddenagencyname != '')
				{
					hiddenagencynametext = $('#hiddenagencynametext').val();
					$('#agencyname').val($('#hiddenagencynametext').val());
					$('#agencynameclear').show();
				}else{
					$('#agencyname').val('');
				}							
			}	
			
			screeningtype = $.trim($('#screeningtype').val());
			if(screeningtype != '')
			{				
				$('#screeningtypeclear').show(); 
			}			
			
			process_status = $('#process_status').val();
			if(process_status !='')
			{
				process_statustext = $("#process_status option:selected").text();
			  $('#process_statusclear').show(); 
			}
			
			month = $('#month').val();
			if(month !='')
			{
				monthtext = $("#month option:selected").text();
			  $('#monthclear').show(); 
			}
			
			year = $('#year').val();
			if(year !='')
			{
			  yeartext= $("#year option:selected").text();
			  $('#yearclear').show(); 
			}
       }else{
    	   if($('#specimenclear').is(':visible'))
    	   {
    		 $('#specimen').val(specimen);
    		 $('#s2id_specimen .select2-choice span').html(specimentext);		
    	   }
    	   if($('#empnameclear').is(':visible'))
    	   { 
    		 $('#empname').val(empname);
    		 $('#hiddenempname').val(empname);
    		 $('#hiddenempnametext').val(hiddenagencynametext);
    	   }
    	   if($('#agencynameclear').is(':visible'))
    	   { 
    		 $('#agencyname').val(agencyname);
    		 $('#hiddenagencyname').val(agencyname);
    		 $('#hiddenagencynametext').val(hiddenagencynametext);
    	   }
    	   if($('#agencynameclear').is(':visible'))
    	   { 
    		 $('#agencyname').val(agencyname);
    		 $('#hiddenagencyname').val(agencyname);
    		 $('#hiddenagencynametext').val(hiddenagencynametext);
    	   }
    	   if($('#screeningtypeclear').is(':visible'))
    	   {
        	   //alert(screeningtype);
    		    $('#screeningtype').val(screeningtype).trigger("change");
    			
    	   }
    	   if($('#process_statusclear').is(':visible'))
    	   {
    		    $('#process_status').val(process_status);
    			$('#s2id_process_status .select2-choice span').html(process_statustext);
    	   }
    	   if($('#monthclear').is(':visible'))
    	   {
    		    $('#month').val(month);
    			$('#s2id_month .select2-choice span').html(monthtext);
    	   }
    	   if($('#yearclear').is(':visible'))
    	   {
    		    $('#year').val(year);
    			$('#s2id_year .select2-choice span').html(yeartext);
    	   }
    	   
       }
			
		var pageno = $('#pageno').val();		
		var perpage = $('#perPagetext').val();
		if(perpage != '')
		$('#perpage').val(perpage);
		
		var sortby = $('#sortby').val();
		var by = $('#by').val();
		
		var columnsortby = $('#columnsortby').val();
		var columnby = $('#columnby').val();
		var checkedheaders = $('#checkedheaders').val();

		var param_str = $('#filters_div').find('select, textarea, input').serialize();
		$('#id_param_string').val(param_str);
		$.ajax({
				url : base_url+'/reports/empscreening/format/html',
				type : 'POST',
				data : 'specimen='+specimen+'&empname='+empname+'&agencyname='+agencyname+'&screeningtype='+screeningtype+'&process_status='+process_status+'&month='+month+'&year='+year+'&pageno='+pageno+'&perpage='+perpage+'&sortby='+sortby+'&by='+by+'&columnsortby='+columnsortby+'&columnby='+columnby+'&checkedheaders='+checkedheaders,
				datatype : 'html',
				beforeSend : function () 
							{
								$.blockUI({ width:'50px',message: $("#spinner").html() });
							},
				success : function(response)						
							{
								$('#loader').remove();
								if(response != '' && response != 'null' && $.trim(response) != 'norecord')
								{
									$('#userlogpreview').html(response);
								}
								if($.trim(response) == 'norecord')
								{
									$('#userlogpreview').html('No Data Found');
								}
							}
		});
	}
	function sortcolumn(sortflag,divid)
	{
		$('#columnid').val(divid);
	    if($('#columnsortby').val() == sortflag)
		{
			if($('#columnby').val() == 'Asc')
				$('#columnby').val('Desc');		
			else
				$('#columnby').val('Asc');
		}
		else
		{
		    $('#columnsortby').val(sortflag);
			if($('#columnby').val() == 'Asc')
				$('#columnby').val('Desc');	
			else
				$('#columnby').val('Asc'); 		
		}
		getempscreeningdetails('');
	}
	function clearempscreeningsearch(searchfilterid)
	{
		var inputid = jQuery.trim(searchfilterid.replace("clear", ""));
		$('#'+inputid).val(''); 
		if(inputid == 'screeningtype')
		{
			screeningtype = '';screeningtypetext='';
			$('#s2id_screeningtype .select2-choice span').html('Select screening type');
			$('#s2id_screeningtype .select2-search-choice').remove();
		}        		
		if(inputid == 'specimen')
		{
			specimen = ''; specimentext = '';
			$('#s2id_specimen .select2-choice span').html('Select Employee/Candidate');
		}   
		if(inputid == 'process_status')
		{
			process_status='';process_statustext='';
			$('#s2id_process_status .select2-choice span').html('Select Status');
		}   
		if(inputid == 'month')
		{
			month = '';monthtext = '';
			$('#s2id_month .select2-choice span').html('Select Month');
		} 
		if(inputid == 'year')
		{
			year='';yeartext='';
			$('#s2id_year .select2-choice span').html('Select Year');
		}  
		if(inputid == 'empname')
		{
			hiddenempnametext = '';empname ='';
			$('#hiddenempname').val('');
			$('#hiddenempnametext').val('');
		}  		
		if(inputid == 'agencyname')
		{
			agencyname = '';hiddenagencynametext = '';
			$('#hiddenagencyname').val('');
			$('#hiddenagencynametext').val('');
		}  
		$('#'+searchfilterid).hide(); 
	    getempscreeningdetails('');
	}
	function clearall()
	{
		year='';yeartext='';month = '';monthtext = '';process_status='';process_statustext='';screeningtype = '';screeningtypetext='';agencyname = '';hiddenagencynametext = '';hiddenempnametext = '';empname ='';specimen = ''; specimentext = '';
		
		if($('#specimen').val() !='')
		{
			$('#s2id_specimen .select2-choice span').html('Select Employee/Candidate');
			$('#specimen').val(''); 
		} 	
		if($('#empname').val() !='')
			$('#empname').val('');		
		if($('#agencyname').val() !='')
			$('#agencyname').val('');      
		if($('#screeningtype').val() !='')
		{
			$('#s2id_screeningtype .select2-choice span').html('Select screening type');
			$('#s2id_screeningtype .select2-search-choice').remove();
			$('#screeningtype').val(''); 
		} 	
		if($('#process_status').val() !='')
		{
			$('#s2id_process_status .select2-choice span').html('Select Status');
			$('#process_status').val(''); 
		} 			
		if($('#month').val() !='')
		{
			$('#s2id_month .select2-choice span').html('Select Month');
			$('#month').val(''); 
		} 	
		if($('#year').val() !='')
		{
			$('#s2id_year .select2-choice span').html('Select Year');
			$('#year').val(''); 
		} 			
		if($('#hiddenempname').val() != '')
			$('#hiddenempname').val('');
		if($('#hiddenempnametext').val() != '')
			$('#hiddenempnametext').val('');	
		if($('#hiddenagencyname').val() != '')
			$('#hiddenagencyname').val('');
		if($('#hiddenagencynametext').val() != '')
			$('#hiddenagencynametext').val('');		
			
			
		 /* Start Make all hidden variables to default */
		   $('#perPagetext').val('20');
		   $('#pageno').val('1');
		   $('#sortby').val('modifieddate');
		   $('#by').val('Desc');
		   $('#columnsortby').val('');
		   $('#columnby').val('');
		   $('#columnid').val('');
		   $('#checkedheaders').val('specimen_name,specimen_flag_name,agencyname,screeningtype_name,process_status,createdname,createddate,isactive_text');
		   $('#specimenclear').hide();
		   $('#empnameclear').hide();
		   $('#agencynameclear').hide();
		   $('#screeningtypeclear').hide();
		   $('#process_statusclear').hide();
		   $('#monthclear').hide();
		   $('#yearclear').hide();
	  /* End */ 	

		getempscreeningdetails();	  
	}
	function displaytabs()
	{
		if($('#selectfields').hasClass("config-up"))		
		{
			$( '#selectfields').removeClass("config-up");
			$( '#selectfields').addClass("config-down");
			$('#tabdiv').slideDown();	

			var overlay	= '<div id="reportgridoverlay" class="overlayreport"></div>';
			$('#reportgrid').prepend(overlay);      

		}
		else
		{
			$( '#selectfields').removeClass("config-down");
			$( '#selectfields').addClass("config-up");
			$('#tabdiv').slideUp();
			
			$('.overlayreport').remove();
		}
	}
	function canceltabs()
	{
		var selectedColumns = $('#selectedColumns').val();
		  selectedColumns = selectedColumns.split(','); 
		  if(selectedColumns.length > 0){ 
			setTimeout(function(){	  
				 $("#tabdiv input").each(function() {
					var val = jQuery.trim($(this).val()); 				
					if(jQuery.inArray(val, selectedColumns) != -1){
						$('#check_'+val).prop('checked', true);
					}else{
					$('#check_'+val).prop('checked', false);
					}   		
				});
			},1000);	
		  }
		
	   $('#selectfields').removeClass("config-down").addClass("config-up");
	   $('#tabdiv').slideUp();
	   $('#reportgridoverlay').remove();
	}
	function setcolumnstab()
	{
            var chk_len = $("#tabdiv input:checked").length;
            if(chk_len == 0)
            {
                jAlert("Please check atleast one column.");                
                $( '#selectfields').addClass("config-up");
                displaytabs();
                return false;
            }
            else 
            {
                assignheaders();
                getempscreeningdetails('');
            }
	}
	function assignheaders()
	{
		var chkArray = [];     	  
		$("#tabdiv input:checked").each(function() {
			chkArray.push($(this).val());
		});   
	
		var checkedheaders;
		checkedheaders = chkArray.join(',');
		$('#checkedheaders').val(checkedheaders);
	}
	
	function clearEmpScreeningAutoCompleteNames(ele)
	{
		var elementid = $(ele).prop('id');	    
		var elementvalue = $('#'+elementid).val();
		if(elementvalue != '')
		{
			var text = '';
			hiddenname = $('#hidden'+elementid).val();					   
		    if(elementid == 'empname')
			{
				text = $('#hiddenempnametext').val();
				if(hiddenname =='' && text != elementvalue)
				{
					$("#"+elementid).val('');
					$('#empnameclear').hide();
					$('#hiddenempname').val('');
					$('#hiddenempnametext').val('');
				}else if(text != elementvalue){
					$("#"+elementid).val(text);
				}
			}
			else if(elementid == 'agencyname')
			{
				text = $('#hiddenagencynametext').val();
				if(hiddenname =='' && text != elementvalue)
				{
					$("#"+elementid).val('');
					$('#agencynameclear').hide();
					$('#hiddenagencyname').val('');
					$('#hiddenagencynametext').val('');
				}else if(text != elementvalue){
					$("#"+elementid).val(text);
				}
			}
		}  
	}
</script>
<?php $checkedheaders = 'specimen_name,specimen_flag_name,agencyname,screeningtype_name,process_status,createdname,createddate,isactive_text';
echo $this->reportsheader('backgroundreport'); 
?>
<div class="export-links">
<div class="fltright" style="width:600px;">
<div class="export-links">
<div class="sprite export-xls" style="display:none;" id="excel">Export to Excel</div>	
<div class="sprite export-pdf" style="display:none;" id="pdf">Export to PDF</div></div>
</div>
</div>
	<div class="left-config-ctrl">
    <div class="heading-name-1">configure report</div>
    <form name="<?php echo $this->form->getName(); ?>"	id="<?php echo $this->form->getId(); ?>" action="<?php echo $this->form->getAction();?>" method="<?php echo $this->form->getMethod(); ?>">
		<div class="total-form-controller-left">
			<div id="filters_div">		
				<div class="new-form-ui">
					 <label><?php echo $this->form->specimen->getLabel(); ?></label>
					 <button type="button" id="specimenclear" name="specimenclear" class="inputclear" style="display:none;" onclick="clearempscreeningsearch('specimenclear')">Clear</button>
					 <div class="division"><?php echo $this->form->specimen; ?>					 
						<span id="errors-<?php echo $this->form->specimen->getId(); ?>" class="errors">
						 <?php echo isset($this->msgarray['specimen'])?$this->msgarray['specimen']:"";?>
						</span>
					 </div>
				</div>
				
				<div class="new-form-ui">
					 <label><?php echo $this->form->empname->getLabel(); ?></label>
					 <button type="button" id="empnameclear" name="empnameclear" class="inputclear" style="display:none;" onclick="clearempscreeningsearch('empnameclear')">Clear</button>
					 <div class="division"><?php echo $this->form->empname; ?>
						<input type="hidden" id="hiddenempname" name="hiddenempname" value=""/>
						<input type="hidden" id="hiddenempnametext" name="hiddenempnametext" value=""/>
						<span id="errors-<?php echo $this->form->empname->getId(); ?>" class="errors">
						 <?php echo isset($this->msgarray['empname'])?$this->msgarray['empname']:"";?>
						</span>
					 </div>
				</div>
				
				<div class="new-form-ui">
					 <label><?php echo $this->form->agencyname->getLabel(); ?></label>
					 <button type="button" id="agencynameclear" name="agencynameclear" class="inputclear" style="display:none;" onclick="clearempscreeningsearch('agencynameclear')">Clear</button>
					 <div class="division"><?php echo $this->form->agencyname; ?>
						<input type="hidden" id="hiddenagencyname" name="hiddenagencyname" value=""/>
						<input type="hidden" id="hiddenagencynametext" name="hiddenagencynametext" value=""/>
						<span id="errors-<?php echo $this->form->agencyname->getId(); ?>" class="errors">
						 <?php echo isset($this->msgarray['agencyname'])?$this->msgarray['agencyname']:"";?>
						</span>
					 </div>
				</div>
				
				<div class="new-form-ui-multi">
					 <label><?php echo $this->form->screeningtype->getLabel(); ?></label>
					 <button type="button" id="screeningtypeclear" name="screeningtypeclear" class="inputclear" style="display:none;" onclick="clearempscreeningsearch('screeningtypeclear')">Clear</button>
					 <div class="division"><?php echo $this->form->screeningtype; ?>						
						<span id="errors-<?php echo $this->form->screeningtype->getId(); ?>" class="errors">
						 <?php echo isset($this->msgarray['screeningtype'])?$this->msgarray['screeningtype']:"";?>
						</span>
					 </div>
				</div>	
				
				<div class="new-form-ui">
					 <label><?php echo $this->form->process_status->getLabel(); ?></label>
					 <button type="button" id="process_statusclear" name="process_statusclear" class="inputclear" style="display:none;" onclick="clearempscreeningsearch('process_statusclear')">Clear</button>
					 <div class="division"><?php echo $this->form->process_status; ?>						
						<span id="errors-<?php echo $this->form->process_status->getId(); ?>" class="errors">
						 <?php echo isset($this->msgarray['process_status'])?$this->msgarray['process_status']:"";?>
						</span>
					 </div>
				</div>	
				
				<div class="new-form-ui">
					 <label><?php echo $this->form->month->getLabel(); ?></label>
					 <button type="button" id="monthclear" name="monthclear" class="inputclear" style="display:none;" onclick="clearempscreeningsearch('monthclear')">Clear</button>
					 <div class="division"><?php echo $this->form->month; ?>						
						<span id="errors-<?php echo $this->form->month->getId(); ?>" class="errors">
						 <?php echo isset($this->msgarray['month'])?$this->msgarray['month']:"";?>
						</span>
					 </div>
				</div>	
				
				<div class="new-form-ui">
					 <label><?php echo $this->form->year->getLabel(); ?></label>
					 <button type="button" id="yearclear" name="yearclear" class="inputclear" style="display:none;" onclick="clearempscreeningsearch('yearclear')">Clear</button>
					 <div class="division"><?php echo $this->form->year; ?>						
						<span id="errors-<?php echo $this->form->year->getId(); ?>" class="errors">
						 <?php echo isset($this->msgarray['year'])?$this->msgarray['year']:"";?>
						</span>
					 </div>
				</div>	
				
				<input type="hidden" id="id_param_string" value="" />
				<input type="hidden" id="generatereport" name="generatereport" value="" /> 
				<input type="hidden" name="pageno" id="pageno" value="<?php echo $this->pageno;?>" />
				<input type="hidden" name="sortby" id="sortby" value="<?php echo $this->sortby?>" />
				<input type="hidden" name="by" id="by" value="<?php echo $this->by;?>" />
				<input type="hidden" name="perpage" id="perpage" value="<?php echo $this->perpage;?>" />
				<input type="hidden" name="columnsortby" id="columnsortby" value="<?php echo $this->columnsortby;?>" />
				<input type="hidden" name="columnby" id="columnby" value="<?php echo $this->columnby;?>" />
				<input type="hidden" name="columnid" id="columnid" value="" />
				<input type="hidden" id="checkedheaders" name="checkedheaders" value="<?php echo $checkedheaders; ?>" />		
			</div>
			<div class="new-form-ui-submit mrgetop5">
				<button type="button" class="previewreport" onclick="getempscreeningdetails('button');" id="businessunitsgo" title="Generate Preview">Generate Report</button>			
				<button type="button" id="clear" onclick="clearall();" name="clear">Reset</button>
			</div>
    </form>
				
		</div>					
	</div>
	<div class="right-config-ctrl">
		<div id="userlogpreview" class="all-grid-control" ></div>
	</div>

</div>
</div>

<script type='text/javascript'>
	$(document).ready(function() {
		
		year='';yeartext='';month = '';monthtext = '';process_status='';process_statustext='';screeningtype = '';screeningtypetext='';agencyname = '';hiddenagencynametext = '';hiddenempnametext = '';empname ='';specimen = ''; specimentext = '';
		
		getempscreeningdetails();
		
		$('#empname').autocomplete({
			source: base_url+"/reports/getspecimennames",        
			select: function( event, ui ) 
			{
				var sel_id = ui.item.id;           
				$('#hiddenempname').val(ui.item.id);
				$('#hiddenempnametext').val(ui.item.value);				
				if(sel_id == '')
				{
					$('#empname').val('');
					$('#hiddenempnametext').val('');
					return false;
				}
			}
		}).data( "ui-autocomplete" )._renderItem = function( ul, item ) 
			{
				return $( "<li>" )
				.append( "<a><span>" + item.label + "</span></a>" )
				.appendTo( ul );
			}; 
		
		$('#agencyname').autocomplete({
			source: base_url+"/reports/getagencynames",        
			select: function( event, ui ) 
			{
				var sel_id = ui.item.id;           
				$('#hiddenagencyname').val(ui.item.id);		
				$('#hiddenagencynametext').val(ui.item.value);
				if(sel_id == '')
				{
					$('#agencyname').val('');
					$('#hiddenagencynametext').val('');
					return false;
				}
			}
		}).data( "ui-autocomplete" )._renderItem = function( ul, item ) 
			{
				return $( "<li>" )
				.append( "<a><span>" + item.label + "</span></a>" )
				.appendTo( ul );
			}; 
		
		
		$('#excel').click(function() {
			
			var param_str = $('#id_param_string').val();
			var url= base_url+"/default/reports/getexcelreportempscreening?"+param_str; 	
			var form = [ '<form method="POST" action="', url, '" accept-charset=utf-8 id="idfrm_export" >' ];	
			form.push('</form>');        
			jQuery(form.join('')).appendTo('body')[0].submit();       
				$('#idfrm_export').remove();
	    });
		$('#pdf').click(function(){		  
		   downloadBUsPdf(base_url + '/reports/getempscreeningpdf', '#<?php echo $this->form->getId();?>');	
	   });
	});
</script>