<?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" src="<?php echo SCRIPTS_PATH;?>/jquery.fileDownload-master/src/Scripts/jquery.fileDownload.js"></script>
<?php 
// Code was replicated from views/scripts/reports/employeereort.phtml
//echo $this->reportsheader('employeereport');
$form = $this->form;
sapp_Global::generateClientValidations($form);
$messages = $this->messages;

$default_sort_name = "modifieddate";
$default_sort_type = "DESC";
?>

<div class="reports-grid-ctrl" style="width: 100%;">
<div id="error_message" style="display:none;"></div>
<div class="reports-back-btn-div" style="margin-right: 16px;">
<a href="<?php echo BASE_URL;?>/myemployees" class="sprite reports-back-btn">Back</a>
</div>

<div class="export-links">

<div class="fltright">

<div class="sprite export-xls" id="export_excel" >Export to Excel</div>
<div class="sprite export-pdf" id="pdf">Export to PDF</div>
<div class="no_of_emp">My Team Count : <?php echo $this->count_emp_reporting;?></div>
</div>
</div>
<div id="all_param_div">
<div class="left-config-ctrl">
<div class="heading-name-1">Generate Custom Report</div>
<div class="total-form-controller-left">  
    <div id="filters_div">
    <form name="<?php echo $this->form->getName(); ?>" id="<?php echo $this->form->getId(); ?>" action="javascript: void(0)" method="<?php echo $this->form->getMethod(); ?>" accept-charset=utf-8>
	<div class="new-form-ui">
		<label><?php echo $form->hidempId->getLabel();?></label>
		<button style="display:none;" class="inputclear" id="employeeId_clear" data-ids="employeeId,hid_employeeId,hidempId" type="button">Clear</button>
		<div class="division"><span class="textarea-data-show"><?php echo $form->hidempId;?></span></div>        
        <input type="hidden" id="employeeId" name="employeeId" />
        <input type="hidden" id="hid_employeeId"  />
	</div>
	<div class="new-form-ui">
		<label><?php echo $form->userfullname->getLabel();?></label>
		<button style="display:none;" class="inputclear" id="userfullname_clear" data-ids="userfullname,iduserfullname,hid_userfullname_name" type="button">Clear</button>
		<div class="division"><span class="textarea-data-show"><?php echo $form->userfullname;?></span></div>        
        <input type="hidden" id="userfullname" name="userfullname" />
        <input type="hidden" id="hid_userfullname_name"  />
	</div>
	<div class="new-form-ui">
		<label><?php echo $form->emailaddress->getLabel();?></label>
		<button style="display:none;" class="inputclear" id="emailaddress_clear" data-ids="emailaddress,idemailaddress,hid_emailaddress_name" type="button">Clear</button>
		<div class="division"><span class="textarea-data-show"><?php echo $form->emailaddress;?></span></div>        
        <input type="hidden" id="emailaddress" name="emailaddress" />
        <input type="hidden" id="hid_emailaddress_name"  />
	</div>	
    <div class="new-form-ui">
        <label><?php echo $form->jobtitle_id->getLabel() ;?></label>
        <button style="display:none;" class="inputclear" id="jobtitle_id_clear" data-ids="jobtitle_id" type="button">Clear</button>
        <div class="division"><span class="textarea-data-show"><?php echo $form->jobtitle_id;?></span></div> 
<?php 
        if(isset($messages['jobtitle_id']))
        {
?>
            <span id="errors-<?php echo $form->jobtitle_id->getId(); ?>" class="errors"><?php echo $messages['jobtitle_id'];?></span>
<?php 
        }
?> 
    </div>
    <div class="new-form-ui">
        <label><?php echo $form->position_id->getLabel() ;?></label>
        <button style="display:none;" class="inputclear" id="position_id_clear" data-ids="position_id" type="button">Clear</button>
        <div class="division"><span class="textarea-data-show"><?php echo $form->position_id;?></span></div>        
<?php 
        if(isset($messages['position_id']))
        {
?>
            <span id="errors-<?php echo $form->position_id->getId(); ?>" class="errors"><?php echo $messages['position_id'];?></span>
<?php 
        }
