<?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 
$form = $this->form;
sapp_Global::generateClientValidations($form);
$messages = $this->messages;
$default_sort_name = "ins.modified_date";
$default_sort_type = "DESC";
?>
<script type="text/javascript" src="<?php echo SCRIPTS_PATH;?>/jquery.fileDownload-master/src/Scripts/jquery.fileDownload.js"></script>
<?php 
echo $this->reportsheader('requisitionstatusreport');
?>

<div class="export-links">
      <div class="sprite export-xls" id="export_excel" >Export to Excel</div>
      <div class="sprite export-pdf" id="pdf">Export to PDF</div>  
</div>
<div id="all_params_div">
<div class="left-config-ctrl">
<div class="heading-name-1">Generate Custom Report</div>
<form name="<?php echo $form->getName(); ?>" id="<?php echo $form->getId(); ?>" action="javascript: void(0);" method="<?php echo $form->getMethod(); ?>">
<div class="total-form-controller-left">  
    <div id="filters_div">
        <div class="new-form-ui">
            <label><?php echo $form->interviewer_id->getLabel() ;?></label>
            <button style="display:none;" class="inputclear" id="idinterviewer_id_clear" data-ids="idinterviewer_id,interviewer_id,hid_interviewer_name" type="button">Clear</button>
            <div class="division"><span class="textarea-data-show"><?php echo $form->interviewer_id;?></span></div>     
            <input type="hidden" id="interviewer_id" name="interviewer_id" />
            <input type="hidden" id="hid_interviewer_name"  />
<?php 
        if(isset($messages['interviewer_id']))
        {
?>
            <span id="errors-<?php echo $form->interviewer_id->getId(); ?>" class="errors"><?php echo $messages['interviewer_id'];?></span>
<?php 
        }
?> 
        </div>
        <div class="new-form-ui">
            <label><?php echo $form->req_id->getLabel() ;?></label>
            <button style="display:none;" class="inputclear" id="req_id_clear" data-ids="req_id" type="button">Clear</button>
            <div class="division"><span class="textarea-data-show"><?php echo $form->req_id;?></span></div>                    
<?php 
        if(isset($messages['req_id']))
        {
?>
            <span id="errors-<?php echo $form->req_id->getId(); ?>" class="errors"><?php echo $messages['req_id'];?></span>
<?php 
        }
?>             
        </div>
        <div class="new-form-ui">
            <label><?php echo $form->interview_date->getLabel() ;?></label>
            <button style="display:none;" class="inputclear" id="interview_date_clear" data-ids="interview_date" type="button">Clear</button>
            <div class="division"><span class="textarea-data-show"><?php echo $form->interview_date;?></span></div>                    
        </div>
        <div class="new-form-ui">
            <label><?php echo $form->department_id->getLabel() ;?></label>
            <button style="display:none;" class="inputclear" id="department_id_clear" data-ids="department_id" type="button">Clear</button>
            <div class="division"><span class="textarea-data-show"><?php echo $form->department_id;?></span></div>                    
<?php 
        if(isset($messages['department_id']))
        {
?>
            <span id="errors-<?php echo $form->department_id->getId(); ?>" class="errors"><?php echo $messages['department_id'];?></span>
<?php 
        }
?>             
        </div>
        <div class="new-form-ui">
            <label><?php echo $form->createdby->getLabel() ;?></label>
            <button style="display:none;" class="inputclear" id="idcreatedby_clear" data-ids="idcreatedby,created_by,hid_createdby_name" type="button">Clear</button>
            <div class="division"><span class="textarea-data-show"><?php echo $form->createdby;?></span></div>     
            <input type="hidden" id="created_by" name="created_by" />
            <input type="hidden" id="hid_createdby_name"  />
<?php 
        if(isset($messages['createdby']))
        {
?>
            <span id="errors-<?php echo $form->createdby->getId(); ?>" class="errors"><?php echo $messages['createdby'];?></span>
<?php 
        }
?> 
        </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><!-- end of filters div -->
    <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" value="Reset" id="idbtnreset">Reset</button>
    </div>
