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


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 
{ 
	if($this->flag == 1) 
	{
		$appraisalid = $this->checkActiveApp['id'];
		$performance_app_flag = '';		
	}
	else
	{
		$appraisalid = $this->appraislaid;
		$performance_app_flag = $this->performanceappflag;
	}
?>

<input type="hidden" id="appraisalid" name="appraisalid" value="<?php echo $appraisalid;?>" >
<input type="hidden" id="perf_app_flag" name = "perf_app_flag" value="<?php echo $performance_app_flag;?>">
<?php if($this->flag == 1) {?>
	<div class="new-form-ui">
		<label >Business Unit</label>
		<div class="division">
			<select id="bunit_id" name="bunit_id" onchange="checkappratings('1',this.value)">
			<option  value="">Select Business Unit</option>
			<?php if(!empty($this->bunitdataarr)) { 
				 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="checkappratings('2',this.value)">
				<option  value="">Select Department</option>
				</select>
				<span class="errors" id="errors-dept_id"></span>
		</div>
	</div>
<?php } else {?>
	<div class="new-form-ui">
		<label >Business Unit</label>
		<div class="division">
			<select id="bunit_id" name="bunit_id" >
			<?php 
				if($this->buOptions !='')
				{
					echo $this->buOptions;
				}
			?>
			</select>
			<span class="errors" id="errors-bunit_id"></span>
		</div>	
	</div>
	<?php if($this->performanceappflag == 0) { ?>
		<div class="new-form-ui" id="deptdiv">
			<label >Department </label>
			<div class="division">
				<select id="dept_id" name="dept_id" >
					<?php 
						if($this->deptOptions !='')
						{
							echo $this->deptOptions;
						}
					?>
				</select>
				<span class="errors" id="errors-dept_id"></span>
			</div>
		</div>
	<?php } ?>

<?php }?>
<div id="employeeresponsediv" style="display:none;" class="employeeresponsediv"></div>
<script type="text/javascript">
function displayappraisalratings(flag)
{
	var appraisalid = $("#appraisalid").val();
	var bunitid = $("#bunit_id").val();
	var deptid = $("#dept_id").val();
	var perf_app_flag = $("#perf_app_flag").val();
	if(flag == 1)
		DATA = 'appraisalid='+appraisalid+'&flag='+flag;
	else
		DATA = 'bunitid='+bunitid+'&deptid='+deptid+'&flag='+flag+'&perf_app_flag='+perf_app_flag;
	$.ajax({
		url: base_url+"/appraisalratings/addratings/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 checkappratings(flag,id)
{
	if(flag == 1)
	{
		$("#employeeresponsediv").html('');
		$('#s2id_dept_id').find('span').html('Select Department');
		if(id)
		{
			$("#dept_id").html('');
			$("#errors-bunit_id").html("");
			$("#errors-dept_id").html("");
			$.ajax({
	         	url: base_url+"/appraisalstatus/checkappraisalimplementation/format/json",
	         	type : 'POST',	
				data : 'buid='+id,
				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);
						displayappraisalratings(2);
					}
					if($.trim(response['msg']) == 'true')
					{
						$("#dept_id").html(response['result']);
					}
					else
					{
						$("#errors-dept_id").removeClass('errors').addClass('add-coloum');
						$("#errors-dept_id").html("Appraisal settings is configured business unit wise.");
					}
				}
	    	});
		}
	}
	else
	{
		$("#employeeresponsediv").html('');
		if(id)
		{
			displayappraisalratings(2);
		}	
	}	
}


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

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

</script>
<?php }?>