<?php 
include('../config/config.php');
include("../classes/classShift.php");
/*echo '<pre>';
print_r($_SESSION);
echo '</pre>';
*/
if(isset($_SESSION['temp']['diagshiftid']))
{
	$shiftid=$_SESSION['temp']['diagshiftid'];
}

/////////////////////////////Close Shift////////////////////////

if(isset($_GET['status']))
{
	$status=$_GET['status'];
	
	if($status=='close')
	{	
		try
		{	
			$closeShiftOb = new classShift;
			$closeShiftOb->setShiftStatus($status);
			$closeShiftOb->setShiftEndTime(time());
			$closeShiftOb->setShiftID($shiftid);
			$closeShiftOb->closeShift();
			
			unset($_SESSION['temp']['diagshiftid']);
			
			header("Location:mgmtdiagshifts.php");
		}
		catch (MyException $e)
		{
			$error_message = $e->getUserMsg();
		}	
	}
}

/////////////////////////////Add New Shift////////////////////////

if(isset($_POST["saveshift"]) && $_POST["saveshift"] == "Ceate Shift")
{	
	try
	{
		$shiftCreateOb = new classShift;
		$shiftCreateOb->setCreatedBy($_SESSION['security']['user_id']);
		$shiftCreateOb->setShiftStatus('open');
		$shiftCreateOb->setUserName($_SESSION['security']['user_name']);
		$shiftCreateOb->setLocationID($_SESSION['security']['location_id']);
		$shiftCreateOb->setShiftType('diagnostic');
		$shiftCreateOb->execute();
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
	}	
}

/////////////////////////////Re-open SHift////////////////////////

if(isset($_GET['openshift']))
{
	$openshift=base64_decode($_GET['openshift']);
			
	if($openshift!='')
	{
		try
		{	
			$reopenShiftOb = new classShift;
			$reopenShiftOb->setShiftStatus('open');
			$reopenShiftOb->setShiftID($openshift);
			$reopenShiftOb->reOpenShift();
		}
		catch (MyException $e)
		{
			$error_message = $e->getUserMsg();
		}	
	}
}
/////////////////////////////Delete shift////////////////////////

if(isset($_GET['rec_delete']))
{
	$rec_delete = base64_decode($_GET['rec_delete']);
		
	try
	{	
		$delShiftOb = new classShift;
		$delShiftOb->setShiftID($rec_delete);
		$delShiftOb->deleteShift();
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
	}	
}

	


/////////////////////////////Retrieve Shifts////////////////////////

try
{
	$retrieveShiftOB = new classShift;
	
	$retrieveShiftOB->setLocationID($_SESSION['security']['location_id']);
	$retrieveShiftOB->setShiftType('diagnostic');
	
	if($_SESSION['security']['userrole_id']==1||$_SESSION['security']['userrole_id']==2||$_SESSION['security']['userrole_id']==3||$_SESSION['security']['userrole_id']==9)
	{
		$shift_array = $retrieveShiftOB->retrieveAllShifts();
	}
	
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
}	

################################################################################
#########################Pagination Starts here. {########################
################################################################################


$array['item_count'] = count($shift_array);
if(isset($_GET['page']) && isset($_GET['items']))
{
	if($array['item_count'] > $_GET['items'])
	{
		$array['pages'] = ceil($array['item_count']/$_GET['items']);
		if($_GET['page'] >$array['pages'])
		{
			$_GET['page'] = 1;
		}
		$array['current_page'] = $_GET['page'];
		if($_GET['page'] > 1)
		{
			$starting_record = $_GET['items']*($_GET['page']-1);
			$ending_record = $_GET['items'];
			$array['limit'] = $starting_record . "," . $ending_record;
			$limit = $array['limit'];
		}
		else
		{
			$limit = $_GET['items'];
		}
	}
	else
	{
		$array['current_page'] = $_GET['current_page'] = 1;
		$array['pages'] = 1;
		$array['limit'] = $_GET['items'];
		$limit = $array['limit'];
	}
}
else
{
	if($array['item_count'] > 20)
	{
		$array['pages'] = ceil($array['item_count']/20);
	}
	else
	{
		$array['pages'] = 1;
	}
	$array['current_page'] = 1;
	$array['limit'] = 20;
	$limit = $array['limit'];
}

$_SESSION['pagination']['item_count']=$array['item_count'];
$_SESSION['pagination']['pages']=$array['pages'];
$_SESSION['pagination']['current_page']=$array['current_page'];
$_SESSION['pagination']['limit']=$limit;

################################################################################
######################### Pagination Ends here. }##########################
################################################################################

/////////////////////////////Retrieve Shifts////////////////////////

