<?php include 'inc_classes.php';
$action = $_POST['action'];  
$branch_id=$_POST['branch_id'];
$admin_id=$_POST['admin_id'];
$presence=$_POST['presence'];
if($action=='performance') 
{
    // $branch_id=$_POST['branch_id'];
		// $staff_id=$_POST['staff_id'];
		$date=$_POST['date'];
		$frm_date=explode("/",$date);
		$frm_dates=$frm_date[2]."-".$frm_date[1]."-".$frm_date[0];
		$added_date=$frm_dates;

        $sel_branch="select cm_id from site_setting where branch_name='".$branch_id."' ";
		$ptr_branch=mysql_query($sel_branch);
		$data_branch=mysql_fetch_array($ptr_branch);
		$cm_id=$data_branch['cm_id'];

        $sel_pres="select attendance_id,total_hrs from pr_daily_attendance where employee_id='".$admin_id."'and cm_id='".$cm_id."' and DATE(date)='".$added_date."'";
		$ptr_pres=mysql_query($sel_pres);
		$data_pres=mysql_fetch_array($ptr_pres);
        if($data_pres['attendance_id']!='')
        {
            
            echo '<table width="80%">';
            $select_cm_id="select type from site_setting where admin_id='".$admin_id."' ";
            $ptr_cm_id=mysql_query($select_cm_id);
            $data_cm_id=mysql_fetch_array($ptr_cm_id);
            $data_type=$data_cm_id['type'];
            if($data_type=='F'){
                $sel_type1="select * from pm_key_performance_point where 1 and employee_type ='".$data_type."'  order by key_id ASC";
            }
            else{
                $sel_type1="select * from pm_key_performance_point where 1 and employee_type !='F'  order by key_id ASC";
            }
            $ptr_type1=mysql_query($sel_type1);
            $cnt=mysql_num_rows($ptr_type1);
            $i=1;
            while($type_data1=mysql_fetch_array($ptr_type1))
                {
                    echo '<tr>';
                    echo '<td width="40%"><strong>'.$type_data1['key_title'].'</strong><input type="hidden" name="key_title'.$i.'" id="key_title" value="'.$type_data1['key_id'].'" /></td>';
                    echo '<td width="30%"><div>Yes<input type="radio" name="check1'.$i.'" id="class_start_time_r_y'.$i.'" onchange="cal_points(this.value,'.$i.')" checked="checked" class="input_radio" value="yes">No<input type="radio" name="check1'.$i.'" id="class_start_time_r_n'.$i.'" onchange="cal_points(this.value,'.$i.')" class="input_radio" value="no"> </div></td>';
                    
                    echo '<td width="20%"><input type="hidden" name="" style="text-align:center; width:100px" class="input_text" id="class_start_time_mark'.$i.'" value="'.$type_data1['point'].'">';
                    echo'<input type="text" readonly="readonly" name="point'.$i.'" style="text-align:center; width:100px" class="input_text" id="pointing'.$i.'" value="'.$type_data1['point'].'"></td>';
                    echo '</tr>';
                    $i++;
                }
                echo '<input type="hidden" name="total_cnt" id="total_cnt" value="'.$cnt.'" />';
        }
        else if($presence=="yes")
        {
            echo '<table width="80%">';
            $select_cm_id="select type from site_setting where admin_id='".$admin_id."' ";
            $ptr_cm_id=mysql_query($select_cm_id);
            $data_cm_id=mysql_fetch_array($ptr_cm_id);
            $data_type=$data_cm_id['type'];
            if($data_type=='F'){
            $sel_type1="select * from pm_key_performance_point where 1 and employee_type ='".$data_type."'  order by key_id ASC";
            }
            else{
                $sel_type1="select * from pm_key_performance_point where 1 and employee_type !='F'  order by key_id ASC";
            }
            $ptr_type1=mysql_query($sel_type1);
            $cnt=mysql_num_rows($ptr_type1);
            $i=1;
            while($type_data1=mysql_fetch_array($ptr_type1))
            {
                echo '<tr>';
                echo '<td width="40%"><strong>'.$type_data1['key_title'].'</strong><input type="hidden" name="key_title'.$i.'" id="key_title" value="'.$type_data1['key_id'].'" /></td>';
                echo '<td width="30%"><div>Yes<input type="radio" name="check1'.$i.'" id="class_start_time_r_y'.$i.'" onchange="cal_points(this.value,'.$i.')" checked="checked" class="input_radio" value="yes">No<input type="radio" name="check1'.$i.'" id="class_start_time_r_n'.$i.'" onchange="cal_points(this.value,'.$i.')" class="input_radio" value="no"> </div></td>';
                
                echo '<td width="20%"><input type="hidden" name="" style="text-align:center; width:100px" class="input_text" id="class_start_time_mark'.$i.'" value="'.$type_data1['point'].'">';
                echo'<input type="text" readonly="readonly" name="point'.$i.'" style="text-align:center; width:100px" class="input_text" id="pointing'.$i.'" value="'.$type_data1['point'].'"></td>';
                echo '</tr>';
                $i++;
            }
            echo '<input type="hidden" name="total_cnt" id="total_cnt" value="'.$cnt.'" />';
        }
}
else if($action=="performance_report")
{
	$date=$_POST['date'];
	$frm_date=explode("/",$date);
	$frm_dates=$frm_date[2]."-".$frm_date[1]."-".$frm_date[0];
	$added_date=$frm_dates;
    $sel_branch="select cm_id from site_setting where branch_name='".$branch_id."' ";
	$ptr_branch=mysql_query($sel_branch);
	$data_branch=mysql_fetch_array($ptr_branch);
	$cm_id=$data_branch['cm_id'];
	$employee_id='0';
	$sel_staff="select employee_id from pm_total_keypoint_performancee where 1 and date='".$added_date."' and cm_id='".$cm_id."'";
	$ptr_staff=mysql_query($sel_staff);
	$tot=mysql_num_rows($ptr_staff); 
	$it=1;
	while($data_staff=mysql_fetch_array($ptr_staff)){
		if($it==$tot)
		{
			$employee_id .= $data_staff['employee_id'];
		}
		else{            
			$employee_id .=$data_staff['employee_id'].',';
			}
		$it++;
	}

	$where_branch='';
	$where_branch="  and branch_name='".$branch_id."'";
	 $sel_name="SELECT * FROM site_setting WHERE 1 ".$where_branch." and system_status='Enabled' and admin_id NOT IN(".$employee_id.") order by name";
	$ptr_name=mysql_query($sel_name);
	?>
    <select name="staff_id" id="staff_id" class="input_select" style="width:300px" onchange="check_data(this.value),select_key(this.value)">
	<option value="">Select Staff</option>
	<?php
	while($fetch_name=mysql_fetch_array($ptr_name))
	{
		$sel='';
		if($admin_id==$fetch_name['admin_id'])
		{
			$sel='selected="selected"';
		}
		?>
		<option value="<?php echo $fetch_name['admin_id']?>" <?php echo $sel; ?>> <?php echo $fetch_name['name'] ?> </option>
		<?php
	}
	?>
    </select>
	<?php
}
if($action=="add_kpi")
{
    $dates=$_POST['dates'];
	$frm_date=explode("/",$dates);
	$frm_dates=$frm_date[2]."-".$frm_date[1]."-".$frm_date[0];
    $branch_id=$_POST['branch_name'];
    $sel_branch="select cm_id from site_setting where branch_name='".$branch_id."' ";
	$ptr_branch=mysql_query($sel_branch);
	$data_branch=mysql_fetch_array($ptr_branch);
    $cm_id=$data_branch['cm_id'];
	$adminid=$_POST['adminid'];
    $tot_points=$_POST['tot_points'];
    $percentage=$_POST['percentage'];
    $comment=$_POST['comment'];
    $absent_status=$_POST['absent_status'];
    $status=$_POST['status'];
    if($status=='yes')
    {
        $presence='present';
    }
    else if($status=="no")
    {
        $presence='absent';
    }
    $key_array=$_POST['key_array'];
    $value_array=$_POST['value_array'];
    if($status=='yes')
    {
        $array_key = explode(",", $key_array);
        $array_value = explode(",", $value_array);
        for ($i = 0; $i < count($array_key); $i++) {
            echo $ins_kpi="INSERT INTO `pm_daily_keypoint_performance`(`employee_id`, `presence`,`key_point_id`, `points`, `date`,`added_date`,`cm_id`, `admin_id`) VALUES ('".$adminid."','".$status."','".$array_key[$i]."','".$array_value[$i]."','".$frm_dates."','".date('Y-m-d : H:i:s')."','".$cm_id."','".$_SESSION['admin_id']."')";
            $ptr_ins_kpi=mysql_query($ins_kpi);
        }
    }
    else if($status=="no")
    {
        $ins_kpi="INSERT INTO `pm_daily_keypoint_performance`(`employee_id`, `presence`, `without_info_absent`,`key_point_id`, `points`, `date`,`added_date`,`cm_id`, `admin_id`) VALUES ('".$adminid."','".$status."','".$absent_status."','0','".$tot_points."','".$frm_dates."','".date('Y-m-d : H:i:s')."','".$cm_id."','".$_SESSION['admin_id']."')";
        $ptr_ins_kpi=mysql_query($ins_kpi);
    }
    $ins="INSERT INTO `pm_total_keypoint_performancee`(`employee_id`, `status`, `total_point`,`total_percentage`, `comment`, `date`,`cm_id`, `admin_id`, `added_date`) VALUES ('".$adminid."','".$presence."','".$tot_points."','".$percentage."','".$comment."','".$frm_dates."','".$cm_id."','".$_SESSION['admin_id']."','".date('Y-m-d : H:i:s')."')";
    $ptr_ins=mysql_query($ins);
}
if($action=="add_kpi_f")
{
    $dates=$_POST['dates'];
	$frm_date=explode("/",$dates);
	$frm_dates_f=$frm_date[2]."-".$frm_date[1]."-".$frm_date[0];
    $branch_id=$_POST['branch_name'];
    $sel_branch="select cm_id from site_setting where branch_name='".$branch_id."' ";
	$ptr_branch=mysql_query($sel_branch);
	$data_branch=mysql_fetch_array($ptr_branch);
    $cm_id_f=$data_branch['cm_id'];
	$adminid_f=$_POST['adminid_f'];
    $tot_points_f=$_POST['tot_points_f'];
    $percentage_f=$_POST['percentage_f'];
    $comment_f=$_POST['comment_f'];
    $absent_status_f=$_POST['absent_status_f'];
    $status_f=$_POST['status_f'];
    if($status_f=='yes')
    {
        $presence_f='present';
    }
    else if($status_f=="no")
    {
        $presence_f='absent';
    }
     '<br>'.$key_array_f=$_POST['key_array_f'];
     '<br>'.$value_array_f=$_POST['value_array_f'];
    if($status_f=='yes')
    {
        $array_key_f = explode(",", $key_array_f);
        $array_value_f = explode(",", $value_array_f);
        for ($i = 0; $i < count($array_key_f); $i++) {
           '<br>'.$ins_kpi_f="INSERT INTO `pm_daily_keypoint_performance`(`employee_id`, `presence`,`key_point_id`, `points`, `date`,`added_date`,`cm_id`, `admin_id`) VALUES ('".$adminid_f."','".$status_f."','".$array_key_f[$i]."','".$array_value_f[$i]."','".$frm_dates_f."','".date('Y-m-d : H:i:s')."','".$cm_id_f."','".$_SESSION['admin_id']."')";
            $ptr_ins_kpif=mysql_query($ins_kpi_f);
        }
    }
    else if($status_f=="no")
    {
       '<br>'.$ins_kpi_f="INSERT INTO `pm_daily_keypoint_performance`(`employee_id`, `presence`, `without_info_absent`,`key_point_id`, `points`, `date`,`added_date`,`cm_id`, `admin_id`) VALUES ('".$adminid_f."','".$status_f."','".$absent_status_f."','0','".$tot_points_f."','".$frm_dates_f."','".date('Y-m-d : H:i:s')."','".$cm_id_f."','".$_SESSION['admin_id']."')";
        $ptr_ins_kpif=mysql_query($ins_kpi_f);
    }
    $ins="INSERT INTO `pm_total_keypoint_performancee`(`employee_id`, `status`, `total_point`,`total_percentage`, `comment`, `date`,`cm_id`, `admin_id`, `added_date`) VALUES ('".$adminid_f."','".$presence_f."','".$tot_points_f."','".$percentage_f."','".$comment_f."','".$frm_dates_f."','".$cm_id_f."','".$_SESSION['admin_id']."','".date('Y-m-d : H:i:s')."')";
    $ptr_ins=mysql_query($ins);
}
if($action=="sel_staff")
{
	$where_branch='';
	$where_branch="  and branch_name='".$branch_id."'";
	$sel_name="SELECT * FROM site_setting WHERE 1 ".$where_branch." and system_status='Enabled' order by name";
	$ptr_name=mysql_query($sel_name);
	?>
    <select name="staff_id" id="staff_id" class="form-control select2" >
	<option value="">Select Staff</option>
	<?php
	while($fetch_name=mysql_fetch_array($ptr_name))
	{
		$sel='';
		if($admin_id==$fetch_name['admin_id'])
		{
			$sel='selected="selected"';
		}
		?>
		<option value="<?php echo $fetch_name['admin_id']?>" <?php echo $sel; ?>> <?php echo $fetch_name['name'] ?> </option>
		<?php
	}
	?>
    </select>
	<?php
}
if($action=='staff') 
{
	$branch_name=$_POST['branch_name'];
    $sel_branch="select cm_id from site_setting where branch_name='".$branch_name."'";
    $ptr_branch = mysql_query($sel_branch);
	$branch_data= mysql_fetch_array($ptr_branch);
  echo  '<option  value="">Select Staff</option>';
        $categary="select admin_id,name from site_setting where cm_id='".$branch_data['cm_id']."'and system_status='Enabled' order by name ASC";
        $c_query=mysql_query($categary);
        while($data_c=mysql_fetch_array($c_query))
        {
           echo  '<option  value="'.$data_c['admin_id'].'">'.strip_tags(ucfirst($data_c['name'])). '</option>';
        }	
}
?>
