<?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>
 ********************************************************************************/
?>

<script type="text/javascript">

function paginationUserLogReport(){

      if($('#emp_roleclear').is(':visible'))
	   {
		 $('#emp_role').val(emp_role);
		 $('#s2id_emp_role .select2-choice span').html(emp_roletext);
	   }else{
		   $('#emp_role').val('');
	       $('#s2id_emp_role .select2-choice span').html('Select Employee Role');
	   }	  

	   if($('#groupclear').is(':visible'))
	   {
		 $('#group').val(group);
		 $('#s2id_group .select2-choice span').html(grouptext);		
	   }else{
		   $('#group').val('');
	       $('#s2id_group .select2-choice span').html('Select Group');
	   }

	   if($('#employeeIdfclear').is(':visible'))
	   { 
		 $('#employeeIdf').val(empid);
		 $('#hiddenemployeeIdf').val(empid);
		 $('#hiddenemployeeIdftext').val(empid);
	   }else{
		   $('#employeeIdf').val('');
		   $('#hiddenemployeeIdf').val('');
		   $('#hiddenemployeeIdftext').val('');
	   }

	   if($('#usernameclear').is(':visible'))
	   {
		
		 $('#username').val(username);
		 $('#hiddenusername').val(username);
		 $('#hiddenusernametext').val(username);
	   }else{
		   $('#username').val('');
		   $('#hiddenusername').val('');
		   $('#hiddenusernametext').val('');
	   }

	   if($('#emailIdclear').is(':visible'))
	   {		  
		 $('#emailId').val(emailId);
		 $('#hiddenemailId').val(emailId);
		 $('#hiddenemailIdtext').val(emailId);
	   }else{
		   $('#emailId').val('');
		   $('#hiddenemailId').val('');
		   $('#hiddenemailIdtext').val('');
	   }

	   if($('#logindateclear').is(':visible'))
	   {
		 $('#logindate').val(logindate);
	   }else{
		   $('#logindate').val('');
		}
	  
	   
	   if($('#ipaddressclear').is(':visible'))
	   {
		 $('#ipaddress').val(ipaddress);
		 $('#hiddenipaddress').val(ipaddress);
		 $('#hiddenipaddresstext').val(ipaddress);
	   }else{
		   $('#ipaddress').val('');
			 $('#hiddenipaddress').val('');
			 $('#hiddenipaddresstext').val('');
	  }
    
    fields = $('#fields').val(); 
    var page =  $('#pageno').val();
    var per_page = $('#per_page').val();
    $('#generatereport').val('');
    var reporttype = $("#generatereport").val();

    var sortBy = $.trim($('#sortby').val()); 
    var order = $('#order').val();
    set_all_params();
     $.ajax({
			type: "POST",
			url: base_url +'/reports/userlogreport/format/html',
			data: 'emp_role='+emp_role+'&group='+group+'&employeeIdf='+empid+'&username='+username+'&emailId='+emailId+'&logindate='+logindate+'&ipaddress='+ipaddress+'&pageno='+page+'&sortby='+sortBy+'&order='+order+'&per_page='+per_page+'&generatereport='+reporttype+'&fields='+fields,
			beforeSend : function (){
				 $.blockUI({ width:'50px',message: $("#spinner").html() });
			},
			success: function(responce) {
				$.unblockUI();
				$("#userlogpreview").html(responce);	
						
			},
		});

}
function set_all_params()
{
    var data = $('#id_all_div').find('select,textarea,input').serialize();
    $('#idhid_allparams').val(data);
}
</script>

<?php sapp_Global::generateClientValidations($this->form);
 echo $this->reportsheader('logreport'); 
?>


