<?php 
include('includes/config/config.php');
include('includes/functions.php');
include_once('log_entry.php');
$emp_id = $_REQUEST['id'];
$emp_name=$issue_by=getTableFieldValue('patient_master','patient_name','id',$emp_id);
$emp_code=$issue_by=getTableFieldValue('patient_master','emp_code','id',$emp_id);
$data = array();
$data['emp_name']=$emp_name;
if(!empty($emp_code)){
$data['emp_name'].=' ('.$emp_code.")";
}
$tbl_opd=array();
$tbl_inj=array();
$tbl_medical=array();
$tbl_sickness=array();


$opd_followup=array();
$inj_followup=array();

$query = "select case_type,followup_remarks, patient_name,dept_id,emp_code ,e.id,appointment_id,ep.* ,ep.modified_by as mod_by from employee_appointment ep inner join  patient_master e on e.id=ep.emp_id where appointment_id=( select max(appointment_id) from employee_appointment where emp_id='".$emp_id."' and appointment_type='O')";
 	error_log("Patient_Search:".$query);
	if (!$result = @mysqli_query($conn,$query)) {
				exit(mysqli_error($conn));
	}
	
	if(mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
			//extract($row);


			$query_followup = "select * from followup_details where appointment_id='".$row['appointment_id']."'";
			error_log("Patient_Search_followup:".$query_followup);
			if (!$result_followup = @mysqli_query($conn,$query_followup)) {
				exit(mysqli_error($conn));
	}
		   
		   if(mysqli_num_rows($result_followup) > 0) {
			   while ($row_followup = mysqli_fetch_assoc($result_followup)) {
				array_push($opd_followup,($row_followup['ticket_no'].','.$row_followup['followup_id'].','.$row_followup['appointment_id']));
			   }   
			}

			$complaint_ids = $row['complaints'];
    $complaint_ids_array = array();
    $complaint_ids_array = explode(",", $complaint_ids);
    
			$complaints = "";
			for ($i = 0; $i < count($complaint_ids_array); $i++) {
				if ($i == 0) {
					if (is_numeric($complaint_ids_array[$i])) {
						$complaints = getTableFieldValue('complaints', 'complaint', 'complaint_id', $complaint_ids_array[$i]);
					} else {
						$complaints = $complaint_ids_array[$i];
					}
				} else {
					if (is_numeric($complaint_ids_array[$i])) {
						$complaints = $complaints . "," . getTableFieldValue('complaints', 'complaint', 'complaint_id', $complaint_ids_array[$i]);
					} else {
						$complaints = $complaints . "," . $complaint_ids_array[$i];
					}
				}
			}
			

			$tbl_opd['id']=$row['appointment_id'];
			$tbl_opd['case_type']=$row['appointment_type'];
			$tbl_opd['ticket_no']=$row['ticket_no'];
			$tbl_opd['date']=date_format(date_create($row['appointment_date']), "d-M-Y h:i a") ;
			if(date_format(date_create($row['appointment_date']), "Y-m-d")==date('Y-m-d')){
				$tbl_opd['app_date']='true';
			}else{
				$tbl_opd['app_date']='false';
			}
			
			$tbl_opd['complaints']=$complaints;
			$tbl_opd['Treatment']=getTreatmentText($row['appointment_id']);
			

        }
		
	}
	$data['opd_followup']=$opd_followup;

	$data['opd']=$tbl_opd;
//  -----injury data---------
$query_inj = "select case_type,followup_remarks, patient_name,dept_id,emp_code ,e.id,appointment_id,ep.* ,ep.modified_by as mod_by from employee_appointment ep inner join  patient_master e on e.id=ep.emp_id where appointment_id=( select max(appointment_id) from employee_appointment where emp_id='".$emp_id."' and appointment_type='I')";
error_log("Patient_Search:_inj".$query_inj);
if (!$result_inj = @mysqli_query($conn,$query_inj)) {
		   exit(mysqli_error($conn));
}