</div>
</form>
</div>
<div class="right-config-ctrl"><div id="idget_report_content" class="all-grid-control"></div></div>
</div><!-- for all parameters -->
</div>
</div>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
	interviewer_id = '';interviewer_idtext = '';req_id = '';req_idtext = '';interview_date = '';department_id = '';department_idtext = '';createdby = '';createdbytext = '';
    interviewreport_ajax();
    
    $('.inputclear').click(function(){
        var ids_arr = $(this).attr('data-ids').split(',');
        $.each( ids_arr, function( key, value ) {
            $('#'+value).val('');
           
            if(value == 'req_id'){
            	req_id = '';req_idtext = '';
                $('#s2id_req_id').find('a.select2-choice').find('span').html('Select Requisition Code');
            }
            if(value == 'department_id'){
            	department_id = '';department_idtext = '';
                $('#s2id_department_id').find('a.select2-choice').find('span').html('Select Department');
            }  
            if(value == 'interviewer_id'){
            	interviewer_id = '';interviewer_idtext = '';
            }
            if(value == 'createdby'){
            	createdby = '';createdbytext = '';
            } 
            if(value == 'interview_date'){
            	interview_date = '';
            }     
        });
        $('#idsubmitbutton').trigger('click');
        $(this).hide();
        
    });
    $('#idcreatedby').change(function(){
        iround_helper();
    });
   $('#idinterviewer_id').change(function(){
        iround_helper();
    }); 
    $('#idcreatedby').autocomplete({
        source: base_url+"/reports/empauto",        
        minLength: 1,
        select: function( event, ui ) 
        {
            var sel_id = ui.item.id;
            $('#created_by').val(ui.item.id);
            $('#hid_createdby_name').val(ui.item.value);
            if(sel_id == '')
            {
                $('#created_by').val('');
                $('#hid_createdby_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+"' 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 );
        }
        else 
        {
            return $( "<li>" )
            .append( "<a>" + item.label + "</a>" )
            .appendTo( ul );
        }
    };
    $('#idinterviewer_id').autocomplete({
        source: base_url+"/reports/empauto",        
        minLength: 1,
        select: function( event, ui ) 
        {
            var sel_id = ui.item.id;
            $('#interviewer_id').val(ui.item.id);
            $('#hid_interviewer_name').val(ui.item.value);
            if(sel_id == '')
            {
                $('#interviewer_id').val('');
                $('#hid_interviewer_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+"' 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 );
        }
        else 
        {
            return $( "<li>" )
            .append( "<a>" + item.label + "</a>" )
            .appendTo( ul );
        }
    };
    $( "#interview_date" ).datepicker({
            showOn: "both",
            buttonImage: "<?php echo MEDIA_PATH;?>images/cal.png",
            buttonImageOnly: true,
            buttonText: "",
            changeMonth: true,
            changeYear: true,
            dateFormat:"<?php echo DATEFORMAT_JS;?>",
            showButtonPanel: true ,
            onClose:function(){
                
                $(this).trigger('blur');
            }
        });
    $('#idbtnreset').click(function(){
    	interviewer_id = '';interviewer_idtext = '';req_id = '';req_idtext = '';interview_date = '';department_id = '';department_idtext = '';createdby = '';createdbytext = '';
    	
        $('#s2id_req_id').find('a.select2-choice').find('span').html('Select Requisition Code');
        $('#s2id_department_id').find('a.select2-choice').find('span').html('Select Department');        
        $('#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();
        });
        $('.cls_clmchk').remove();        
        interviewreport_ajax();
    });
    //for export to excel
    $('#export_excel').click(function(){
        var param_str = $('#id_param_string').val();
        var url= base_url+"/default/reports/exportinterviewrpt?"+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/reports/interviewrptpdf?"+param_str; 	
	$.ajax({
                type: "POST",
                url: base_url +'/default/reports/interviewrptpdf',
                data: param_str,
                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) 
                            {
                                $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!
                },
        });
    });
    $('#idsubmitbutton').click(function(){                           
        var chk_len = $('.cls_clmchk:checked').length;
        $('#errors-chkclm').remove();
        if(chk_len > 0)
        {
            iround_helper();
            interviewreport_ajax('button');
        }
        else 
        {
            jAlert("Please check atleast one column.");
            $( '#selectfields').addClass("config-up");
            displaytabs();
            return false;
        }
    });            
});//end of ready function
function iround_helper()
{
    if($('#interviewer_id').val() != '')
    {
        $('#idinterviewer_id').val($('#hid_interviewer_name').val());
    }
    else 
    {
        $('#idinterviewer_id').val('');
        $('#hid_interviewer_name').val('');
    }
    
    
    if($('#created_by').val() != '')
    {
        $('#idcreatedby').val($('#hid_createdby_name').val());
    }
    else 
    {
        $('#idcreatedby').val('');
        $('#hid_createdby_name').val('');
    }
            
}
function interviewreport_ajax(flag)
{
    if(flag == 'button'){
    	$('#page_no').val('1');
    	interviewer_id = $('#interviewer_id').val();
    	interviewer_idtext=$('#hid_interviewer_name').val();
    	req_id = $('#req_id').val();
    	req_idtext = $("#req_id option:selected").text();
    	interview_date = $('#interview_date').val();
    	department_id = $('#department_id').val();
    	department_idtext = $("#department_id option:selected").text();
    	createdby = $('#created_by').val(); 
    	createdbytext=$('#hid_createdby_name').val();
    }else{
    	if($('#idinterviewer_id_clear').is(':visible'))
	  	 {
	  		 $('#interviewer_id').val(interviewer_id);
	  		 $('#hid_interviewer_name').val(interviewer_idtext);	
	  		 $('#idinterviewer_id').val(interviewer_idtext);
	  	 }else{
	  		$('#interviewer_id').val('');
	  		 $('#hid_interviewer_name').val('');	
	  		 $('#idinterviewer_id').val('');
		 }
    	if($('#req_id_clear').is(':visible'))
	  	 {
	  		 $('#req_id').val(req_id);
	  		 $('#s2id_req_id .select2-choice span').html(req_idtext);		
	  	 }else{
	  		 $('#req_id').val('');
	  		 $('#s2id_req_id .select2-choice span').html('Select Requisition Code');
		 }
    	if($('#interview_date_clear').is(':visible'))
	  	 {
	  		 $('#interview_date').val(interview_date);
	  	 }else{
	  		$('#interview_date').val('');
		 }
    	if($('#department_id_clear').is(':visible'))
	  	 {
	  		 $('#department_id').val(department_id);
	  		 $('#s2id_department_id .select2-choice span').html(department_idtext);		
	  	 }else{
	  		$('#department_id').val('');
	  		 $('#s2id_department_id .select2-choice span').html('Select Department');	
		 }
    	if($('#idcreatedby_clear').is(':visible'))
	  	 {
	  		 $('#created_by').val(createdby); 
	  		 $('#hid_createdby_name').val(createdbytext);	
	  		 $('#idcreatedby').val(createdbytext);
	  	 }else{
	  		$('#created_by').val(''); 
	  		 $('#hid_createdby_name').val('');	
	  		 $('#idcreatedby').val('');
		 }
    }
    var param_str = $('#all_params_div').find('select, textarea, input').serialize();
    $('#id_param_string').val(param_str);
    show_clear();
    $.blockUI({ width:'50px',message: $("#spinner").html() });
    var param_str = $('#all_params_div').find('select, textarea, input').serialize();
    
    $.post(base_url+"/default/reports/getinterviewroundsdata/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('idinterviewer_id','req_id','interview_date','department_id','idcreatedby');
    $.each( ele_arr, function( key, value ) {
        
        var eleval = $('#'+value).val();
        if(eleval != '')
        {
            $('#'+value+"_clear").show();
        }
        else 
        {
            $('#'+value+"_clear").hide();
        }
    });
}
</script>