try
{
	$retrieveShiftOB = new classShift;
	
	$retrieveShiftOB->setLocationID($_SESSION['security']['location_id']);
	$retrieveShiftOB->setShiftType('diagnostic');
	
	if($_SESSION['security']['userrole_id']==1||$_SESSION['security']['userrole_id']==2||$_SESSION['security']['userrole_id']==3||$_SESSION['security']['userrole_id']==9)
	{
		$shift_array = $retrieveShiftOB->retrieveAllShifts($limit);
	}
	
}
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"><!-- 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" -->DOCTORS' SHIFTS<!-- InstanceEndEditable --></h2>
            </header>

  <br />
  
<!-- InstanceBeginEditable name="main" -->
  		<!-- page start-->
        <div class="row">
        
        <header class="panel-heading"></header>
        
        <p>
        <?php
        if(isset($error_message)&&$error_message!='')
        {
			?>
			<span class="message_red"><?php echo $error_message;?></span>
			<?php
        }
        ?>
        </p>
        <table width="100%" cellpadding="0" cellspacing="0" class="bottomBorder">
                
                  <thead>
                    <tr>
                    
                      <th width="5%" align="center">ID</th>
                      <th width="10%" align="center">Date</th>
                      <th width="15%">Start Time</th>
                      <th width="15%">End Time</th>
                      <th width="20%">Created By</th>
                    </tr>
                  </thead>
                  <tbody>
					<?php
                    if(isset($shift_array[0]['shift_id'])&&$shift_array[0]['shift_id']!='')
                    {
						
						for($s=0;$s<count($shift_array);$s++)
						{
							if($s%2==0){$class = 'gradeX';}{$class = 'gradeC';}
							
							$shift_id = $shift_array[$s]['shift_id'];
							$shift_status = $shift_array[$s]['shift_status'];
							$shift_id_encoded = base64_encode($shift_id);
							
							
							
							
							$query = "SELECT count(*) FROM diagnostic_invoice WHERE shift_id='$shift_id'";
                  			$queryOb = new classQueryGlobal;
							$resultCheckVisitsExistOrNot = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
							
							if ($shift_status=="open") 
							{
								 $enter_shift_link  = "<a href='mgmttests.php?shiftid=$shift_id_encoded'>Enter Shift</a>";
							}
							else
							{
								$enter_shift_link = '';
							}
							
							if (($shift_status=="close") && ($_SESSION['security']['userrole_id']==1||$_SESSION['security']['userrole_id']==2||$_SESSION['security']['userrole_id']==9))
							{
								$reopen_link = "<a href='mgmtdiagshifts.php?openshift=$shift_id_encoded'>Re-Open Shift</a>";
							}
							else
							{ 
								$reopen_link = '';
							} 
							
							if ($shift_status=="open" && (isset($resultCheckVisitsExistOrNot[0]['count(*)'])&&$resultCheckVisitsExistOrNot[0]['count(*)']==0)) 
							{
								$delete_view_link = "<a href='mgmtdiagshifts.php?rec_delete=$shift_id_encoded'><img src='../images/delete.png' alt='Delete'/></a>";
							} 
							else 
							{ 
								$delete_view_link = "<a href='mgmttestsvw.php?shiftid=$shift_id_encoded'>View</a>"; 
							}
							
							$userOb = new classUser;
							$userOb->retrieveUserAllDataByUserID($shift_array[$s]['created_by']);
							?>
							<tr class="<?php echo $class;?>">
                            
								<td align="center"><?php echo $shift_id;?></td>
								<td align="center"><?php echo date('d M Y',$shift_array[$s]['date']);?></td>
								<td align="center"><?php echo date('h:i A',$shift_array[$s]['start_time']);?></td>
								<td align="center"><?php if($shift_array[$s]['end_time']!=''){echo  date('h:i A',$shift_array[$s]['end_time']);}?></td>
								<td align="center"><?php echo $userOb->getFirstName().' '.$userOb->getLastName();?></td>
								
							</tr>
							<?php
						}
                    }
					else
					{
						?>
                        <tr>
                            <table>
                                <tr>
                                    <td>No entry found.</td>
                                </tr>
                            </table>
                        </tr>
                        <?php
					}
                    ?>
                  </tbody>
                 
                </table>    
        
        
        </div>
        <!-- page end-->
        
        <p align="center" style="font-size:12px; font-family:Arial, Helvetica, sans-serif; margin-top:260px;">
        <strong>(Not Valid for Court or any Legal Purpose)</strong><br />
        For Appointment call at 042-35151666 (bring your Patient ID Card)
        </p>
        
<!-- 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>