if(mysqli_num_rows($result_inj) > 0) {
   while ($row = mysqli_fetch_assoc($result_inj)) {
	   //extract($row);
	   
	   $query_followup = "select * from followup_details where appointment_id='".$row['appointment_id']."'";
	   error_log("Patient_Search_followup:".$query_followup);
	   if (!$result_followup = @mysqli_query($conn,$query_followup)) {
		   exit(mysqli_error($conn));
}
	  
	  if(mysqli_num_rows($result_followup) > 0) {
		  while ($row_followup = mysqli_fetch_assoc($result_followup)) {
		   array_push($inj_followup,($row_followup['ticket_no'].','.$row_followup['followup_id'].','.$row_followup['appointment_id']));
		  }   
	   }

			
	   $complaint_ids = $row['complaints'];
$complaint_ids_array = array();
$complaint_ids_array = explode(",", $complaint_ids);

	   $complaints = "";
	   for ($i = 0; $i < count($complaint_ids_array); $i++) {
		   if ($i == 0) {
			   if (is_numeric($complaint_ids_array[$i])) {
				   $complaints = getTableFieldValue('complaints', 'complaint', 'complaint_id', $complaint_ids_array[$i]);
			   } else {
				   $complaints = $complaint_ids_array[$i];
			   }
		   } else {
			   if (is_numeric($complaint_ids_array[$i])) {
				   $complaints = $complaints . "," . getTableFieldValue('complaints', 'complaint', 'complaint_id', $complaint_ids_array[$i]);
			   } else {
				   $complaints = $complaints . "," . $complaint_ids_array[$i];
			   }
		   }
	   }
	   

	   $tbl_inj['id']=$row['appointment_id'];
	   $tbl_inj['case_type']=$row['appointment_type'];
	   $tbl_inj['ticket_no']=$row['ticket_no'];
	   $tbl_inj['date']=date_format(date_create($row['appointment_date']), "d-M-Y h:i a") ;
	   if(date_format(date_create($row['appointment_date']), "Y-m-d")==date('Y-m-d')){
		$tbl_inj['app_date']='true';
	}else{
		$tbl_inj['app_date']='false';
	}

	   $tbl_inj['complaints']=$complaints;
	   $tbl_inj['Treatment']=getTreatmentText($row_employee_appointment['appointment_id']);
	   

   }
   
}

$data['inj_followup']=$inj_followup;
$data['inj']=$tbl_inj;

//-------- medical-------
$query_medical = "select a.*, b.patient_name,b.emp_code,b.dept_id from checkup_form a left join patient_master b on a.emp_id=b.id left join checkup_type x on a.checkup_type_id=x.checkup_type_id where checkup_id=( select max(checkup_id) from checkup_form a left join patient_master b on a.emp_id=b.id left join checkup_type x on a.checkup_type_id=x.checkup_type_id where  x.type_state!='Yes' and emp_id='".$emp_id."' )";
error_log("Patient_Search:_medical".$query_medical);
if (!$result_medical = @mysqli_query($conn,$query_medical)) {
		   exit(mysqli_error($conn));
}

if(mysqli_num_rows($result_medical) > 0) {
   while ($row = mysqli_fetch_assoc($result_medical)) {
	   //extract($row);
	   
	   $tbl_medical['ticket_no']=$row['ticket_no'];
	   $tbl_medical['id']=$row['checkup_id'];
	   $tbl_medical['checkup_type_id']=$row['checkup_type_id'];
	   $tbl_medical['date']=date_format(date_create($row['checkup_date']), "d-M-Y h:i a") ;
	  
	   if(date_format(date_create($row['checkup_date']), "Y-m-d")==date('Y-m-d')){
		$tbl_medical['app_date']='true';
	}else{
		$tbl_medical['app_date']='false';
	}
	   $tbl_medical['type'] = getFieldFromTable('checkup_type_name', 'checkup_type', 'checkup_type_id', $row['checkup_type_id']);
	   $tbl_medical['tests']= getCommaSeperatedValuesForInClause("select section_name from  checkup_form_section", "section_id", $row['checkup_section_ids']);


   }
   
}
$data['medical']=$tbl_medical;
// -----------sickness----
$query_sickness = "select patient_name,followup_to_opd,emp_code,designation_id,dept_id,bu_id,ticket_no, token_no, sickness_id,sickness_date,certificate_type,from_date, to_date, date_absent, date_return,doctor_last_attended,s.modified_by,sickness_name,des,fitness_status from sickness s, patient_master e where s.emp_id=e.id and sickness_id=( select max(sickness_id) from sickness   where   emp_id='".$emp_id."' )";
error_log("Patient_Search:_sickness".$query_sickness);
if (!$result_sickness = @mysqli_query($conn,$query_sickness)) {
		   exit(mysqli_error($conn));
}

if(mysqli_num_rows($result_sickness) > 0) {
   while ($row = mysqli_fetch_assoc($result_sickness)) {
	   //extract($row);
	   $tbl_sickness['id']=$row['sickness_id']; 
	   $tbl_sickness['ticket_no']=$row['ticket_no'];
	   $tbl_sickness['opd_ticket']=getTableFieldValue('employee_appointment','ticket_no','appointment_id',$row['followup_to_opd']);
	   $tbl_sickness['opd_case_type']=getTableFieldValue('employee_appointment','appointment_type','appointment_id',$row['followup_to_opd']);
	   $tbl_sickness['opd_id']=$row['followup_to_opd'];
	   $tbl_sickness['date']=date_format(date_create($row['sickness_date']), "d-M-Y h:i a") ;
	   $tbl_sickness['absence'] ='From: '.date_format(date_create($row['date_absent']), "d-M-Y ").'  To: '. date_format(date_create($row['date_return']), "d-M-Y ");
	   $tbl_sickness['ailment']= getTableFieldValue('ailment_system', 'ailment_sys_name', 'ailment_sys_id', $row['sickness_name']);;


   }
   
}
$data['sickness']=$tbl_sickness;


	
	error_log(print_r($data,true)) ;
 echo json_encode($data);
?>