<?php 
include('../config/config.php');
include("../classes/classShift.php");
include("../classes/classDoctor.php");
include("../classes/classVisits.php");
include('../classes/classPatient.php');
include("../classes/classImages.php");
include('../classes/classTransaction.php');


/*echo '<pre>';
print_r($_SESSION);
echo '</pre>';*/

#################### set temp session  ##############################################

if(isset($_GET['patientid'])&&isset($_GET['visitid']))
{
	$_SESSION['temp']['patient_id'] = base64_decode($_GET['patientid']);
	$_SESSION['temp']['visit_id'] = base64_decode($_GET['visitid']);
	//header("Location:mgmtvisits.php");
}
if(isset($_SESSION['temp']['shiftid'])&&$_SESSION['temp']['shiftid']!='')
{
	$shift_id = $_SESSION['temp']['shiftid'];
}
if(isset($_SESSION['temp']['doctorid'])&&$_SESSION['temp']['doctorid']!='')
{
	$doctor_id = $_SESSION['temp']['doctorid'];
}
if(isset($_SESSION['temp']['patient_id'])&&$_SESSION['temp']['patient_id']!='')
{
	$patient_id = $_SESSION['temp']['patient_id'];
}
if(isset($_SESSION['temp']['visit_id'])&&$_SESSION['temp']['visit_id']!='')
{
	$visit_id = $_SESSION['temp']['visit_id'];
}

#################### insert images ##############################################

if(isset($_FILES['image']))
{
	try
	{
		$imageOb = new classImages;
		$imageOb->setImageName($_FILES['image']['name']);
		$imageOb->setImageSize($_FILES['image']['size']);
		$imageOb->setImageType($_FILES['image']['type']);
		$imageOb->setPatientAutoID($patient_id);
		$imageOb->executeForPatient();
		
		$image_id = $imageOb->getImageID();
		
		$path = '../images/patient_images/'.$image_id.'.jpg';
		move_uploaded_file($_FILES['image']['tmp_name'],$path);
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
	}
}

#################### Delete images ##############################################

if(isset($_POST['delete_image'])&&$_POST['delete_image']=='Delete')
{
	if(isset($_POST['image_ids'][0]))
	{
		try
		{
			$delImageOb = new classImages;
			for($d=0;$d<count($_POST['image_ids']);$d++)
			{
				$delImageOb->setImageID($_POST['image_ids'][$d]);
				$delImageOb->deleteImageByImageID();
			}
		}
		catch (MyException $e)
		{
			$error_message = $e->getUserMsg();
		}	
	}
}


#################### Compare images ##############################################

if(isset($_POST['compare_image'])&&$_POST['compare_image']=='Compare')
{
	if(isset($_POST['image_ids'][0]))
	{
		$images = base64_encode(implode(',',$_POST['image_ids']));
		?>
		 	<script type="text/javascript">
			window.open("printimages.php?images=<?php echo $images;?>","","top=10,left=200,width=850,height=700,status=no,menubar=no,toolbar=no,scrollbars=yes"); 
			</script>   
        <?php
	}
}

#################### retrieve data ##############################################

try
{
	$retrieveVisitPrecOb = new classVisits;
	$retrieveVisitPrecOb->setPatientID($patient_id);
	$visit_array = $retrieveVisitPrecOb->retrieveVisitByPatientID();
	
	$count_array = $retrieveVisitPrecOb->countVisitsAndPaymenstByPatientID($patient_id);
	$total_payments = $count_array[0]['sum(payment)'];
	$total_visits = $count_array[0]['count(*)'];
	
	///////////// retrieve patient single data ///////////////////////////////
	
	$retrievePatientOb = new classPatient;
	$retrievePatientOb->setPatientAutoID($patient_id);
	$patient_single_detail = $retrievePatientOb->retrievePatientByPatientID();
	$patient_age = $retrievePatientOb->age_from_dob($patient_single_detail[0]['dob']);
	
	$imageOb = new classImages;
	$imageOb->setPatientAutoID($patient_id);
	$image_id = $imageOb->fetchImageOfPatient();
	$images_array = $imageOb->fetchAllImageOfPatient();
	
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
}


/*echo '<pre>';
print_r($count_array);
echo '</pre>';	*/

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/print.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <link rel="shortcut icon" href="../images/favicon.png">
    <title>Med2TEC Hospital Management Information System</title> 
    <link href="../css/print.css" rel="stylesheet">
    <link href="../datepicker/jquery-ui.css" rel="stylesheet" >  
    <script type="text/javascript" src="../datepicker/jquery-1.9.1.js"></script>
	<script type="text/javascript" src="../datepicker/jquery-ui.js"></script> 
</head>

<body> 
<div align="right"> 
 <SCRIPT LANGUAGE="JavaScript">  
if (window.print) {
document.write('<form class="no-print"><input type=button name=print value="Print" onClick="window.print()"></form>');
}
</script></div>

