<?php 
error_reporting(0);
include('accesscheck2.php');
require_once('Connections/courier.php'); 
require_once 'functions/functions.php';
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_courier, $courier);
$id = mysql_real_escape_string($_POST['id']);
$status = mysql_real_escape_string($_POST['status']);
if ($action == 'save_room_allocation' || $action == 'update_room_allocation') {
    $room_id = mysql_real_escape_string($_POST['room_id']);
    $doc_id = mysql_real_escape_string($_POST['doc_id']);
    $start_time = date('H:i', strtotime(mysql_real_escape_string($_POST['start_time_picker'])));
    $end_time = date('H:i', strtotime(mysql_real_escape_string($_POST['end_time_picker'])));
//    $error_msg = null;
if(!empty($room_id)&& !empty($doc_id)&&!empty($start_time)&&!empty($end_time)){
    //CHECK IF ATTENDING OFFICER IS AVAILABLE
    $room_details = mysql_fetch_assoc(mysql_query("SELECT room_no FROM consultation_rooms WHERE id = '$room_id'"));
    $doc_details = mysql_fetch_assoc(mysql_query("SELECT title,firstnames,surname,area_specialisation FROM users WHERE id = '$doc_id'"));
    $doc_full = $doc_details['title'] . ' ' . $doc_details['firstnames'] . ' ' . $doc_details['surname'];
    
    $check_today_avail = mysql_query("SELECT * FROM scheduler WHERE doctor_id = '$doc_id' AND start_date ='".date('Y-m-d')."' AND start_time ='$start_time' AND end_time= '$end_time'", $courier);
    $today_avail = mysql_num_rows($check_today_avail);
    if ($today_avail > 0) {
            if ($today_avail > 1) {} else {
                if ($action == 'save_room_allocation') {
                    mysql_query("INSERT INTO duty_roster(staff_no,staff_name,area_specialisation,duty_date,start_time,end_time,room_id,room_name,created_by) VALUES('$doc_id','" . $doc_full . "','" . $doc_details['area_specialisation'] . "','" . date('Y-m-d') . "','$start_time','$end_time','$room_id','" . $room_details['room_no'] . "','" . $_SESSION['myMM_Userid'] . "')", $courier);
                } else {
                    mysql_query("UPDATE duty_roster SET staff_no='$doc_id',staff_name='" . $doc_full . "',area_specialisation='" . $doc_details['area_specialisation'] . "',duty_date='" . date('Y-m-d') . "',start_time='$start_time',end_time='$end_time',room_id='$room_id',room_name='" . $room_details['room_no'] . "',created_by='" . $_SESSION['myMM_Userid'] . "' WHERE id = '$id' ", $courier);
                }
            }
        }
        else if($today_avail < 1){
        $error_msg = "Sorry <b>$doc_full</b> Wil Not Be On Duty On The Start and End Time You Selected.";
        $check_immediate_avail = mysql_query("SELECT * FROM scheduler WHERE doctor_id = '$doc_id' AND start_date >= '".date('Y-m-d')."' ORDER BY start_date, start_time LIMIT 1", $courier);
        $immediate_avail = mysql_num_rows($check_immediate_avail);
    }
       
}  
}else if($action == 'edit_room_allocation'){
    $edit = mysql_fetch_assoc(mysql_query("SELECT * FROM duty_roster WHERE id = '$id'", $courier));
}else if($action == 'delete_room_allocation'){
    mysql_query("DELETE FROM duty_roster WHERE id = '$id'", $courier);
}else if($action == 'finish_room_allocation'){
    mysql_query("UPDATE duty_roster SET status = '$status' WHERE id = '$id'");
}

$docs = mysql_query("SELECT * FROM users where (security='MEDICAL OFFICER' or security='OPTICIAN' or security='DENTIST') AND id NOT IN (SELECT staff_no FROM duty_roster WHERE status = 'In Progress' AND duty_date = '".date('Y-m-d')."') order by title, surname, firstnames", $courier);
$rooms = mysql_query("SELECT * FROM consultation_rooms WHERE status = 'available' AND id NOT IN (SELECT room_id FROM duty_roster WHERE status = 'In Progress' AND duty_date = '".date('Y-m-d')."') ORDER BY room_no", $courier);

?>

