<?php
// leave_history.php using the old mysql_* functions
$connect = new mysqli("localhost", "sooggoo", "4Compassion*", "evergreenint"); 

		$_SESSION['FromCust'] = $_POST['FromCust'];
	
	$FromCust= $_SESSION['FromCust'];
//$FromCust= "13277";
 echo $FromCust;

    // Escape employeeid to prevent SQL injection
    //$employeeid = mysql_real_escape_string($_GET['employeeid']);

    // Build the UNION query to fetch leave history from three tables.
    // Adjust the field names (e.g. date_field, remark) as necessary to match your schema.
     $sql= "
        (SELECT 'Manual Timesheet' AS source,id, leave_type, leave_days,remarks,department 
         FROM manual_timesheet 
         WHERE employeeid = '" . $FromCust. "'
           AND leave_type = 'Annual Leave' 
           AND id >= 179)
        UNION ALL
        (SELECT 'Leave Management' AS source,id, type As leave_type,days As leave_days,comments As remarks,department 
         FROM absent 
         WHERE employeeid = '" . $FromCust. "'
           AND type = 'Annual Leave' 
           AND id >= 179)
        UNION ALL
        (SELECT 'Manual Tech Timesheet' AS source, id,leave_type, leave_days,remarks,department 
         FROM manual_tech_timesheet 
         WHERE employeeid = '" . $FromCust. "'
           AND leave_type = 'Annual Leave' 
           AND id >= 179)
       
    ";

    $OstdgTrans=mysqli_query($connect,$sql);

	  $returnarray = array();
	while ($myrow=mysqli_fetch_array($OstdgTrans)){
            
             $source=htmlspecialchars($myrow['source']);
		     $id=htmlspecialchars($myrow['id']);
             $leavetype=htmlspecialchars($myrow['leave_type']) ;
             $leavedays=htmlspecialchars($myrow['leave_days']) ;
             $remarks = htmlspecialchars($myrow['remarks']) ;
		     $department= htmlspecialchars($myrow['department']) ;
		     $sql1= "select departmentname from department where departmentid = '" . $department. "'
       
    ";

    $depart=mysqli_query($connect,$sql1);
		$myrowd=mysqli_fetch_array($depart);
		$departmentname = $myrowd['departmentname'];
		
		 $sql11= "select end_date from calendar_management where id = '" . $id. "'
       
    ";

    $depart1=mysqli_query($connect,$sql11);
		$myrowd1=mysqli_fetch_array($depart1);
		$enddate = $myrowd1['end_date'];
		
            echo '</tr>';
		
		
         $rowarray = array(
 		"Source"=> $source,
	    "Id"=> $id, 
 		"LeaveType"=>  $leavetype,
 		"LeaveDays"=> $leavedays,
		"Remarks"=> $remarks,
		"Department"=>  $departmentname,
			  "EndDate"=> $enddate
 		
 	);	
 	$returnarray[] = $rowarray;
		
		


			} //end while there are outstanding transaction to print

echo json_encode($returnarray);

   


?>