?>        
    </div>
    
     <div class="new-form-ui">
        <label><?php echo $form->isactive->getLabel() ;?></label>
        <button style="display:none;" class="inputclear" id="isactive_clear" data-ids="isactive" type="button">Clear</button>
        <div class="division"><span class="textarea-data-show"><?php echo $form->isactive;?></span></div> 
    </div>
    
    <input type="hidden" id="page_no" name="page_no" value="1" />
    <input type="hidden" id="per_page" name="per_page" value="<?php echo PERPAGE ;?>" />
    <input type="hidden" id="sort_name" name="sort_name" value="<?php echo $default_sort_name;?>" />
    <input type="hidden" id="sort_type" name="sort_type" value="<?php echo $default_sort_type;?>" />
    <input type="hidden" id="id_param_string" value="" />
    <div class="new-form-ui-submit mrgetop5">
        
        <button type="button" class="previewreport" id="<?php echo $this->form->submit->getId();?>" title="Generate Report">Generate Report</button>
        <button type="button" id="idbtnreset" class="" >Reset</button>
    </div> 
    
     </div><!-- end of filters div -->
     
     
</div>
</div>    
   
<div class="right-config-ctrl"><div id="idget_report_content" class="all-grid-control"></div></div>
</div><!-- all parameters div -->
</form> 
</div>
</div>
</div>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
	reporting_manager = '';reporting_managertext='';department_id = '';department_idtext = '';
        emprole = '';businessunit_id = '';businessunit_idtext = '';
        emproletext = '';jobtitle_id = '';jobtitle_idtext = '';position_id = '';position_idtext = '';emp_status_id = '';
        emp_status_idtext = '';date_of_joining = '';modeofentry = '';modeofentrytext = '';emailaddress='';emailaddresstext='';userfullname='';userfullnametext='';
        employeeid = '';
		employeeidtext='';isactive=''; isactive_text='';
	    
    $('.inputclear').click(function(){
        var ids_arr = $(this).attr('data-ids').split(',');
        $.each( ids_arr, function( key, value ) {
            $('#'+value).val('');
            if(value == 'modeofentry'){
            	modeofentry = '';modeofentrytext = '';
                $('#s2id_modeofentry').find('a.select2-choice').find('span').html('Select Mode of Employment');
            }
            if(value == 'jobtitle_id'){
            	jobtitle_id = '';jobtitle_idtext = '';
                $('#s2id_jobtitle_id').find('a.select2-choice').find('span').html('Select Job Title');
                position_id = '';
                position_idtext = '';
                $("#position_id_clear").hide();
                $("#position_id").html('<option label="Select Position" value="">Select Position</option>');
                $('#s2id_position_id').find('a.select2-choice').find('span').html('Select Position');
            }
            if(value == 'position_id'){
            	position_id = '';position_idtext = '';
                $('#s2id_position_id').find('a.select2-choice').find('span').html('Select Position');
            }
            if(value == 'emp_status_id'){
            	emp_status_id = '';emp_status_idtext = '';
                $('#s2id_emp_status_id').find('a.select2-choice').find('span').html('Select Employment Status');
            }            
            if(value == 'emprole'){
            	emprole = '';emproletext = '';
                $('#s2id_emprole').find('a.select2-choice').find('span').html('Select Role');
            }
            if(value == 'department_id'){
            	department_id = '';department_idtext = '';                
                apply_select2();
                $('#s2id_department_id .select2-search-choice').remove();
            }
            if(value == 'businessunit_id'){
            	businessunit_id = '';businessunit_idtext = '';                
                apply_select2();
                $('#s2id_businessunit_id .select2-search-choice').remove();
                $('#businessunit_id').trigger('change');
            }
            if(value == 'reporting_manager'){
            	reporting_manager = '';reporting_managertext='';
            }
            if(value == 'date_of_joining'){
            	date_of_joining = '';
            }
            if(value == 'emailaddress'){
            	emailaddress = '';emailaddresstext='';
            }
            if(value == 'employeeId'){
            	employeeid = '';employeeidtext='';
            }
            if(value == 'userfullname'){
            	userfullname = '';userfullnametext='';
            }

            if(value == 'isactive'){
            	isactive = '';isactive_text = '';
                $('#s2id_isactive').find('a.select2-choice').find('span').html('Select User Satus');
                
            }            
            
        });
        $('#idsubmitbutton').trigger('click');
        $(this).hide();
        
    });
    $('#idbtnreset').click(function(){
    	reporting_manager = '';reporting_managertext='';department_id = '';department_idtext = '';emprole = '';
        emproletext = '';jobtitle_id = '';jobtitle_idtext = '';position_id = '';position_idtext = '';emp_status_id = '';
        emp_status_idtext = '';date_of_joining = '';modeofentry = '';modeofentrytext = '';businessunit_id = '';
        businessunit_idtext = '';emailaddress='';emailaddresstext='';userfullname='';userfullnametext='';employeeid = '';
		employeeidtext='';isactive = '';isactive_text = '';
    	
        
        apply_select2();
        $('#page_no').val('1');
        $('#per_page').val('<?php echo PERPAGE ;?>');
        $('#sort_name').val('<?php echo $default_sort_name ;?>');
        $('#sort_type').val('<?php echo $default_sort_type ;?>');
        $('.inputclear').each(function(index,value){
            var ids_arr = $(this).attr('data-ids').split(',');
            $.each( ids_arr, function( key, value ) {
                $('#'+value).val('');
            });
            $(this).hide();
        });
        apply_select2();
        $('#s2id_businessunit_id .select2-search-choice').remove();
        $('#s2id_department_id .select2-search-choice').remove();
        $('.cls_clmchk').remove();        
        employeereport_ajax();
    });

    
    $('#iduserfullname').autocomplete({
        source: base_url+"/myemployees/empauto/field/userfullname",        
        minLength: 2,
        select: function( event, ui ) 
        {
            var sel_id = ui.item.id;
            $('#userfullname').val(ui.item.value);
            $('#hid_userfullname_name').val(ui.item.value);
            if(sel_id == '')
            {
                $('#userfullname').val('');
                $('#hid_userfullname_name').val('');
                return false;
            }
        }
    }).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
        if(item.id != '')
        {
            return $( "<li>" )
            .append( "<a><img class='flag' src='"+domain_data+"/public/uploads/profile/"+item.profile_img+"' onerror=\"this.src='"+domain_data+"/public/uploads/profile/profile_pic.png'\" /><span>" + item.label + "</span></a>" )
            .appendTo( ul );
        }
        else 
        {
            return $( "<li>" )
            .append( "<a>" + item.label + "</a>" )
            .appendTo( ul );
        }
	}; 

    
    $('#idemailaddress').autocomplete({
        source: domain_data+"/myemployees/empauto/field/emailaddress",        
        minLength: 2,
        select: function( event, ui ) 
        {
            var sel_id = ui.item.id;
            $('#emailaddress').val(ui.item.value);
            $('#hid_emailaddress_name').val(ui.item.value);
            if(sel_id == '')
            {
                $('#emailaddress').val('');
                $('#hid_emailaddress_name').val('');
                return false;
            }
        }
    }).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
        if(item.id != '')
        {
            return $( "<li>" )
            .append( "<a><img class='flag' src='"+domain_data+"/public/uploads/profile/"+item.profile_img+"' onerror=\"this.src='"+domain_data+"/public/uploads/profile/profile_pic.png'\" /><span>" + item.label + "</span></a>" )
            .appendTo( ul );
        }
        else 
        {
            return $( "<li>" )
            .append( "<a>" + item.label + "</a>" )
            .appendTo( ul );
        }
	}; 	

	$('#hidempId').autocomplete({
        source: domain_data+"/myemployees/empauto/field/employeeId",        
        minLength: 2,
        select: function( event, ui ) 
        {
            var sel_id = ui.item.id;
            $('#employeeId').val(ui.item.value);
            $('#hid_employeeId').val(ui.item.value);
            if(sel_id == '')
            {
                $('#employeename').val('');
				$('#hiddenemployeetext').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+"' onerror=\"this.src='"+domain_data+"/public/uploads/profile/profile_pic.png'\" />" + item.label + "</a>" )
.appendTo( ul );
};
	
//end of report auto
    
    $('#idreporting_manager').change(function(){
        var val = $(this).val();
        if($.trim(val) == '')
        {
            $('#reporting_manager').val('');
        }
        report_manager_help();
    });
    
    $('#idemailaddress').change(function(){
        var val = $(this).val();
        if($.trim(val) == '')
        {
            $('#emailaddress').val('');
        }
        emailaddress_help();
    });
    
    $('#iduserfullname').change(function(){
        var val = $(this).val();
        if($.trim(val) == '')
        {
            $('#userfullname').val('');
        }
        userfullname_help();
    });

    $('#hidempId').change(function(){
        var val = $(this).val();
        if($.trim(val) == '')
        {
            $('#employeeId').val('');
        }
        employeeid_help();
    });
    
    $('#idsubmitbutton').click(function(){        
        var chk_len = $('.cls_clmchk:checked').length;
        $('#errors-chkclm').remove();
        if(chk_len > 0)
        {
            $('#idcols_arr').hide();            
            report_manager_help();
            employeereport_ajax('button');
        }
        else 
        {
            jAlert("Please check atleast one column.");
            $( '#selectfields').addClass("config-up");
            displaytabs();
            return false;
        }
    });
    
    employeereport_ajax();
    
  
    //for export to excel
    $('#export_excel').click(function(){
        
        var param_str = $('#id_param_string').val();
        
        var url= base_url+"/default/myemployees/exportemployeereport?"+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();
    });
    //for export to pdf
   
    $('#pdf').click(function(){
        
        var param_str = $('#id_param_string').val();
        var url= base_url+"/default/myemployees/emprptpdf?"+param_str; 	
	$.ajax({
                type: "POST",
                url: base_url +'/default/myemployees/emprptpdf',
                data: param_str,
                success: function(response) 
                { 
                    response = JSON.parse(response);
                    url = base_url +'/myemployees/downloadreport/file_name/' + response.file_name;
                    var $preparingFileModal = $("#preparing-file-modal");
                    $preparingFileModal.dialog({ modal: true });

                    $.fileDownload(url, {
                            successCallback: function(url) 
                            {
                                $preparingFileModal.dialog('close');
                            },
                            failCallback: function(responseHtml, url) 
                            {
                                $preparingFileModal.dialog('close');
                                $("#error-modal").dialog({ modal: true });
                            }
                        });			        
                    return false; //this is critical to stop the click event which will trigger a normal file download!
                }
        });
    });
    $('#menu-shadow').hide();
    $('.wrapperdivleft').hide();
    $('#mainmenushowhidebutton').hide();

});//end of ready function
function report_manager_help()
{
    if($('#reporting_manager').val() != '')
        $('#idreporting_manager').val($('#hid_reporting_manager_name').val());
    else 
        $('#idreporting_manager').val('');
}
function userfullname_help()
{
    if($('#userfullname').val() != '')
        $('#iduserfullname').val($('#hid_userfullname_name').val());
    else 
        $('#iduserfullname').val('');
}
function emailaddress_help()
{
    if($('#emailaddress').val() != '')
        $('#idemailaddress').val($('#hid_emailaddress_name').val());
    else 
        $('#idemailaddress').val('');
}