<section class="wrapper">
<div style="background:#FFF; padding:0px 0px 0; height:auto;">
  <table cellspacing="0" cellpadding="0" width="100%" border="0" style="font-size:11px;">
    <tbody>
      <tr>
        <td align="left" style="background:#fff;"><img alt="" src="../images/org_print_logos/<?php echo $_SESSION['security']['org_id']?>.png" style="width:100%;"></td>
      </tr>
    </tbody>
  </table>
  
</br>

</div>

<div class="row">
          <section class="panel">
            <header class="panel-heading">
              <h2><!-- InstanceBeginEditable name="title" -->PATIENT'S OPD HISTORY<!-- InstanceEndEditable --></h2>
            </header>

  <br />
  
<!-- InstanceBeginEditable name="main" -->
  <!-- page start-->
  
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    
    <td valign="top" width="25%"><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><h1><?php  if(isset($patient_single_detail[0]['patient_id'])){echo $patient_single_detail[0]['patient_id'];}?> - <?php if(isset($patient_single_detail[0]['patient_fname'])){echo $patient_single_detail[0]['patient_fname'].' '.$patient_single_detail[0]['patient_lname'];}else{echo 'Outdoor';}?></h1>
    <span>Age: <?php echo $patient_age?> <span style="float:right; padding-right:10%;"></span><br>
    <span>Blood Group: <?php if(isset($patient_single_detail[0]['blood_group'])){echo $patient_single_detail[0]['blood_group'];}?></span><br />
                  <span>Phone: <?php if(isset($patient_single_detail[0]['phone'])){echo $patient_single_detail[0]['phone'];}?></span></br>
                  <span>Address: <?php  if(isset($patient_single_detail[0]['address'])){echo $patient_single_detail[0]['address'];}?></span></span></br>
                  <span>Total Visits: <?php echo $total_visits;?></span></br>
                  <span>Total Payments: Rs. <?php echo number_format($total_payments,2);?></span></td>
                 
  </tr>
</table>
</td>
<td valign="top" width="75%" align="right"><div class="col-md-4" style="width:60%;">
                <section class="panel">
                
                    <div class="panel-body">
                    
                        
                        <h4 class="widget-h" style="float:right;">Image History</h4>
                        
                        	<?php
                            if(isset($images_array[0]['image_id']))
                            {
								?>
                                <div style="float:left; width:100%;">
									<?php
                                    for($im=0;$im<count($images_array);$im++)
                                    {
                                        ?>
                                        <div style="float:right;width:10%; margin-right:25px;">
                                        <img src="../images/patient_images/<?php echo $images_array[$im]['image_id'];?>.jpg" height="63" width="60" >
                                        <?php echo $images_array[$im]['image_id'];?>
                                        </div>
                                        <?php
                                    }
                                    ?>
                                </div>
                                
								<?php
								
                            }
							else
							{
								?>
                                <span style="float:left; margin-top:10px; width:100%;">
                                	<img src="../images/photo.png" width="63" height="60"><br><br>
                                </span>
                                <?php
							}
							?>
                            
                            
                    </div>
                </section>
          	</div></td>
  </tr>
