<?php
include('../config/config.php');
include('../classes/classWardMgmt.php');
include('../classes/classPatient.php');

if(isset($_GET['cbr'])&&$_GET['cbr']!='')
{
	$cbrID = base64_decode($_GET['cbr']);
	$cbra = base64_decode($_GET['cbra']);
	
	$canceResOb = new classWardMgmt;
	$canceResOb->setWardBedID($cbrID);
	$canceResOb->setWardBedStatus('Empty');
	$canceResOb->updateWardBedStatus();
	
	$canceResOb->setWardBedAllotID($cbra);
	$canceResOb->deleteBedAllotment();
	
	header("Location:mgmtadmissions.php");
}
################# retrieve all wards #######################################

if(isset($_POST['search_bed_no'])&&$_POST['search_bed_no']!='')
{
	$search_ipd_bed_no = $_POST['search_bed_no'];
}
else
{
	$search_ipd_bed_no = NULL;
}

if(isset($_POST['search_patient_name'])&&$_POST['search_patient_name']!='')
{
	$search_patient_name = $_POST['search_patient_name'];
}
else
{
	$search_patient_name = NULL;
}


try
{
	$wardOb = new classWardMgmt;
	$ward_array = $wardOb->retrieveWardAndBeds($_SESSION['security']['location_id'],$search_ipd_bed_no,$search_patient_name);
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
}


try
{
	$wardDDOb = new classWardMgmt;
	$wardDDOb->setLocationID($_SESSION['security']['location_id']);
	$ward_dropdown = $wardDDOb->retrieveAllWards();
	$bed_dropdown = $wardDDOb->retrieveAllEmptybedOfWard();
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
}



?>
<!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"><head></head><!-- 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" --><!-- InstanceEndEditable --></h2>
            </header>

  <br />
  