function employeeid_help()
{
    if($('#employeeId').val() != '')
        $('#hidempId').val($('#hid_employeeId').val());
    else 
        $('#hidempId').val('');
}
function employeereport_ajax(flag)
{

	if(flag == 'button'){
            $('#page_no').val('1');
		reporting_manager = $('#reporting_manager').val();
		reporting_managertext=$('#hid_reporting_manager_name').val();

		emailaddress = $('#emailaddress').val();
		emailaddresstext=$('#hid_emailaddress_name').val();

		employeeid = $('#employeId').val();
		employeeidtext=$('#hid_employeeId').val();

		userfullname = $('#userfullname').val();
		userfullnametext=$('#hid_userfullname_name').val();
		
		department_id = $('#department_id').val(); department_idtext = $("#department_id option:selected").text();	
                businessunit_id = $('#businessunit_id').val(); businessunit_idtext = $("#businessunit_id option:selected").text();	
		emprole = $('#emprole').val();emproletext = $("#emprole option:selected").text(); 
		jobtitle_id = $('#jobtitle_id').val();jobtitle_idtext = $("#jobtitle_id option:selected").text();
		position_id = $('#position_id').val();position_idtext = $("#position_id option:selected").text();
		emp_status_id = $('#emp_status_id').val();emp_status_idtext = $("#emp_status_id option:selected").text();
		date_of_joining = $('#date_of_joining').val();
		modeofentry = $('#modeofentry').val();modeofentrytext = $("#modeofentry option:selected").text();
		isactive = $('#isactive').val();isactive_text = $("#isactive option:selected").text();
		
	}else{ 
		 if($('#reporting_manager_clear').is(':visible'))
	  	 {
	  		 $('#reporting_manager').val(reporting_manager);
	  		 $('#hid_reporting_manager_name').val(reporting_managertext);	
	  		 $('#idreporting_manager').val(reporting_managertext);
	  	 }
		 if($('#emailaddress_clear').is(':visible'))
	  	 {
	  		 $('#emailaddress').val(emailaddress);
	  		 $('#hid_emailaddress_name').val(emailaddresstext);	
	  		 $('#idemailaddress').val(emailaddresstext);
	  	 }
		 if($('#hidempId_clear').is(':visible'))
	  	 {
	  		 $('#employeId').val(employeeid);
	  		 $('#hid_employeeId').val(employeeidtext);	
	  		 $('#hidempId').val(employeeidtext);
	  	 }	
		 if($('#userfullname_clear').is(':visible'))
	  	 {
	  		 $('#userfullname').val(userfullname);
	  		 $('#hid_userfullname_name').val(userfullnametext);	
	  		 $('#iduserfullname').val(userfullnametext);
	  	 }		  	   	 
		 if($('#department_id_clear').is(':visible'))
	  	 {
                    $('#department_id').val(department_id);
                    
                    apply_select2();
                    
	  	 }
                 if($('#businessunit_id_clear').is(':visible'))
	  	 {
                    $('#businessunit_id').val(businessunit_id);
                    
                    apply_select2();
	  	 }
		 if($('#emprole_clear').is(':visible'))
	  	 {
	  		 $('#emprole').val(emprole);
	  		 $('#s2id_emprole .select2-choice span').html(emproletext);		
	  	 }
		 if($('#jobtitle_id_clear').is(':visible'))
	  	 {
	  		 $('#jobtitle_id').val(jobtitle_id);
	  		 $('#s2id_jobtitle_id .select2-choice span').html(jobtitle_idtext);		
	  	 }
		 if($('#position_id_clear').is(':visible'))
	  	 {
	  		 $('#position_id').val(position_id);
	  		 $('#s2id_position_id .select2-choice span').html(position_idtext);		
	  	 }
		 if($('#emp_status_id_clear').is(':visible'))
	  	 {
	  		 $('#emp_status_id').val(emp_status_id);
	  		 $('#s2id_emp_status_id .select2-choice span').html(emp_status_idtext);		
	  	 }
		 if($('#date_of_joining_clear').is(':visible'))
	  	 {
	  		 $('#date_of_joining').val(date_of_joining);
	  	 }
		 if($('#modeofentry_clear').is(':visible'))
	  	 {
	  		 $('#modeofentry').val(modeofentry); 
	  		$('#s2id_modeofentry .select2-choice span').html(modeofentry);	
	  	 }
		 if($('#isactive_clear').is(':visible'))
	  	 {
	  		 $('#isactive').val(isactive);
	  		 $('#s2id_isactive .select2-choice span').html(isactive_text);		
	  	 }
		
	}
    var param_str = $('#all_param_div').find('select, textarea, input').serialize();
    $('#id_param_string').val(param_str);
    show_clear();
    $.blockUI({ width:'50px',message: $("#spinner").html() });
    var param_str = $('#all_param_div').find('select, textarea, input').serialize();
    $.post(base_url+"/default/myemployees/getempreportdata/format/html",param_str,function(data){
        $('#idget_report_content').html(data);
        $.unblockUI();
    },'html');
}
function displaytabs()
{
    if($('#selectfields').hasClass("config-up"))		
    {
        $( '#selectfields').removeClass("config-up");
        $( '#selectfields').addClass("config-down");
        $('#idcols_arr').slideDown();	

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

    }
    else
    {
        $( '#selectfields').removeClass("config-down");
        $( '#selectfields').addClass("config-up");
        $('#idcols_arr').slideUp();
        $('.overlayreport').remove();
    }
}
function show_clear()
{
    var ele_arr = new Array('department_id','emailaddress','employeeId', 'userfullname', 'date_of_joining','modeofentry',
    'jobtitle_id','position_id','emp_status_id','emprole','businessunit_id','isactive');
    $.each( ele_arr, function( key, value ) {
        
        var eleval = $.trim($('#'+value).val());
        
        if(eleval !== '' && eleval !== null)
        {
            $('#'+value+"_clear").show();
        }
        else 
        {
            $('#'+value+"_clear").hide();
        }
    });
}
</script>