<?php
include_once("../class/class.MySQL.php");
$db	= new MySQL();
session_start();

$action=$_POST['act'];
if($action=='add_performance_qns')
{
	//$add_form='sfdsdf';
	$attr_array = $db->fetchQuery("SELECT * FROM performance_attribute");
	$attr_combo='<select class="form-control" id="attr_combo" name="attr_combo" onchange="load_attr_details(this.value)">
		<option value="">Select Attribute</option>';
		if(!empty($attr_array))
		{
			foreach($attr_array as $attr_row)
			{
				$attr_combo.='<option value="'.$attr_row['perfom_attr_id'].'">'.$attr_row['attributes'].'</option>';
			}
		}
	$attr_combo.='</select>';
	
	$add_form='<div class="row">
		<form class="AddAttributeForm" role="form">
			<table class="table">
				<tr>
					<td style="width:60%"><label>Select Attribute</label>'.$attr_combo.'</td>
					<td style="width:40%"><a style="cursor:pointer" class="addMoreAttribute"><span class="sub-category-bt"><translate>Add Attribute</translate></span></a></td>
				</tr>
			</table>
			<div class="col-md-12">
				<table class="table table-bordered attr_table" style="display:none">
					<thead>
						<tr>
							<th width="60%">Question</th>
							<th width="20%">Weightage</th>
							<th width="20%">Total Weightage in %: <span id="weightage_total_span"></span><input type="hidden" id="weightage_total" name="weightage_total" /></th>
						</tr>
					</thead>
				</table>
			</div>
		</form>
	</div>';
	echo json_encode(array('add_performace_form'=>$add_form));
}
elseif($action=='add_new_attr_name')
{
	$data['attributes'] = $_POST['attribute'];	
	$db->Insert($data,'performance_attribute');	
	$attribute_id   =   $db->LastInsertID();
	echo $attribute_id;
}
elseif($action=='attr_details')
{
	$details='<tbody>';
		$attr_details = $db->fetchQuery("SELECT * FROM performance_question where perfom_attr_id=".$_POST['attr_id']."");
		$i=1;
		if(!empty($attr_details))
		{
			foreach($attr_details as $attr_row)
			{
				$details.='<tr>
					<td><textarea name="qns_area_'.$i.'" id="qns_area_'.$i.'" class="form-control" rows="3" placeholder="Enter ...">'.$attr_row['question'].'</textarea></td>
					<td colspan="2"><input type="text" name="qns_weightage_'.$i.'" id="qns_weightage_'.$i.'" value="'.$attr_row['weightage'].'" onchange="calculate_weightage()" class="weightage_text form-control numeric_filter" onkeypress="return onlyNos(event,this);" /><input type="hidden" id="qns_id_'.$i.'" name="qns_id_'.$i.'" value="'.$attr_row['perform_qn_id'].'"></td>
				</tr>';
				$i++;
			}
		}
		else
		{
			$details.='<tr>
				<td><textarea name="qns_area_'.$i.'" id="qns_area_'.$i.'" class="form-control" rows="3" placeholder="Enter ..."></textarea></td>
				<td colspan="2"><input type="text" name="qns_weightage_'.$i.'" id="qns_weightage_'.$i.'" onchange="calculate_weightage()" class="weightage_text form-control  numeric_filter" onkeypress="return onlyNos(event,this);" /></td>
			</tr>';
		}
		$details.='
	</tbody>';
	$details.='<tfoot>
		<tr>
			<td></td>
			<td colspan="2">
				<input type="hidden" name="qns_count" id="qns_count" value="'.$i.'">
				<input type="button" name="button" class="color-button saveperformance_qns" id="button" value="Submit" onclick="saveperformance_qns()">
				<input type="button" name="button" class="color-button addMoreQuestions" id="button" value="Add More Questions" onclick="add_more_qns();">
			</td>
		</tr>
	</tfoot>';
	echo $details;
}
elseif($action=='list_total_qns')
{
	$qns_list_table='<table class="table" id="view_qns_table">
		<thead>
			<tr>
				<th width="20%"><translate>Sl No</translate></th>
				<th width="80%"><translate>Attribute</translate></th>
			</tr>
		</thead>
		<tbody>';
		$qns_list  = $db->fetchQuery("SELECT * from performance_attribute");
	//$qns_list  = $db->fetchQuery("SELECT * FROM performance_question A left join performance_attribute B on A.perfom_attr_id=B.perfom_attr_id ORDER BY A.perfom_attr_id");
		if(!empty($qns_list))
		{
			$i=1;
			foreach($qns_list as $qns_row)
			{
				$qns_list_table.='<tr style="cursor:pointer" class="expand_parent_row">
					<td>'.$i.'</td>
					<td>'.$qns_row['attributes'].'</td>
				</tr>';
				$i++;
				$qns_sub_list  = $db->fetchQuery("SELECT * FROM performance_question where perfom_attr_id=".$qns_row['perfom_attr_id']." ");
				$qns_list_table.='<tr class="row_to_expand" style="display:none;">
					<td colspan="2">
						<table class="table table-bordered">
							<thead>
								<tr style="background-color:#eee;">
									<th style="width:5%;"><translate>Sl No</translate></th>
									<th style="width:15%;"><translate>Issue Date</translate></th>
									<th style="width:60%;"><translate>Question</translate></th>
									<th style="width:15%;"><translate>Weightage</translate></th>
									<th style="width:5%;"><translate>Edit</translate></th>
								</tr>
							</thead>
							<tbody>';
								if(!empty($qns_sub_list))
								{
									$j=1;
									foreach($qns_sub_list as $qns_sub_row)
									{
										$qns_list_table.='<tr class="qns_list_new'.$qns_sub_row['perform_qn_id'].'" style="background-color:#eee;">
											<td>'.$j.'</td>
											<td>'.date("d-m-Y", strtotime($qns_sub_row['issue_date'])).'</td>
											<td>'.$qns_sub_row['question'].'</td>
											<td>'.$qns_sub_row['weightage'].'</td>
											<td><img src="images/pen.png" class="editQuestions" onclick="edit_questions('.$qns_sub_row['perform_qn_id'].')"></td>
										</tr>';
										$j++;
									}
								}
								else
								{
									$qns_list_table.='<tr style="background-color:#eee;">
										<td colspan="5" align="center" style="color:#FF0000;"><strong>No Data to Display....</strong></td>
									</tr>';
								}
							$qns_list_table.='</tbody>
						</table>
					</td>
				</tr>';
			}
		}
		else
		{
			$qns_list_table.='<tr>
				<td colspan="2" align="center" style="color:#FF0000;"><strong>No Data to Display....</strong></td>
			</tr>';
		}
		$qns_list_table.='</tbody>
	</table>';
	
	echo $qns_list_table;
}
elseif($action=='review_qns')
{
    $cond='where 1';
	if($_POST['employee_id'])
	{
		$employee_id=$_POST['employee_id'];
		$cond.=' and A.emp_id = "'.$_POST['employee_id'].'"';
	}
	else
	{
		$employee_id=0;
	}
	
	if($_SESSION['usertype']=='Superadmin')
	{
		$employee_array = $db->fetchQuery("SELECT e_id,CONCAT_WS(' ',e_fname, e_mname, e_lname) as employee_name,employee_id FROM employee where user_type!='Superadmin'");
	}
	elseif($_SESSION['branchadmin']=='Y')
	{
		$employee_array = $db->fetchQuery("SELECT e_id,CONCAT_WS(' ',e_fname, e_mname, e_lname) as employee_name,employee_id FROM employee where user_type!='Superadmin' and branchid=".$_SESSION['branch_id']."");
	}
	else
	{
		$employee_array = $db->fetchQuery("SELECT e_id,CONCAT_WS(' ',e_fname, e_mname, e_lname) as employee_name,employee_id FROM employee where user_type!='Superadmin' and branchid=".$_SESSION['branch_id']."");
	}
	$employee_combo='<select name="filter_attribute" id="filter_attribute" onChange="search_attribute(this.value)">
		<option value="0">Select Employee</option>';
	if(!empty($employee_array))
	{
		foreach($employee_array as $employee_row)
		{
			if($employee_id==$employee_row['e_id'])
			{
				$employee_combo.='<option value="'.$employee_row['e_id'].'" selected="selected">'.$employee_row['employee_name'].'</option>';
			}
			else
			{
				$employee_combo.='<option value="'.$employee_row['e_id'].'">'.$employee_row['employee_name'].'</option>';
			}
		}
	}
	$employee_combo.='</select>';
	echo '<table class="table">
		<tr>
			<td>'.$employee_combo.'</td>
		</tr>
	</table>
	<form action="" method="post" enctype="multipart/form-data" id="review_qstform" name="review_qstform" >
		<table class="table">
			<thead>
        		<tr>
					<th style="width:10%;">Sl No</th>
					<th style="width:40%;">Employee Name</th>
                	<th style="width:20%;">Rate</th>
					<th style="width:30%;">Answered Date</th>
             	</tr>
			</thead>';    

			 if($_SESSION['usertype']=='Superadmin')
			 {
			 	$query_main_attribute = mysql_query("SELECT avg(rate) as emp_avg_rate,(case when max(answered_modified_date) is null then max(answered_date) else max(answered_modified_date) end ) AS answered_date,CONCAT_WS(' ',e_fname, e_mname, e_lname) as employee_name,emp_id FROM performance A left join employee B on A.emp_id=B.e_id ".$cond." group by A.emp_id");
			}
			else
			{
				$query_main_attribute = mysql_query("SELECT avg(rate) as emp_avg_rate,(case when max(answered_modified_date) is null then max(answered_date) else max(answered_modified_date) end ) AS answered_date,CONCAT_WS(' ',e_fname, e_mname, e_lname) as employee_name,emp_id FROM performance A left join employee B on A.emp_id=B.e_id ".$cond." and B.branchid=".$_SESSION['branch_id']." group by A.emp_id");
			}
			$query1_num_rows = mysql_num_rows($query_main_attribute);
			if($query1_num_rows > 0)
			{	
				$i=1;
				while($row = mysql_fetch_array($query_main_attribute))
				{
					$expandable_query=mysql_query('SELECT perform_id,question,answer,rate,weightage,issue_date,attributes FROM performance A left join performance_question B on A.perform_qn_id=B.perform_qn_id left join performance_attribute C on B.perfom_attr_id=C.perfom_attr_id where A.emp_id='.$row['emp_id'].'');
					
					$query2_num_rows = mysql_num_rows($expandable_query);
					$numerator_rate=0;
					$denumerator_rate=0;
					$expandable_rows='';
					if($query2_num_rows > 0)
					{
						$j=1;
						while($row_expand = mysql_fetch_array($expandable_query))
						{
							$numerator_rate=$numerator_rate+($row_expand['weightage']*$row_expand['rate']);
							$denumerator_rate=$denumerator_rate+$row_expand['weightage'];
							$expandable_rows.='<tr style="background-color:#eee;" class="sub_rate_row_'.$row['emp_id'].'">
								<td><input type="hidden" name="qst_review_id[]" value="'.$row_expand['perform_id'].'" />'.$j.'</td>
								<td>'.date("d-m-Y", strtotime($row_expand['issue_date'])).'</td>
								<td>'.$row_expand['attributes'].'</td>
								<td>'.$row_expand['question'].'</td>
								<td>'.$row_expand['answer'].'</td>
								<td class="emp_rate">
									<select class="emp_rate_combo" name=review_rate[]>';
										for($k=0;$k<=10;$k++) { 
											if($row_expand['rate']==$k)
											{
												$expandable_rows.='<option value="'.$k.'" selected="selected">'.$k.'</option>'; 
											}
											else
											{
												$expandable_rows.='<option value="'.$k.'">'.$k.'</option>'; 
											}
										}
									$expandable_rows.='</select></td>
								<td class="qns_weightage">'.$row_expand['weightage'].'</td>
							</tr>';
							$j++;
							$expandable_footer='<tr>
								<td colspan="8" align="center" style="background-color:#FFFFFF;">
									<input type="button" name="button" class="color-button" id="button" value="Calculate Rate" onclick="calculate_rate('.$row['emp_id'].')" />
									<input type="button" name="button" class="color-button" id="button" value="Submit" onclick="fnFillReview()" />
								</td>
							</tr>';
						}
					}
					else
					{
						$expandable_rows.='<tr>
							<td colspan="4" align="center" style="color:#FF0000;"><strong>No Data to Display....</strong></td>
						</tr>';
						$expandable_footer='';
					}
					/*echo '<tr class="expand_parent_row" style="cursor:pointer">
						<td>'.$i.'</td>
						<td>'.$row['employee_name'].'</td>
						<td>'.$row['emp_avg_rate'].'</td>
						<td>'.date("d-m-Y", strtotime($row['answered_date'])).'</td>
					</tr>';*/
					echo '<tr class="expand_parent_row" id="main_rate_row_'.$row['emp_id'].'" style="cursor:pointer">
						<td>'.$i.'</td>
						<td>'.$row['employee_name'].'</td>
						<td>'.($numerator_rate/$denumerator_rate).'</td>
						<td>'.date("d-m-Y", strtotime($row['answered_date'])).'</td>
					</tr>';
					
					$i++;
					echo '<tr class="row_to_expand" style="display:none">
						<td colspan="4">
							<table class="table table-bordered">
								<thead>
									<tr style="background-color:#eee;">
										<th style="width:5%;"><translate>Sl No</translate></th>
										<th style="width:10%;"><translate>Issued Date</translate></th>
										<th style="width:15%;"><translate>Attribute</translate></th>
										<th style="width:30%;"><translate>Question</translate></th>
										<th style="width:30%;"><translate>Answer</translate></th>
										<th style="width:5%;"><translate>Rate</translate></th>
										<th style="width:5%;"><translate>Weightage</translate></th>
									</tr>
								</thead>
								<tbody>'.$expandable_rows.'</tbody>
								<tfoot>'.$expandable_footer.'</tfoot>
							</table>
						</td>
					</tr>';
				}
				
			}
			else
			{
				echo '<tbody>
					<tr>
						<td colspan="3" align="center" style="color:#FF0000;"><strong>No Data to Display....</strong></td>
					</tr>
				</tbody>';
			}
		echo '</table>
	</form>';
}
elseif($action=='fill_review_qns')
{
	$data['rate'] = $_POST['review_rate'];
	$data['perform_id'] = $_POST['qst_review_id'];
	
	if(!empty($data['perform_id']))
	{
		$count = count($data['perform_id']);
		for($i=0;$i<$count;$i++)
		{
			//echo "<br />Perform ID is : ".$data['perform_id'][$i];
			//echo "<br />Rate is : ".$data['rate'][$i];	
			$update_sql = mysql_query("UPDATE performance SET rate = '".$data['rate'][$i]."' WHERE perform_id = '".$data['perform_id'][$i]."'");	
		}
	}
	echo 'success';
}
elseif($action=='update_qns')
{
	$weightage_query=mysql_fetch_array(mysql_query("select weightage,perfom_attr_id from performance_question where perform_qn_id = '".$_POST['update_id']."'"));
	$weightage_db=$weightage_query['weightage'];
	$attr_id=$weightage_query['perfom_attr_id'];
	if($weightage_db==$_POST['edit_weightage'])
	{
		$update_sql = mysql_query("UPDATE performance_question SET question = '".$_POST['update_qns_str']."',weightage='".$_POST['edit_weightage']."' WHERE perform_qn_id = '".$_POST['update_id']."'");
		echo 'success';
	}
	else
	{
		$check_weightage_query=mysql_fetch_array(mysql_query("select sum(weightage) as weightage_sum from performance_question where perform_qn_id != '".$_POST['update_id']."' and perfom_attr_id=".$attr_id.""));
		$check_weightage_sum=$check_weightage_query['weightage_sum']+$_POST['edit_weightage'];
		if($check_weightage_sum==100)
		{
			$update_sql = mysql_query("UPDATE performance_question SET question = '".$_POST['update_qns_str']."',weightage='".$_POST['edit_weightage']."' WHERE perform_qn_id = '".$_POST['update_id']."'");
			echo 'success';
		}
		else
		{
			echo 'error';
		}
	}
}
else
{
	$limit=$_POST['qns_count'];
	for ($x=1; $x<=$limit; $x++) 
	{
		if($_POST['qns_id_'.$x.''])
		{
			$data['question']	  = $_POST['qns_area_'.$x.''];
			$data['weightage']	  = $_POST['qns_weightage_'.$x.''];	
			
			
			$where['perform_qn_id']			= $_POST['qns_id_'.$x.''];
			
			$db->Update('performance_question',$data,$where);
		}
		else
		{
			if(isset($_POST['qns_area_'.$x.'']) && isset($_POST['qns_weightage_'.$x.'']))
			{
				$data['question']	  = $_POST['qns_area_'.$x.''];
				$data['weightage']	  = $_POST['qns_weightage_'.$x.''];	
				//$query=mysql_fetch_array(mysql_query("select if(max(question_no) is null,1,max(question_no)) qns_no from performance_question where perfom_attr_id=".$_POST['attr_combo']." "));
				//echo $query['qns_no']+1;
				//$data['question_no']	  = $query['qns_no']+1;
				$data['perfom_attr_id']	  = $_POST['attr_combo'];	
				$data['issue_date']	  = date('Y-m-d');
				$db->Insert($data,'performance_question');
			}	
		}
	}
	echo 'success';
}
?>