<!-- InstanceBeginEditable name="main" -->
					
    <title>Med2TEC Hospital Management Information System</title>
    
  <section id="main-content">
    <section class="wrapper">

      <!-- page start-->
      <div class="row">
        <div class="col-sm-12">
          <section class="panel">
            <header class="panel-heading">
              <h2>ADMISSIONS &amp; DISCHARGE</h2>
            </header>
            
            <?php
            if(isset($error_message)&&$error_message!='')
            {
                ?>
                <tr>
                    <td><span class="message_red"><?php echo $error_message;?></span></td>
                </tr>
                <?php
            }
            ?>

            <div class="panel-body">
              <div class="adv-table">
              
              	<table class="bottomBorder" id="dynamic-table" width="100%" cellpadding="0" cellspacing="0">
                
                  <thead>
                  
                    <tr>
                      
                      <th width="15%">Ward</th>
                      <th width="65%">
                  </th>
                      <th width="10%">No. of Beds</th>
                      <th width="10%">Ward Incharge</th>
                    </tr>
                    
                  </thead>
                  
                  <tbody>
                   <?php
					if(isset($ward_array[0]['ipd_ward_id'])&&$ward_array[0]['ipd_ward_id']!='')
					{
						for($w=0;$w<count($ward_array);$w++)
						{
							
							$ipd_ward_id = $ward_array[$w]['ipd_ward_id'];
							$ipd_ward_id_encode = base64_encode($ipd_ward_id);
							
							$ipd_ward_no = $ward_array[$w]['ipd_ward_no'];
							$ipd_ward_type = ucwords(str_replace('_',' ',$ward_array[$w]['ipd_ward_type']));
							$staff_id = $ward_array[$w]['staff_id'];
							$no_of_beds = $ward_array[$w]['no_of_beds'];
							
							?>
							<tr>
                      
                      		<td><?php echo $ipd_ward_no;?> - <?php echo $ipd_ward_type;?></td>
                      		<td>
                     
                             <?php
							if(isset($ward_array[$w]['bed_details'][0])&&$ward_array[$w]['bed_details'][0]!='')
							{
								for($wb=0;$wb<count($ward_array[$w]['bed_details']);$wb++)
								{
									$ipd_ward_id = $ward_array[$w]['bed_details'][$wb]['ipd_ward_id'];
									
									$ipd_bed_id = $ward_array[$w]['bed_details'][$wb]['ipd_bed_id'];
									$ipd_bed_id_encode = base64_encode($ipd_bed_id);
									
									$ipd_bed_no = $ward_array[$w]['bed_details'][$wb]['ipd_bed_no'];
									$ipd_bed_fee = $ward_array[$w]['bed_details'][$wb]['ipd_bed_fee'];
									$ipd_bed_type = $ward_array[$w]['bed_details'][$wb]['ipd_bed_type'];
									$status = $ward_array[$w]['bed_details'][$wb]['status'];
									
									
									$patient_id = $ward_array[$w]['bed_details'][$wb]['patient_id'];
									$patient_id_encoded = base64_encode($patient_id);
									$patient_name = $ward_array[$w]['bed_details'][$wb]['patient_name'];
									$allot_time = $ward_array[$w]['bed_details'][$wb]['allot_time'];
									$discharge_due_time = $ward_array[$w]['bed_details'][$wb]['discharge_due_time'];
									$ipd_bed_allotment_id = $ward_array[$w]['bed_details'][$wb]['ipd_bed_allotment_id'];
									$ipd_bed_allotment_id_encoded = base64_encode($ward_array[$w]['bed_details'][$wb]['ipd_bed_allotment_id']);
									$discharge_slip_created = $ward_array[$w]['bed_details'][$wb]['discharge_slip_created'];
									
									if($status=='Empty')
									{
										$image = 'bed_green';
										$button_type = '';
										$transfer_button = '';
										$cancel_button = '';
										$show_info = "";
										
										
									}
									if($status=='Occupied')
									{
										$image = 'bed_red';
										
										if($discharge_slip_created)
										{
											$button_type = "";
										}
										else
										{
											$button_type = "";
										}
										
										$transfer_button = '';
										$cancel_button = '';
										$show_info = "Occupied by: $patient_name <br>
										Admitted at: $allot_time <br>
										Discharge Due at: $discharge_due_time <br><br>";
									}
									
									if($status=='Reserved')
									{
										$image = 'bed_yellow';
										$button_type = '<a href="#allocate_ipdwardbed-box" class="allocate_ipdwardbed-window btn btn-success btn-sm allocate_reserved_ipdwardbed" id="'.$w.$wb.'">Allocate</a>';
										$transfer_button = '';
										$cancel_button = " <a href='?cbr=$ipd_bed_id_encode&cbra=$ipd_bed_allotment_id_encoded'>Cancel Reservation</a>";
										$show_info = "Reserved by: $patient_name <br>
										Reserved at: $allot_time <br>";
									}
									
									?>
                                    
                                    <table width="100%">
                                    <tr>
                                    
                                    <td width="10%" align="left"><img src="../images/<?php echo @$image;?>.png" /></td>
                                    
                                    <td width="30%" align="left"><strong><?php echo $ipd_bed_no;?></strong><br />
                                    Fee: Rs. <?php echo number_format($ipd_bed_fee,2);?>
                                    <p>
                                    <span style="float:left;">Detail: <?php echo $ipd_bed_type;?></span></p></td>
                                    
                                    <td width="60%">
                                    <strong>Status: <?php echo $status.$cancel_button;?></strong><br />
                                    <?php echo $show_info;?>
                                    <span style="float:right;"><?php echo @$button_type;?> <?php echo @$transfer_button;?></span>     								</td>
                                    
                                    </tr>
                                    </table>

                                    
                                    <input type="hidden" name="ipd_ward_id" class="set_to_pop" id="ipd_ward_id_<?php echo $w.$wb;?>" value="<?php echo $ipd_ward_id;?>" />
                                    <input type="hidden" name="ipd_bed_id" class="set_to_pop" id="ipd_bed_id_<?php echo $w.$wb;?>" value="<?php echo $ipd_bed_id;?>" />
                                    
                                    <input type="hidden" name="ipd_bed_allotment_id" class="set_to_pop" id="ipd_bed_allotment_id_<?php echo $w.$wb;?>" value="<?php echo $ipd_bed_allotment_id;?>" />
                                    
                                    <input type="hidden" name="ipd_patient_id" class="set_to_pop" id="ipd_patient_id_<?php echo $w.$wb;?>" value="<?php echo $patient_id;?>" />
                                    <input type="hidden" name="ipd_patient_name" class="set_to_pop" id="ipd_patient_name_<?php echo $w.$wb;?>" value="<?php echo $patient_name;?>" />
                                    
                                    <input type="hidden" name="ipd_ward_no" class="set_to_pop" id="ipd_ward_no_<?php echo $w.$wb;?>" value="<?php echo $ipd_ward_no;?>" />
                                    
                                    <input type="hidden" name="ipd_bed_no" class="set_to_pop" id="ipd_bed_no_<?php echo $w.$wb;?>" value="<?php echo $ipd_bed_no;?>" />
									<?php 
								}
								
							}
							else
							{
								?>
								There are no beds in this ward.<p>
								<?php
								
							}
						?>
                      
                      
                      </p></td>
                      		<td align="right"><?php echo $no_of_beds;?></td>
                      		<td><?php echo $staff_id;?></td>
                      
                            </tr>
                            </tbody>
                
                            <?php	
                         }
					}
					else
					{
						?>
                        <tr>
                             <td colspan="6">No Ward entered yet!</td>
                        </tr>
                        <?php
					}
					?>       
                  
                  
               </table>
                
              </div>
            </div>

            
            
          </section>
        </div>
      </div>
      <!-- page end-->
    </section>
  </section>
  <!-- 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>