<div class="export-links">
<div class="fltright"><div class="sprite export-xls" id="xcel">Export to Excel</div><div class="sprite export-pdf" id="pdf">Export to PDF</div></div>
</div>
<div id="id_all_div">
<div class="left-config-ctrl">
<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 class="heading-name-1">Generate Custom report</div>

		<div class="new-form-ui">
			<label><?php echo $this->form->emp_role->getLabel(); ?> </label> <button type="button" id="emp_roleclear" name="emp_roleclear" class="inputclear" style="display:none;">Clear</button>
			<div class="division">
			<?php echo $this->form->emp_role; ?>
				<span id="errors-<?php echo $this->form->emp_role->getId(); ?>"
					class="errors"><?php echo isset($this->msgarray['emp_role'])?$this->msgarray['emp_role']:"";?>
				</span>
			</div>
		</div>

		<div class="new-form-ui">
			<label><?php echo $this->form->group->getLabel(); ?> </label><button type="button" id="groupclear" name="groupclear" class="inputclear" style="display:none;">Clear</button>
			<div class="division">
			<?php echo $this->form->group; ?>
				<span id="errors-<?php echo $this->form->group->getId(); ?>"
					class="errors"><?php echo isset($this->msgarray['group'])?$this->msgarray['group']:"";?>
				</span>
			</div>
		</div>

		<div class="new-form-ui">
			<label><?php echo $this->form->employeeIdf->getLabel(); ?> </label>
			<input type="hidden" id="hiddenemployeeIdf" name="hiddenemployeeIdf" value="" />
			<input type="hidden" id="hiddenemployeeIdftext" name="hiddenemployeeIdftext" value="" />
			<button type="button" id="employeeIdfclear" name="employeeIdfclear" class="inputclear" style="display:none;">Clear</button>
			<div class="division">
								<?php echo $this->form->employeeIdf; ?>
				<span id="errors-<?php echo $this->form->employeeIdf->getId(); ?>"
					class="errors"><?php echo isset($this->msgarray['employeeIdf'])?$this->msgarray['employeeIdf']:"";?>
				</span>
			</div>
		</div>

		<div class="new-form-ui">
			<label><?php echo $this->form->username->getLabel(); ?> </label><button type="button" id="usernameclear" name="usernameclear" class="inputclear" style="display:none;">Clear</button>
			<div class="division">
				<input type="hidden" id="hiddenusername" name = "hiddenusername" value="" />
				<input type="hidden" id="hiddenusernametext" name = "hiddenusernametext" value="" />
				<?php echo $this->form->username; ?>
				<span id="errors-<?php echo $this->form->username->getId(); ?>"
					class="errors"><?php echo isset($this->msgarray['username'])?$this->msgarray['username']:"";?>
				</span>
			</div>
		</div>

		<div class="new-form-ui">
			<label><?php echo $this->form->emailId->getLabel(); ?> </label><button type="button" id="emailIdclear" name="emailIdclear" class="inputclear" style="display:none;">Clear</button>
			<div class="division">
				<input type="hidden" id="hiddenemailId" name = "hiddenemailId" value="" />
				<input type="hidden" id="hiddenemailIdtext" name = "hiddenemailIdtext" value="" />
				<?php echo $this->form->emailId; ?>
				<span id="errors-<?php echo $this->form->emailId->getId(); ?>"
					class="errors"><?php echo isset($this->msgarray['emailId'])?$this->msgarray['emailId']:"";?>
				</span>
			</div>
		</div>

		<div class="new-form-ui">
			<label><?php echo $this->form->logindate->getLabel(); ?> </label><button type="button" id="logindateclear" name="logindateclear" class="inputclear" style="display:none;">Clear</button>
			<div class="division">
			<?php echo $this->form->logindate; ?>
				<span id="errors-<?php echo $this->form->logindate->getId(); ?>"
					class="errors"><?php echo isset($this->msgarray['logindate'])?$this->msgarray['logindate']:"";?>
				</span>
			</div>
		</div>

		<div class="new-form-ui">
			<label><?php echo $this->form->ipaddress->getLabel(); ?> </label><button type="button" id="ipaddressclear" name="ipaddressclear" class="inputclear" style="display:none;">Clear</button>
			<div class="division">
				<input type="hidden" id="hiddenipaddress" name = "hiddenipaddress" value="" />
				<input type="hidden" id="hiddenipaddresstext" name = "hiddenipaddresstext" value="" />
				<?php echo $this->form->ipaddress; ?>
				<span id="errors-<?php echo $this->form->ipaddress->getId(); ?>"
					class="errors"><?php echo isset($this->msgarray['ipaddress'])?$this->msgarray['ipaddress']:"";?>
				</span>
			</div>
		</div>
		
			 <input type="hidden" id="pageno" name="pageno" value="<?php echo $this->pageNo;?>" />
			<input type="hidden" id="totalcount" name="totalcount" value="<?php echo $this->totalCount;?>" /> 
			<input type="hidden" id="lastpageno" name="lastpageno" value="<?php echo $this->lastPageNo?>" /> 
			<input type="hidden" id="sortby" name="sortby" value="<?php echo $this->sortBy?>" />
			<input type="hidden" id="order" name="order" value="<?php echo $this->order;?>" />
			<input type="hidden" id="generatereport" name="generatereport" value="" /> 
			<input type="hidden" id="per_page" name="per_page" value="<?php echo $this->perPage;?>" />
			<input type="hidden" id="fields" name="fields" value="<?php echo $this->tabkeys;?>" />
	        <input type="hidden" id="idhid_allparams" value="" />
	       
		</div>

		<div class="new-form-ui-submit mrgetop5">
			<button type="button" id="previewreport" title="Generate Report">Generate Report</button>
			<button type="button" id="clear" name="clear">Reset</button>
		</div>
		       </form>
		</div>

