<?php 

/********************************************************************************* 
 *  This file is part of Sentrifugo.
 *  Copyright (C) 2015 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>
 ********************************************************************************/
$init_data = $this->init_data;
$exist_data = $this->exist_data;
$init_id = $this->init_id;
$budeptArr =  sapp_Global::getbudeptname($init_id);
$manager_id_arr = array();
if(!empty($exist_data))
{
?>

<div id="spinner" class="ajax-loader" style="display:none;" >
	<img id="img-spinner" src="<?php echo MEDIA_PATH;?>images/loader2.gif" alt="Loading" />				
</div>
<div id="id_acc_message" style="display:none;"></div>
<div id="idaccordion" class="line_manager_grid">
<?php     
    $i=0;
    foreach($exist_data as $edata)
    {
    	$jobtitle_length=strlen($edata['jobtitle_name']);
    	$jobtitle =$edata['jobtitle_name'];
    	
		array_push($manager_id_arr,(isset($edata['user_id'])?$edata['user_id']:0));
        if(($i+1)%2==0)
            $odd_even_class='acc_even';
        else 
            $odd_even_class='acc_odd';        
?>
        <div class="cls_headers <?php echo $odd_even_class;?>">
			<div class="acc_emp_list">
				<div class="emp_details_li">	
					<div class="profile_img"><img src="<?php echo $this->baseUrl("public/uploads/profile/").$edata['profileimg'];?>" width="48px"  height="48px" onerror="this.src='<?php echo MEDIA_PATH; ?>images/default-profile-pic.jpg'" /></div> 
					<div class="config_emp_div">
					<div  class="name_disply approvalpending_name_dis" id="idname_display<?php echo $i;?>" title="<?php echo $edata['userfullname'];?>"><?php echo $edata['userfullname'];?></div>       
					<span class="emp_id"><?php echo $edata['employeeId'];?></span>
					<span class="desg_txt" title="<?php echo $edata['jobtitle_name'];?>"><?php echo ($jobtitle_length<=17)?$jobtitle:substr($jobtitle,0,15) . '...';?></span>
				</div>
				</div>			
				<span class="emp_name_span l1_manager_span">L1 Manager</span>
				<input type="hidden" id="idhid_init_<?php echo $i;?>" value="<?php echo $init_id;?>" class="hidinit_ac" />                
				<input type="hidden" id="idhid_manager_<?php echo $i;?>" value="<?php echo $edata['user_id'];?>" class="hidmanager_ac"  />
				<input type="hidden" id="idhid_flag_<?php echo $i;?>" value="<?php echo !empty($edata['manager_levels'])?"view":"add";?>" class="hidflag_ac"  />
				<input type="hidden" id="idhid_manager_levels_<?php echo $i;?>" value="<?php echo $edata['manager_levels'];?>" class="hid_manager_levels" />
			</div>               
			<div class="appri_emp_count">
				<span class="emp_name_span">Employee(s)<span class="emp_count_txt"><?php echo $edata['emp_cnt'];?></span></span>
				<?php             
				if(!empty($edata['manager_levels']))
				{
					if($init_data['status']==1 && $init_data['initialize_status']!=1){ 
				?>
					<span class="emp_name_span">Appraisal Level(s)<span class="emp_count_txt"><?php echo $edata['manager_levels'];?></span></span>
					<div class="action_disply approvalpending_action_dis" >
						<input type="button" name="btn_edit" class="clsbtn_edit" panel-no="<?php echo $i;?>" id="idbtn_edit_<?php echo $i;?>" value="Edit"/>
						<input type="button" name="btn_discard" class="clsbtn_discard" id="idbtn_discard_<?php echo $i;?>" value="Discard" />
					</div>
					<?php } ?>
				<?php 
				}
				?>
			</div>
            <div class="clear"></div>    
        </div>
        <div id="idemps_<?php echo $i;?>"></div>
<?php        
        $i++;
    }
?>
</div>
<?php if(isset($this->active_records) && $this->active_records == 0 && isset($this->active_records_temp) && $this->active_records_temp == 0) 
	{ ?>
	<div class="total-form-controller" style="    padding-top: 0;">
		<div class="new-form-ui-submit	" style="    margin-top: 0;    margin-left: 5px;">
			<input type="submit" name="sbtnsaveorg" id="sbtnsaveorg" value="Save" style="display:none;"/>
			<input type="button" name="btnsaveorg" id="btnsaveorg" value="Save" />
		</div>
	</div>
<?php 
	}
}
else 
{
?>
<div class="ml-alert-1-info m1-info-set-2" style="width: 90%; margin-top: 10px;"><div class="style-1-icon info"></div>No employees to add.</div>
<?php    
}
$manager_ids = '';
if(count($manager_id_arr) > 0)
{
	$manager_ids = implode(',',$manager_id_arr);
}
?>
<input type="hidden" name="manager_ids" id="manager_ids" value="<?php echo $manager_ids;?>" />
<script type="text/javascript">
function displaycontentreportacc(context,ui)
{
    var init_id = ui.newHeader.find('.hidinit_ac').val();  
    var manager_id = ui.newHeader.find('.hidmanager_ac').val();    
    var manager_levels = ui.newHeader.find('.hid_manager_levels').val();
    
    ui.oldPanel.html('');
    if(init_id !== null && manager_id !== null && init_id !== undefined && manager_id !== undefined)
    {             
		$.blockUI({ width:'50px',message: $("#spinner").html() });	
        var params = {init_id:init_id,manager_id:manager_id,type:'<?php echo $init_data['manager_level_type'];?>',context:context,manager_levels:manager_levels,businessunit_id:'<?php echo isset($budeptArr['appdata']['businessunit_id'])?$budeptArr['appdata']['businessunit_id']:0;?>',department_id:'<?php echo isset($budeptArr['appdata']['department_id'])?$budeptArr['appdata']['department_id']:0;?>'};
        $.post(base_url+'/appraisalinit/displaycontentreportacc/format/html',params,function(data){
            ui.newPanel.html(data);
        },'html');                    
    }
    else
    {
        ui.oldPanel.html('');
    }
}
$(document).ready(function()
{
    $( "#idaccordion" ).accordion({
        collapsible: true,
        active:false,
        heightStyle: "content",
        beforeActivate: function( event, ui ) {               
            var event_type = event.originalEvent;
            var context = ui.newHeader.find('.hidflag_ac').val();
            if(event_type == undefined)
                context = 'edit';
            displaycontentreportacc(context,ui);
        },
        header:'.cls_headers'
    });//end of accordion function    
    
    $(".clsbtn_edit,.clsbtn_discard").bind("click", function(event) {
        event.stopPropagation();
    });//these above bindings are for accordion headers means by clicking these buttons accordion will not collapse.
    
    $('.clsbtn_edit').click(function(){        
        var panel_no = parseInt($(this).attr('panel-no'));
        $( "#idaccordion" ).accordion({ active: false });
        $( "#idaccordion" ).accordion({ active: panel_no });
       
        //display_managers('<?php echo $init_id;?>','edit',manager_id);
    });
    $('.clsbtn_discard').click(function(){
        var btn_id = $(this).prop('id');
        jConfirm("Are you sure you want to discard this L1 Manager", "Discard L1 Manager ", function(r) {
            if(r==true)
            {
                var manager_id = $('#'+btn_id).parent().parent().parent().find('.hidmanager_ac').val();
                $.post(base_url+"/appraisalinit/deletereportmanager",{init_id:'<?php echo $init_data['id'];?>',manager_id:manager_id},function(data){
                    if(data.status == 'success')
                    {
						successmessage(data.message);
                        construct_rep_accordion('<?php echo $init_data['id'];?>');
						if(data.active_records_temp == 0 && data.active_records == 0)
						{						
							location.reload();
						}
                        $('#id_acc_message').show();
                        $('#id_acc_message').addClass('success');
                        $('#id_acc_message').html(data.message);
                        $('#id_acc_message').fadeOut('slow','swing',function(){
                            $('#id_acc_message').html('');
                            $('#id_acc_message').removeClass();
                        })
                    }
                },'json');
                
            }
        });
    });//end of btn_discard
	$('#btnsaveorg').click(function(){
		jConfirm("Only L1 managers will be saved. Do you wish to continue?","confirm", function(r) {
			if(r==true)
			{
				$("#sbtnsaveorg").trigger("click");
			}
			else
			{
				return false;
			}
		});
	});
});//end of ready function
</script> 