<script>

$(".allocate_ipdwardbed").click(function() {
	var id = this.id;
	//alert(id);
	ipd_bed_id = $("#ipd_bed_id_"+id).val();
	$("#bed_id_allot").val(ipd_bed_id);
	
	ipd_ward_id = $("#ipd_ward_id_"+id).val();
	$("#ward_id").val(ipd_ward_id);
	
	ipd_ward_no = $("#ipd_ward_no_"+id).val();
	$("#ipd_ward_no").html(ipd_ward_no);
	
	ipd_bed_no = $("#ipd_bed_no_"+id).val();
	$("#ipd_bed_no").html(ipd_bed_no);
});

$(".allocate_reserved_ipdwardbed").click(function() {
	var id = this.id;
	//alert(id);
	ipd_bed_id = $("#ipd_bed_id_"+id).val();
	$("#bed_id_allot").val(ipd_bed_id);
	
	ipd_ward_id = $("#ipd_ward_id_"+id).val();
	$("#ward_id").val(ipd_ward_id);
	
	ipd_ward_no = $("#ipd_ward_no_"+id).val();
	$("#ipd_ward_no").html(ipd_ward_no);
	
	ipd_bed_no = $("#ipd_bed_no_"+id).val();
	$("#ipd_bed_no").html(ipd_bed_no);
	
	ipd_patient_id = $("#ipd_patient_id_"+id).val();
	$("#patient_id").val(ipd_patient_id);
	
	ipd_patient_name = $("#ipd_patient_name_"+id).val();
	$("#patient_name").html(ipd_patient_name);
	
	ipd_bed_allotment_id = $("#ipd_bed_allotment_id_"+id).val();
	$("#ipd_bed_allotment_id").val(ipd_bed_allotment_id);
	
	$("#allocate_type").val('allocate_reserved');
	
	$("#select_patient_popup").hide();
	
	$("#bed_status").hide();
	
	
});




$(".transfer_ipdwardbed").click(function() {
	var id = this.id;
	//alert(id);
	
	ipd_bed_allotment_id = $("#ipd_bed_allotment_id_"+id).val();
	$("#transfer_bed_allotment_id").val(ipd_bed_allotment_id);
	
	ipd_patient_name_ = $("#ipd_patient_name_"+id).val();
	$("#transfer_patient_name").html(ipd_patient_name_);
	
	ipd_bed_no = $("#ipd_bed_no_"+id).val();
	$("#current_bed_no").html(ipd_bed_no);
	
});


$(function() {
		
		$("#datepicker").datepicker({
		showOn: "button",
		buttonImage: "../images/calendar.gif",
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		dateFormat: "yy-mm-dd",
		minDate: '0'
		});
		
		$("#datepicker1").datepicker({
		showOn: "button",
		buttonImage: "../images/calendar.gif",
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		dateFormat: "yy-mm-dd",
		minDate: '0'
		});
		
		
	
	});
</script>

<script type="text/javascript" src="../timepicker/jquery.timepicker.js"></script>
<link rel="stylesheet" type="text/css" href="../timepicker/jquery.timepicker.css" />

<!--<script type="text/javascript" src="../timepicker/lib/bootstrap-datepicker.js"></script>
<link rel="stylesheet" type="text/css" href="../timepicker/lib/bootstrap-datepicker.css" />

<script type="text/javascript" src="../timepicker/dist/datepair.js"></script>           
<script type="text/javascript" src="../timepicker/dist/jquery.datepair.js"></script>
-->

<script>

$('#allocation_time_due').timepicker();


$('#discharge_time_due').timepicker();



	
</script>