<!--  Table -->
<div class="right-config-ctrl"><div id="userlogpreview" class="all-grid-control"></div></div>

</div> <!-- end of all parameter div -->
</div>

</div>


<script type="text/javascript">
 $(document).ready(function(){
	 emp_role = '';emp_roletext = '';
	 group='';grouptext = '';
	 empid = '';username = '';emailId = '';logindate = '';ipaddress = '';
	 
	 paginationUserLogReport();
	
	
   	$('#employeeIdf').autocomplete({
        source: base_url+"/userloginlog/empidauto",  
        minLength: 2,      
        select: function( event, ui ) 
        {
            var sel_id = ui.item.id;    
            $('#hiddenemployeeIdf').val(ui.item.id); 
            $('#hiddenemployeeIdftext').val(ui.item.value);      
            	
            if(sel_id == ''){
                $('#employeeIdf').val('');
                $('#hiddenemployeeIdf').val('');
                $('#hiddenemployeeIdftext').val('');
                return false;
            }
        }
    }).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
    	return $( "<li>" )
    	.append( "<a>" + item.label + "</a>" )
    	.appendTo( ul );
    	}; 
    	

	$('#username').autocomplete({
        source: base_url+"/userloginlog/empnameauto",  
        minLength: 2,      
        select: function( event, ui ) 
        {
            var sel_id = ui.item.id;           
            $('#hiddenusername').val(ui.item.id);
            $('#hiddenusernametext').val(ui.item.value);  
            	
            if(sel_id == ''){
                $('#username').val('');
                $('#hiddenusername').val('');
                $('#hiddenusernametext').val('');
                return false;
            }
        }
    }).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
    	return $( "<li>" )
    	.append( "<a><img class='flag' src='"+domain_data+"/public/uploads/profile/"+item.profile_img+"' width='28' height='28' onerror=\"this.src='"+domain_data+"/public/uploads/profile/profile_pic.png'\" /><span style='   padding-left: 6px;    position: relative;    top: -8px;    font-size: 14px;'>" + item.label + "</span></a>" )
    	.appendTo( ul );
    	}; 
		

    $('#ipaddress').autocomplete({
        source: base_url+"/userloginlog/empipaddressauto",  
        minLength: 2,      
        select: function( event, ui ) 
        {
            var sel_id = ui.item.id;   
            $('#hiddenipaddress').val(ui.item.id);
            $('#hiddenipaddresstext').val(ui.item.value);        
         		
            if(sel_id == ''){
                $('#ipaddress').val('');
                $('#hiddenipaddress').val(''); 
                $('#hiddenipaddresstext').val('');
                return false;
            }
        }
    }).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
    	return $( "<li>" )
    	.append( "<a>" + item.label + "</a>" )
    	.appendTo( ul );
    	}; 

     $('#emailId').autocomplete({
          source: base_url+"/userloginlog/empemailauto",  
          minLength: 2,      
          select: function( event, ui ) 
           {
               var sel_id = ui.item.id; 
               $('#hiddenemailId').val(ui.item.id); 
               $('#hiddenemailIdtext').val(ui.item.value);          
    	         		
               if(sel_id == ''){
                  $('#emailId').val('');
                  $('#hiddenemailId').val(''); 
                  $('#hiddenemailIdtext').val(''); 
                  return false;
               }
            }
      }).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
        	return $( "<li>" ).append( "<a>" + item.label + "</a>" ).appendTo( ul );
      }; 


     $('#pdf').click(function(){
    	 $('#generatereport').val('pdf');
    	 $.blockUI({ width:'50px',message: $("#spinner").html() });		   
                   data = $('#idhid_allparams').val();
		     $.ajax({
					type: "POST",
					url: base_url +'/reports/userlogreport',
					data: data+'&generatereport=pdf',
					success: function(response) {
						response = JSON.parse(response);
						url = base_url +'/reports/downloadreport/file_name/' + response.file_name;
					    var $preparingFileModal = $("#preparing-file-modal");

				        $preparingFileModal.dialog({ modal: true });

				        $.fileDownload(url, {
				            successCallback: function(url) {

				            	$.unblockUI();
				            },
				            failCallback: function(responseHtml, url) {

				            	$.unblockUI();
				                jAlert('Download of the report failed');
				            }
				        });
				        return false; //this is critical to stop the click event which will trigger a normal file download!
				        $('#generatereport').val('');
					}
				});
    });

   $('#xcel').click(function(){
           var param_str = $('#idhid_allparams').val();
        var url= base_url+"/reports/userlogreport?"+param_str+"&generatereport=xcel"; 	
	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();
   });

   $('#clear').click(function(){
		//clear all fields
		 emp_role = '';emp_roletext = '';
	 group='';grouptext = '';
	 empid = '';username = '';emailId = '';logindate = '';ipaddress = '';
	 
       $('#emp_role').val('');
       $('#s2id_emp_role .select2-choice span').html('Select Employee Role');
       $('#group').val('');
       $('#s2id_group .select2-choice span').html('Select Group');
       $('#employeeIdf').val('');
       $('#hiddenemployeeIdf').val('');
       $('#hiddenemployeeIdftext').val('');
       $('#username').val('');
       $('#hiddenusername').val('');
       $('#hiddenusernametext').val('');
       $('#emailId').val('');
       $('#hiddenemailId').val('');
       $('#hiddenemailIdtext').val('');
       $('#logindate').val('');
       $('#ipaddress').val('');
       $('#hiddenipaddress').val('');
       $('#hiddenipaddresstext').val('');
       $('#pageno').val('1');
       $('#sortby').val('logindatetime');
       $('#order').val('desc');
       $('#per_page').val('<?php echo PERPAGE;?>');
       $('#fields').val('userfullname,employeeId,group_id,emprole,emailaddress,empipaddress,logindatetime');
       

       $('.inputclear').hide();
       
       paginationUserLogReport();		
	});
   var yearrange = <?php echo "'".date("Y", strtotime("-10 years")).':'.date('Y')."'"; ?>;

	//var yearrange = <?php echo "'".date("Y", strtotime("-30 years")).':'.date('Y')."'"; ?>;
	   $('#logindate').datepicker({
					showOn:'both',
					maxDate: new Date, 
					yearRange: yearrange,
					dateFormat: '<?php echo DATEFORMAT_JS;?>',
					buttonImage: "<?php echo $this->baseUrl("public/media/images/cal.jpg");?>",
					buttonImageOnly: true,
					buttonText: "",
					changeMonth: true,
					changeYear: true, 
	                                showButtonPanel: true ,
					     onClose:function(){
						$('#pageno').val('1');
	                                }
		});

	   
	    
	   $("#previewreport").click(function(){ 	
		   $('#pageno').val('1');
	       $('#sortby').val('logindatetime');
	       $('#order').val('desc');
	       $('#per_page').val('<?php echo PERPAGE;?>');	
	       $('#fields').val('userfullname,employeeId,group_id,emprole,emailaddress,empipaddress,logindatetime');	


	       emp_role = $('#emp_role').val();
		   emp_roletext = $("#emp_role option:selected").text();			
			 if(emp_role != ''){ 
				$('#emp_roleclear').show();		    
			 }
			 
			group = $('#group').val(); 
		    grouptext = $("#group option:selected").text();		  
			if(group != ''){ 
				$('#groupclear').show();
				
			}
			
		    empid = $('#employeeIdf').val(); 
		    if(empid != ''){ 
			    $('#employeeIdfclear').show();
			}
		    
		    username =  $('#username').val();
		     if(username != ''){ $('#usernameclear').show();}
		     
		    emailId =  $('#emailId').val(); if(emailId != ''){ $('#emailIdclear').show();}
		    
		    logindate = $('#logindate').val(); if(logindate != ''){ $('#logindateclear').show(); }
		    
		    ipaddress = $('#ipaddress').val(); if(ipaddress != ''){ $('#ipaddressclear').show();}		    


	       
		   paginationUserLogReport();		    
		});

	 
	    
	   $("#userlogpreview").on('click','.sort',function(){

		   	  order =  $('#order').val(); 
		   	  
		      $('#sortby').val($(this).attr('id'));
		      if(order == "desc" || order == "Desc"){    	  
		    	  $('#order').val('asc');
		      }else{
		    	  $('#order').val('desc');
		      }
		      
		      paginationUserLogReport();
	   });

	
	    $('.pagination').jqPagination({
	         link_string	: '/?page={page_number}',
	         current_page    : parseInt($('#pageno').val()),
	         max_page	: parseInt(<?php echo $this->lastPageNo?>),
	         paged		: function(page) {
	                
	                 $('#pageno').val(page);
	                 paginationUserLogReport();
	               
	         }
	    });

	    $('.inputclear').click(function(){
            var clearid = $(this).attr('id');
            var inputid = jQuery.trim(clearid.replace("clear", "")); 
           
            $('#'+inputid).val(''); 
            if(inputid == 'emp_role'){
            	 emp_role = '';emp_roletext = '';
            	 
            	 $('#s2id_emp_role .select2-choice span').html('Select Employee Role');
            } 
            if(inputid == 'group'){
            	group='';grouptext = '';
           	
           	 $('#s2id_group .select2-choice span').html('Select Group');
            }
            if(inputid == 'employeeIdf'){
            	 empid = '';
            	 $('#hiddenemployeeIdf').val('');
                 $('#hiddenemployeeIdftext').val('');
            }
            if(inputid == 'username'){
            	username = '';
            	$('#hiddenusername').val('');
                $('#hiddenusernametext').val('');
            }
            if(inputid == 'emailId'){
            	emailId = '';
            	$('#hiddenemailId').val('');
                $('#hiddenemailIdtext').val('');
            }
            if(inputid == 'logindate'){
            	logindate = '';
            }
            if(inputid == 'ipaddress'){
            	ipaddress = '';
            	$('#hiddenipaddress').val('');
                $('#hiddenipaddresstext').val('');
            }
            
            $(this).hide();
            $('#pageno').val('1');
            paginationUserLogReport();
		});

		function displaytabs()
	    {
	      if($("#userlogtabdiv").is(":visible"))
	      {
	  		$("#userlogtabdiv").slideUp();
	  	}
	  	else
	      {
	  		$("#userlogtabdiv").slideDown();
	  	}
	  		
	    }

});

 function changeelement(ele){
    var id;
	if(ele.selectedIndex != -1){
	 id = ele[ele.selectedIndex].value;
	}else{
		id = '';
    }
	if(id == ''){
	  //$("#"+ele.id+'clear').hide();
	}		
 }

 function blurelement(ele){
   if(ele.value == ''){
	  // $("#"+ele.id+'clear').hide();
	}		
 }

 function clearautocompleteuserlog(ele)
	{
		var elementid = $(ele).prop('id');	    
		var elementvalue = $('#'+elementid).val();
		if(elementvalue != '')
		{
			var text = '';
			hiddenname = $('#hidden'+elementid).val(); 	
			if(elementid == 'employeeIdf')
			{
				text = $('#hiddenemployeeIdftext').val();
				if(hiddenname =='' && text != elementvalue)
				{
					$("#"+elementid).val('');
					$('#employeeIdfclear').hide();
					$('#hiddenemployeeIdftext').val('');				
				}else if(text != elementvalue){
					$("#"+elementid).val(text);
				}
			}else if(elementid == 'username'){
				text = $('#hiddenusernametext').val();
				if(hiddenname =='' && text != elementvalue)
				{
					$("#"+elementid).val('');
					$('#usernameclear').hide();
					$('#hiddenusernametext').val('');				
				}else if(text != elementvalue){
					$("#"+elementid).val(text);
				}
			}else if(elementid == 'emailId'){
				text = $('#hiddenemailIdtext').val();
				if(hiddenname =='' && text != elementvalue)
				{
					$("#"+elementid).val('');
					$('#emailIdclear').hide();
					$('#hiddenemailIdtext').val('');				
				}else if(text != elementvalue){
					$("#"+elementid).val(text);
				}
			}else if(elementid == 'ipaddress'){
				text = $('#hiddenipaddresstext').val();
				if(hiddenname =='' && text != elementvalue)
				{
					$("#"+elementid).val('');
					$('#ipaddressclear').hide();
					$('#hiddenipaddresstext').val('');				
				}else if(text != elementvalue){
					$("#"+elementid).val(text);
				}
			}
			else 
			{
				text = '';
			}
		}  
	}

</script>


