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


if($this->ermsg !='')
		{
?>
     <div class="ml-alert-1-info m1-info-set"><div class="style-1-icon info"></div><?php echo $this->ermsg;?></div>
<?php }else { ?>

<?php if($this->flag == 1) {?>
	<?php if(!empty($this->checkActiveApp)) { ?>
	
	 <?php echo sapp_PerformanceHelper::appraisal_status_dropdown($this->app_status_array,2);?>	
	 <input type="hidden" id="appraisalid" name="appraisalid" value="<?php echo $this->checkActiveApp['id'];?>" >
	 <input type="hidden" id="completedmgrids" name="completedmgrids" value="<?php echo $this->checkActiveApp['manager_ids'];?>" >
	<?php } ?> 
<?php } else {?>

			<div class="new-form-ui">
				<label >Business Unit</label>
				<div class="division">
					<select id="bunit_id" name="bunit_id" onchange="checkempimp('1',this.value)">
					<option  value="">Select Business Unit</option>
					<?php if(!empty($this->bunitdataarr)) { ?>
					<?php 	foreach($this->bunitdataarr as $value) {
					?>
									<option  value="<?php echo $value['id']?>"><?php echo $value['unitname']?></option>
					<?php }}?>
					</select>
					<span class="errors" id="errors-bunit_id"></span>
				</div>	
			</div>
			
			<div class="new-form-ui" id="deptdiv">
            <label >Department <img class="tooltip" title="Select business unit to populate departments" src="<?php echo $this->baseUrl().'/public/media/';?>images/help.png"></label>
            <div class="division">
					<select id="dept_id" name="dept_id" onchange="checkempimp('2',this.value)">
					<option  value="">Select Department</option>
					</select>
					<span class="errors" id="errors-dept_id"></span>
			</div>
        	</div>
        	
        	<?php echo sapp_PerformanceHelper::appraisal_status_dropdown($this->app_status_array,1);?>
        	
        	
        	<input type="hidden" id="perf_app_flag" name = "perf_app_flag" value="">
<?php } ?>
	
<input type="hidden" id="app_status_val" value="">

<div id="employeeresponsediv" style="display:none;" class="employeeresponsediv">
</div>

<script type="text/javascript">
function displappstatus(flag,value)
{
	var bunitid = $("#bunit_id").val();
	var deptid = $("#dept_id").val();
	var perf_app_flag = $("#perf_app_flag").val();
	$("#errors-bunit_id").html('');
	$("#errors-dept_id").html('');

	if(value)
	{
		$("#appraisalstatusclear").show();
		if(flag==1)
		{		
			if(bunitid == '')
			{
				$("#errors-bunit_id").html("Please select business unit");
				$("#appraisalstatusclear").hide();
				$('#appraisal_status').val('');
				$('#s2id_appraisal_status .select2-choice span').html('Select Appraisal Status');
			}
			else
			{
				if(perf_app_flag == 0)
				{
					if(deptid == '')
					{
						$("#errors-dept_id").html("Please select department");
						$("#appraisalstatusclear").hide();
						$('#appraisal_status').val('');
						$('#s2id_appraisal_status .select2-choice span').html('Select Appraisal Status');
					}
					else
					{
						$('#app_status_val').val(value);
						displayemployeetatus(2);
					}	
				}
				else
				{
					$('#app_status_val').val(value);
					displayemployeetatus(2);
				}			
			}
		}
		else
		{
			$('#app_status_val').val(value);
			displayemployeetatus(1);
		}		
	}else
	{
		$('#app_status_val').val('');
		$("#appraisalstatusclear").hide();
		if(flag==1)
			displayemployeetatus(2);
		else
			displayemployeetatus(1);
	}			
}
function displayemployeetatus(flag)
{
	var appraisalid = $("#appraisalid").val();
	var bunitid = $("#bunit_id").val();
	var deptid = $("#dept_id").val();
	var perf_app_flag = $("#perf_app_flag").val();
	var app_status_val = $("#app_status_val").val();

	if(flag == 1)
		DATA = 'appraisalid='+appraisalid+'&flag='+flag+'&app_status='+app_status_val;
	else
		DATA = 'bunitid='+bunitid+'&deptid='+deptid+'&flag='+flag+'&perf_app_flag='+perf_app_flag+'&app_status='+app_status_val;
		        	$.ajax({
			         	url: base_url+"/appraisalstatus/employeestatus/format/html",
			         	type : 'POST',	
						data : DATA,
						dataType: 'html',
						beforeSend: function () {
							$.blockUI({ width:'50px',message: $("#spinner").html() });
						},
						success : function(response){
							$.unblockUI();	
							$("#employeeresponsediv").show();
						    $("#employeeresponsediv").html(response);
							
						}
		        	});
		
}

function checkempimp(flag,id)
{
	if(flag == 1)
	{	
		$("#employeeresponsediv").html('');
		$("#appraisalstatusclear").hide();
		$('#s2id_dept_id').find('span').html('Select Department');
		$('select#appraisal_status').select2("val", "");
		$('#app_status_val').val('');
		if(id)
		{
			$("#errors-bunit_id").html("");
			$("#errors-dept_id").html("");
			$.ajax({
	         	url: base_url+"/appraisalstatus/checkappraisalimplementation/format/json",
	         	type : 'POST',	
				data : 'buid=' + id + '&enable_step=2',
				dataType: 'json',
				beforeSend: function () {
					$.blockUI({ width:'50px',message: $("#spinner").html() });
				},
				success : function(response){
					$.unblockUI();	
					if($.trim(response['flag']) == 'notinitialized')
					{
						$("#errors-bunit_id").html("Performance appraisal is not initialized.");
					}
					if($.trim(response['flag']) == 'deptwise')
					{
						$("#perf_app_flag").val(0);
						
					}
					if($.trim(response['flag']) == 'buwise')
					{
						$("#perf_app_flag").val(1);
						displayemployeetatus(2);
					}
					
					if($.trim(response['msg']) == 'true')
					{
						
						$("#dept_id").html(response['result']);
					}else
					{
						$("#errors-dept_id").removeClass('errors').addClass('add-coloum');
						$("#errors-dept_id").html("No departments found as Appraisal settings is configured business unit wise.");
					}
					
					
				}
	    	});
			
		}	
	}else
	{
		$("#employeeresponsediv").html('');
		if(id)
		{
			displayemployeetatus(2);
		}	
	}	
}

function clearappstatus(flag)
{
	$('select#appraisal_status').select2("val", "");
	displappstatus(flag,'');
}


$(document).ready(function() {
	<?php if($this->flag ==1) { ?>
	displayemployeetatus('<?php echo $this->flag; ?>');
	<?php }?>

	$('.employeeresponsediv').alternateScroll({ 'horizontal-bar-class': 'styled-h-bar'});
	

});

</script>
<?php }?>