<?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_floor_allocation' || $action == 'update_floor_allocation') {
    $room_id = mysql_real_escape_string($_POST['room_id']);
    $doc_id = mysql_real_escape_string($_POST['doc_id']);
    $start_date = mysql_real_escape_string($_POST['schedule_start_date']);
    $end_date = mysql_real_escape_string($_POST['schedule_end_date']);
    
if(!empty($room_id)&& !empty($doc_id)&&!empty($start_date)&&!empty($end_date)){
    //CHECK IF ATTENDING OFFICER IS AVAILABLE
    $room_details = mysql_fetch_assoc(mysql_query("SELECT * FROM floors WHERE floor_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 ='$start_date'", $courier);
    $check_today_avail2 = mysql_query("SELECT * FROM scheduler WHERE doctor_id = '$doc_id' AND end_date ='$end_date'", $courier);
    $today_avail = mysql_num_rows($check_today_avail);
    $today_avail2 = mysql_num_rows($check_today_avail2);
    $floor_ds = mysql_fetch_assoc(mysql_query("SELECT * FROM floors WHERE floor_id = '$room_id' LIMIT 1", $courier));
    if ($today_avail > 0 && $today_avail2 > 0) {
            if ($action == 'save_floor_allocation') {
                $pick_all = mysql_query("SELECT * FROM scheduler WHERE doctor_id = '$doc_id' AND start_date = '$start_date' LIMIT 1", $courier);
                while ($rs = mysql_fetch_assoc($pick_all)) {
                    mysql_query("INSERT INTO duty_roster(staff_no,staff_name,area_specialisation,duty_date,end_date,start_time,end_time,building_id,block_id,floor_id,created_by) VALUES('$doc_id','" . $doc_full . "','" . $doc_details['area_specialisation'] . "','$start_date','$end_date','".$rs['start_time']."','".$rs['end_time']."','".$floor_ds['building_id']."','" . $floor_ds['block_id']. "','".$floor_ds['floor_id']."','" . $_SESSION['myMM_Userid'] . "')", $courier);
                    mysql_query("UPDATE floors SET status = 'not available' WHERE floor_id = '$room_id'", $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_date',end_time='$end_date',room_id='$room_id',room_name='" . $room_details['room_no'] . "',created_by='" . $_SESSION['myMM_Userid'] . "' WHERE id = '$id' ", $courier);
            }
        } else if($today_avail < 1 || $today_avail2 < 1){
        $error_msg = "Sorry <b>$doc_full</b> Will Not Be On Duty On The Start and End Date 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_floor_allocation'){
    $edit = mysql_fetch_assoc(mysql_query("SELECT * FROM duty_roster WHERE id = '$id'", $courier));
}else if($action == 'delete_floor_allocation'){
    mysql_query("DELETE FROM duty_roster WHERE id = '$id'", $courier);
}else if($action == 'finish_floor_allocation'){
    mysql_query("UPDATE duty_roster SET status = '$status' WHERE id = '$id'");
}

$docs = mysql_query("SELECT * FROM users where (security='NURSE') AND id NOT IN (SELECT staff_no FROM duty_roster WHERE status = 'In Progress') order by title, surname, firstnames", $courier);
$rooms = mysql_query("SELECT * FROM floors WHERE status = 'available' AND floor_id NOT IN (SELECT floor_id FROM duty_roster WHERE status = 'In Progress' AND duty_date = '".date('Y-m-d')."') ORDER BY floor_id", $courier);

?>

<div style="border: none" class="portlet box green">
    <div style="background:#108dbf" class="portlet-title">
        <div   class="caption"><i class="icon-globe"></i> Nurses' Duty Roster </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_date)&&!empty($end_date)){ ?>
        <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> 
                                      From: <b><?php echo date('l, jS F Y', strtotime($r['start_date'])); ?></b> To: 
                                        <b><?php echo date('l, jS F Y', strtotime($r['end_date'])); ?></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 Floor </option>
                             <?php if($action == 'edit_floor_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['floor_id']){echo 'selected=""';} ?> value="<?php echo $row['floor_id'];?>"><?php echo $row['floor_name'].' --- '.searchNameById('block', 'block_name', $row['block_id'],'block_id').' --- '.searchNameById('building', 'building_name', $row['building_id'],'building_id');?></option>
                            <?php } ?>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <select required="" name="doc_id" id="doc_id" class="form-control">
                            <option value="">Select Nurse</option>
                            <?php if($action == 'edit_floor_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" name="schedule_start_date" id="schedule_start_date" class="form-control date_time_picker" placeholder="Start Date" value="<?php if(!empty($edit['start_date'])){ echo $edit['start_date'];} ?>">
                    </div>
                    <div class="col-md-2">
                        <input type="text" name="schedule_end_date" id="schedule_end_date" class="form-control date_time_picker" placeholder="End Date" value="<?php if(!empty($edit['end_date'])){ echo $edit['end_date'];} ?>">
                    </div>
                    <div class="col-md-2">
                        <?php if ($action == 'edit_floor_allocation'){ ?>
                        <button onclick="Process_Room_Allocation('update_floor_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_floor_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;">Floor Name</th>
                            <th style="background-color: #26a69a;color: #FFF;">Nurse</th>
                            <th style="background-color: #26a69a;color: #FFF;">Start Date</th>
                            <th style="background-color: #26a69a;color: #FFF;">End Date</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')."' AND status = 'In Progress'");
                    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 searchNameById('floors', 'floor_name', $row['floor_id'],'floor_id').' --- '.searchNameById('block', 'block_name', $row['block_id'],'block_id');?></td>
                                <td><?php echo $row['staff_name']?></td>
                                <td><?php echo date('l, jS F Y, g:i a', strtotime($row['duty_date'].' '.$row['start_time'])); ?></td>
                                <td><?php echo date('l, jS F Y, g:i a', strtotime($row['end_date'].' '.$row['end_time'])); ?></td>
                                <?php if($row['status']=='In Progress'){ ?>
                                <td onclick="Delete_In_Room_Allocation('finish_floor_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_floor_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_floor_allocation','<?php echo $row['id'];?>');return false;" style="color: blue;" class="icon-edit"></i> &nbsp;&nbsp; <i onclick="Delete_In_Room_Allocation('delete_floor_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 Allocation 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>