</table>

  

       		   
  <br /><br />
 
          
           	

            <table  class="bottomBorder" cellpadding="0" cellspacing="0" width="100%">
                
                        <thead>
                          <tr>
                            <th width="10%" align="left">Visit</th>
                            <th width="10%" align="left">Medical Issue</th>
                            <th width="25%" align="left">Observations</th>
                            <th width="15%" align="left">Medicines</th>
                            <th width="15%" align="left">Tests</th>
                            <th width="15%" align="left">Procedures</th>
                            <th width="10%" align="left">Payment</th>
                          </tr>
                        </thead>
                        
                        <tbody>
                        <?php
						if(isset($visit_array[0]['visit_id']))
						{
							
							for($v=0;$v<count($visit_array);$v++)
							{
								$retrieveVisitPrecOb = new classVisits;
								$retrieveVisitPrecOb->setVisitID($visit_array[$v]['visit_id']);
								$prec_array = $retrieveVisitPrecOb->retrievePrescription();
								
								$retrieveVisitPrecOb->setMedicalIssueID($visit_array[$v]['med_issue_id']);
								$medical_issue_array = $retrieveVisitPrecOb->retrieveMedicalIssueByID();
								
								$doctorOb = new classDoctor;
								$doctorOb->setDoctorID($visit_array[$v]['doctor_id']);
								$doctor_detail = $doctorOb->retrieveDoctorByDoctorID();
								
								$doctor_name = $doctor_detail[0]['title'].'. '.$doctor_detail[0]['doc_fname'].' '.$doctor_detail[0]['doc_lname'];
								
								
								
								
								?>
								<tr class="gradeX">
						
									<td align="left" valign="top">
									<span class="smallgrey"><?php echo @date('d M Y',$visit_array[$v]['shift_date']);?></span><br>
									
									<?php echo $doctor_name; ?></td>	
                                    
                                    <td valign="top"><?php echo ucwords($medical_issue_array[0]['med_issue']);?> </td>
									
									<td align="left" valign="top">
									<?php
			if(isset($visit_array[$v]['temp'])&&$visit_array[$v]['temp']!='')
			{
				?><span class="smallgrey">Temp:</span> <?php echo $visit_array[$v]['temp'];?> 
                                    <?php
			}
			if(isset($visit_array[$v]['bp'])&&$visit_array[$v]['bp']!='')
			{
				?><span class="smallgrey">BP:</span> <?php echo $visit_array[$v]['bp'];?> 
                                    <?php
			}
			if(isset($visit_array[$v]['bsr'])&&$visit_array[$v]['bsr']!='')
			{
				?><span class="smallgrey">BSR:</span> <?php echo $visit_array[$v]['bsr'];?> 
                                    <?php
			}
			if(isset($visit_array[$v]['weight'])&&$visit_array[$v]['weight']!='')
			{
				?><span class="smallgrey">Weight:</span> <?php echo $visit_array[$v]['weight'];?>
                                    <?php
			} ?>
            <br>
            <?php
			if(isset($visit_array[$v]['complaint'])&&$visit_array[$v]['complaint']!='')
			{
				?><span class="smallgrey">Complaint:</span> <?php echo $visit_array[$v]['complaint']; ?><br>
                                    
                                    <?php
			}
			if(isset($visit_array[$v]['diagnosis'])&&$visit_array[$v]['diagnosis']!='')
			{
				?><span class="smallgrey">Diagnosis:</span> <?php echo $visit_array[$v]['diagnosis']; ?><br>
									<?php
			}
			if(isset($visit_array[$v]['notes'])&&$visit_array[$v]['notes']!='')
			{
				?>
            <span class="smallgrey">Notes:</span> <?php echo $visit_array[$v]['notes']; ?>
            <?php
			} ?>
									</td>
									
									<td align="left" valign="top">
									<?php
									for($prm=0;$prm<count($prec_array);$prm++)
									{
										if($prec_array[$prm]['product_id']!=0)
										{	
											$retreiveMedOb = new classVisits;
											$retreiveMedOb->setMedicineID($prec_array[$prm]['product_id']);
											$med_info = $retreiveMedOb->retrieveMedicineByMedicineID();
											
											echo $med_info[0]['product_name']." - ".$prec_array[$prm]['dosage'].'<br>';
										}
									}
									?>
									</td>
									
									<td align="left" valign="top">
									<?php
									for($prm=0;$prm<count($prec_array);$prm++)
									{
										 if($prec_array[$prm]['labtest_id']!=0)
										 {	
											 $retreiveMedOb = new classVisits;
											 $retreiveMedOb->setLabTestID($prec_array[$prm]['labtest_id']);
											 $labtest_info = $retreiveMedOb->retrieveLabTestsByLabTestID();
																
											 echo $labtest_info[0]['labtest_name'].'<br>';
										}
									}
									?>
									</td>
									
									<td align="left" valign="top">
									<?php
									for($prm=0;$prm<count($prec_array);$prm++)
									{ 
										if($prec_array[$prm]['procedure_id']!=0)
										{	
											$retreiveMedOb = new classVisits;
											$retreiveMedOb->setProcedureID($prec_array[$prm]['procedure_id']);
											$procedure_info = $retreiveMedOb->retrieveProcedureByProcedureID();
												
											echo $procedure_info[0]['procedure_name'].'<br>';
										}
									}
									?>
									</td>
                                    
                                    <td align="right" valign="top">
									<?php echo "Rs. ".number_format($visit_array[$v]['payment']); ?>
									</td>	
						
								</tr> 
								<?php
							}
						}
						?>
						</tbody>
                        
                        
                        
                        <tfoot>
                        
                         
                        </tfoot>
                        
                 </table>
                
  <!-- page end-->
  
<!-- InstanceEndEditable -->
</section>
</div>

<!--<p align="center" style="font-size:12px; font-family:Arial, Helvetica, sans-serif; margin-top:260px;"><strong><!--317 A-1, Nursery Stop, Township, Lahore. 
Ph: 042-35151666</strong><br />
For Appointment call at 042-35151666 (bring your Patient ID Card) </p>-->

   <div class="sidepop" style="float:right;">
    <div class="sidepopbutton stemp"><div style="color: rgb(0, 0, 0); text-align: right; font-size: 10px; margin-top: 4px; position: absolute; right: 180px;">© Software by Softusion Inc. 0322 6155 219</div>   
    </div>
</div>

<!--<div class="sidepop" style="float:right;">
    <div class="sidepopbutton stemp"><div style="color:#000; text-align:right; font-size:9px;">© Software designed by Softusion Inc. Contact us at 0322 6155 219</div>   
    </div>
</div>-->


          
</body>
<!-- InstanceEnd --></html>