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

################# delete ward #######################################

if(isset($_GET['wid'])&&$_GET['wid']!='')
{
	try
	{
		$wid = base64_decode($_GET['wid']);
		$delWardOb = new classWardMgmt;
		$delWardOb->setWardID($wid);
		$delWardOb->deleteWard();
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
	}
}

################# delete bed #######################################

if(isset($_GET['bid'])&&$_GET['bid']!='')
{
	try
	{
		$bid = base64_decode($_GET['bid']);
		$delBedOb = new classWardMgmt;
		$delBedOb->setWardBedID($bid);
		$delBedOb->deleteBed();
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
	}
}

################# 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;
}


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

################# retrieve all staff #######################################

try
{
	$staffOb = new classWardMgmt;
	$staffOb->setLocationID($_SESSION['security']['location_id']);
	$staff_array = $staffOb->retrieveAllStaff();
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
}

/*echo '<pre>';
print_r($ward_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"><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>MANAGE WARDS &amp; BEDS</h2>
            </header>
            
            <?php
            if(isset($error_message)&&$error_message!='')
            {
                ?>
                <span class="message_red"><?php echo $error_message;?></span>
                <?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%" align="left">Ward</th>
                      <th width="55%">
                  
                  </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;?><br />
                            <?php
                            if(count($ward_array)>0)
                            {
								?>
								
								<?php
                            }
                            ?></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'];
									
									$ipd_bed_allotment_id_encoded = base64_encode($ward_array[$w]['bed_details'][$wb]['ipd_bed_allotment_id']);
									
									if($status=='Empty')
									{
										$image = 'bed_green';
										$button_type = '<a href="#allocate_ipdwardbed-box" class="allocate_ipdwardbed-window btn btn-success btn-sm allocate_ipdwardbed" id="'.$w.$wb.'">Allocate</a>';
										
										$transfer_button = '';
										
										$cancel_button = '';
									}
									if($status=='Occupied')
									{
										$image = 'bed_red';
										$button_type = '<a href="#discharge_ipdwardbed-box" class="discharge_ipdwardbed-window btn btn-danger btn-sm discharge_ipdwardbed" id="'.$w.$wb.'">Discharge</a>';
										
										$transfer_button = '<a href="#transfer_ipdwardbed-box" class="transfer_ipdwardbed-window btn btn-success btn-sm transfer_ipdwardbed" id="'.$w.$wb.'">Transfer</a>';
										
										$cancel_button = '';
									}
									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>";
									
									}
									?>
									
									<table width="100%" class="display table table-bordered table-striped">
									<tr>
                                        <td width="10%" align="left">
                                       <img src="../images/<?php echo @$image;?>.png" /><br />
                                        <span style="float:left;"><?php 
                                        $checkAllotOb = new classWardMgmt;
                                        if($checkAllotOb->checkWardBedAllotmentExist($ipd_bed_id))
                                        {
											?><?php
                                        }
                                        ?></span>
                                        </td>
                                        
                                        <td width="90%" align="left">Bed No: 
                                        
                                        <strong><?php echo $ipd_bed_no;?></strong>
                                        
                                        <span style="float:right;">Fee: Rs. <span style="font-size:18px;"><?php echo number_format($ipd_bed_fee,2);?></span>/hr</span><br />
                                        
                                        <span style="float:left;">Detail: <?php echo $ipd_bed_type;?></span>
                                        
                                        </td>
									</tr>
									</table>
									
									<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_no" class="set_to_pop" id="ipd_bed_no_<?php echo $w.$wb;?>" value="<?php echo $ipd_bed_no;?>" />
									<input type="hidden" name="ipd_bed_fee" class="set_to_pop" id="ipd_bed_fee_<?php echo $w.$wb;?>" value="<?php echo $ipd_bed_fee;?>" />
									<input type="hidden" name="ipd_bed_type" class="set_to_pop" id="ipd_bed_type_<?php echo $w.$wb;?>" value="<?php echo $ipd_bed_type;?>" />
									<input type="hidden" name="ipd_bedward_id" class="set_to_pop" id="ipd_bedward_id_<?php echo $w.$wb;?>" value="<?php echo $ipd_ward_id;?>" />
                                    <input type="hidden" name="ipd_bedward_no" class="set_to_pop" id="ipd_bedward_no_<?php echo $w.$wb;?>" value="<?php echo $ipd_ward_no;?>" />
                                    <input type="hidden" name="ipd_bedward_type" class="set_to_pop" id="ipd_bedward_type_<?php echo $w.$wb;?>" value="<?php echo $ipd_ward_type;?>" />
									<?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>

$(".update_ipdward").click(function() {
	
	var id = this.id;
	//alert(id);
	ipd_ward_id = $("#ipd_ward_id_"+id).val();
	ipd_ward_no = $("#ipd_ward_no_"+id).val();
	ipd_ward_type = $("#ipd_ward_type_"+id).val();
	staff_id = $("#staff_id_"+id).val();
	
	$("#ipd_ward_id_value").val(ipd_ward_id);
	$("#ipd_ward_no_value").val(ipd_ward_no);
	$("#ipd_ward_type_value").val(ipd_ward_type);
	$("#staff_id_value").val(staff_id);
	
});

$(".add_ipdwardbed").click(function() {
	
	var id = this.id;
	
	ipd_ward_id = $("#ipd_ward_id_"+id).val();
	ipd_ward_no = $("#ipd_ward_no_"+id).val();
	ipd_ward_type = $("#ipd_ward_type_"+id).val();
	
	//alert(ipd_ward_id);
	$("#ward_id_value").val(ipd_ward_id);
	$("#ward_type_value").html(ipd_ward_no+'-'+ipd_ward_type);
	
	
});

$(".update_ipdwardbed").click(function() {
	
	var id = this.id;
	//alert(id);
	ipd_bed_id = $("#ipd_bed_id_"+id).val();
	ipd_bed_no = $("#ipd_bed_no_"+id).val();
	ipd_bed_fee = $("#ipd_bed_fee_"+id).val();
	ipd_bed_type = $("#ipd_bed_type_"+id).val();
	
	ipd_bedward_id = $("#ipd_bedward_id_"+id).val();
	ipd_bedward_no = $("#ipd_bedward_no_"+id).val();
	ipd_bedward_type = $("#ipd_bedward_type_"+id).val();
	
	
	
	$("#ipd_bed_id_value").val(ipd_bed_id);
	$("#ipd_bed_no_value").val(ipd_bed_no);
	$("#ipd_bed_fee_value").val(ipd_bed_fee);
	$("#ipd_bedward_id").val(ipd_bedward_id);
	
	$("#ipd_bedward_type").html(ipd_bedward_no+'-'+ipd_bedward_type);
	$("#ipd_bed_type").val(ipd_bed_type);
	
});
</script>