<div style="border: none" class="portlet box green">
    <div style="background:#108dbf" class="portlet-title">
        <div   class="caption"><i class="icon-globe"></i>Consulting Room Allocation </div>
        <div class="actions">
                <div class="btn-group">
                    <a class="btn btn-sm green dropdown-toggle" href="javascript:;" data-toggle="dropdown" aria-expanded="false"> Actions <i class="icon-angle-down"></i></a>
                    <ul class="dropdown-menu pull-right">
                        <li><a onclick="getstep('<?php echo $action; ?>','<?php echo $id;?>');" href="javascript:;"><i class="icon-refresh"></i> Refresh </a> </li>
                    </ul>
                </div>
            </div>
    </div>
    <div class="portlet-body">
        <?php if($today_avail < 1 && !empty($room_id)&& !empty($doc_id)&&!empty($start_time)&&!empty($end_time)){ ?>
        <div id="error_revealler" class="alert alert-danger alert-dismissable" style="border-radius: 0px">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
            <?php echo $error_msg;
            if($immediate_avail > 0){ ?>
            <br>Immediate Availability : 
            <ul id="time_schedule_laps">
                      <?php
                      while($r = mysql_fetch_assoc($check_immediate_avail)){ ?>
                 <li <?php if(strtotime(date('Y-m-d')) == strtotime($r['start_date'])){ ?> onclick="pickTimeInsert('<?php echo date('g:ia', strtotime($r['start_time'])).'@@@'.date('g:ia', strtotime($r['end_time']));?>')" <?php } ?>>
                                   <span>     <b><?php echo date('l, jS F Y', strtotime($r['start_date'])); ?></b>
                                      From:   <b><?php echo date('g:ia', strtotime($r['start_time'])); ?></b> To: 
                                        <b><?php echo date('g:ia', strtotime($r['end_time'])); ?></b>
                                   </span>
                        </li>
                     <?php } ?>  
            </ul>   
            <?php } ?>
        </div>
        <?php } ?>
        <?php if ($today_avail > 1) { ?>
        <div class="alert alert-danger alert-dismissable" style="border-radius: 0px">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
             <span style="font-weight: normal;"> <b><?php echo $doc_full."</b> has more than One Schedule <b>Today</b> based on the Time selected. Please Click to select the right Start and End Time."?></span> 
             <ul id="time_schedule_laps">
                      <?php
                      while($r = mysql_fetch_assoc($check_today_avail)){ ?>
                <li onclick="pickTimeInsert('<?php echo date('g:ia', strtotime($r['start_time'])).'@@@'.date('g:ia', strtotime($r['end_time']));?>')">
                           <span>     <b><?php echo date('l, jS F Y', strtotime($r['start_date'])); ?></b>
                              From:   <b><?php echo date('g:ia', strtotime($r['start_time'])); ?></b> To: 
                                <b><?php echo date('g:ia', strtotime($r['end_time'])); ?></b>
                           </span>
                </li>
                     <?php } ?>  
            </ul>
        </div>
        <?php } ?>
        
        <br>
        <form name="form" id="form" class="form-horizontal search_appointment_form" style="background-color: linen;margin-top: -10px;">
            <div class="form-body">
                <div class="row" style="margin-left:-10px;">
                    <div class="col-md-3">
                        <select required="" name="room_id" id="room_id" class="form-control">
                            <option value="">Select Consulting Room</option>
                             <?php if($action == 'edit_room_allocation'){ ?>
                            <option selected="" value="<?php echo $edit['room_id'];?>"><?php echo $edit['room_name']; ?></option>
                                <?php }
                            while($row = mysql_fetch_assoc($rooms)){ ?>
                                 <option <?php if($room_id == $row['id']){echo 'selected=""';} ?> value="<?php echo $row['id'];?>"><?php echo $row['room_no'];?></option>
                            <?php } ?>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <select required="" name="doc_id" id="doc_id" class="form-control">
                            <option value="">Select Attending Officer</option>
                            <?php if($action == 'edit_room_allocation'){ ?>
                            <option selected="" value="<?php echo $edit['staff_no'];?>"><?php echo $edit['staff_name'] . " (" . $edit['area_specialisation'] . ")"; ?></option>
                                <?php }
                            while($row = mysql_fetch_assoc($docs)){ ?>
                                 <option <?php if($doc_id == $row['id']){echo 'selected=""';} ?> value="<?php echo $row['id'];?>"><?php echo $row['title'] . " " . $row['surname'] . " " . $row['firstnames'] . " (" . $row['area_specialisation'] . ")"; ?></option>
                            <?php } ?>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <input type="text" required="" name="start_time_picker" id="start_time_picker" class="form-control time_picker" placeholder="Start Time" value="<?php if(!empty($edit['start_time'])){ echo date('g:ia',  strtotime($edit['start_time']));}?>">
                    </div>
                    <div class="col-md-2">
                        <input type="text" required="" name="end_time_picker" id="end_time_picker" class="form-control time_picker" placeholder="End Time" value="<?php if(!empty($edit['end_time'])) { echo date('g:ia',  strtotime($edit['end_time']));}?>">
                    </div>
                    <div class="col-md-2">
                        <?php if ($action == 'edit_room_allocation'){ ?>
                        <button onclick="Process_Room_Allocation('update_room_allocation','<?php echo $edit['id'];?>');return false;" class="btn green"><i class="icon-ok"></i> Update </button>
                        <?php } else { ?>
                        <button onclick="Process_Room_Allocation('save_room_allocation');return false;" class="btn green"><i class="icon-ok"></i> Save </button>
                        <?php } ?>
                    </div>
                </div>
            </div>
        </form>
        <br>
        <div class="row">
      <div class="col-md-9">
        <table class="table table-hover">
                        <tr>
                            <th style="background-color: #26a69a;color: #FFF;" class="count" width="4">&nbsp;</th>
                            <th style="background-color: #26a69a;color: #FFF;">Consulting Room</th>
                            <th style="background-color: #26a69a;color: #FFF;">Attending Officer</th>
                            <th style="background-color: #26a69a;color: #FFF;">Specialisation</th>
                            <th style="background-color: #26a69a;color: #FFF;;">Start</th>
                            <th style="background-color: #26a69a;color: #FFF;;">End</th>
                            <th style="background-color: #26a69a;color: #FFF;;">Status</th>
                            <th style="background-color: #26a69a;color: #FFF;;text-align: center;">Action</th>
                        </tr>
                <tbody>
                    <?php
                    $i=1;
                    $query_rsodest = mysql_query("SELECT * FROM duty_roster WHERE duty_date = '".date('Y-m-d')."'");
                    if(mysql_num_rows($query_rsodest) > 0){
                        while($row = mysql_fetch_assoc($query_rsodest)){ ?>
                    <tr style="cursor: pointer;">
                                <td><?php echo $i.'.'; ?></td>
                                <td><?php echo 'Consulting Room : '. $row['room_name']?></td>
                                <td><?php echo $row['staff_name']?></td>
                                <td><?php echo $row['area_specialisation']?></td>
                                <td><?php echo date('g:i a', strtotime($row['start_time'])); ?></td>
                                <td><?php echo date('g:i a', strtotime($row['end_time'])); ?></td>
                                <?php if($row['status']=='In Progress'){ ?>
                                <td onclick="Delete_In_Room_Allocation('finish_room_allocation','<?php echo $row['id']; ?>','You want to Finish this Room Allocation','Yes, Finish It!','Finished');" style="color: green;"><?php echo $row['status']?></td>
                                <?php } else { ?>
                                <td onclick="Delete_In_Room_Allocation('finish_room_allocation','<?php echo $row['id']; ?>','You want to Unfinished this Room Allocation','Yes, Unfinished It!','In Progress');" style="color: red;"><?php echo $row['status']?></td>
                                <?php } ?>
                                <td style="text-align: center;"> <i onclick="Process_Room_Allocation('edit_room_allocation','<?php echo $row['id'];?>');return false;" style="color: blue;" class="icon-edit"></i> &nbsp;&nbsp; <i onclick="Delete_In_Room_Allocation('delete_room_allocation','<?php echo $row['id']; ?>','You want to Delete this Room Allocation','Yes, Delete!');" style="color: red;" class="icon-trash"></i></td>
                    </tr>
                    <?php $i++; } } else{ ?>
                            <tr><td colspan="8" style="color: red;text-align: center;">Oops...No Appointments Found</td></tr>
                   <?php } ?>
                </tbody>
        </table>
            </div>
            <div class="col-md-3">
                <div class="picker__date-display" style="height:300px">
                    <div class="picker__weekday-display" style="font-size:1.8em;width: 100%;"><?php echo gmdate('l'); ?></div>
                    <div class="picker__month-display" style="font-size: 2.0em">
                        <div><?php echo gmdate('F') ?></div>
                    </div>
                    <div class="picker__day-display" style="font-size: 4.5em">
                        <div><?php echo gmdate('d'); ?></div>
                    </div>
                    <div class="picker__year-display" style="font-size:2.5em;width: 100%;"><div><?php echo gmdate('Y'); ?></div></div>
                </div>
            </div>
        </div>
        
    </div>
</div>


<div style="display: none;position: fixed;right:-400px;" id="pop_up_modal" class="pop_up_modal col-md-6">
    <div class="portlet white box" id="pop_up_modal_2">
       
    </div>
        <div class="form-actions right form_action_buttons" style="background-color: white;border: none;padding: 0;margin-top: -15px;">
                            <a id="delete_event" class="btn red"><i class="icon-trash"></i> Delete </a>
                            <a id="create_event" class="btn default"><i class="icon-ok-circle"></i> Save </a>
			</div>
</div>
