<?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);

if($action == 'search_for_appointments'){
  $search_text = mysql_real_escape_string($_POST['search_text']);  
  $search_by = mysql_real_escape_string($_POST['search_by']);  
  $schedule_start_date = mysql_real_escape_string($_POST['schedule_start_date']);  
  $start_time_picker = date('H:i', strtotime(mysql_real_escape_string($_POST['start_time_picker'])));  
  $doctor_id = mysql_real_escape_string($_POST['doctor_id']);  
  $urgency = mysql_real_escape_string($_POST['urgency']);  
  if(!empty($search_by)){
  if($search_by == 'doctor_id'){
     $search = "doctor_id=$doctor_id";
  }else if($search_by == 'start_date_time'){
     $search = "start_date='$schedule_start_date' AND start_time='$start_time_picker'";
  }else if($search_by == 'urgency'){
     $search = "urgency='$urgency'";
  }
  else{
     $search =  "$search_by LIKE '%$search_text%'";
  }
    $query_rsodest = mysql_query("SELECT * FROM scheduler WHERE type = 'appointment' AND patient_id > 0 AND status = 'Pending' AND $search ORDER BY start_date, start_time", $courier);

}  
  }else{
$date = date('Y-m-d');
if($action == 'list_of_appointments_today'){
$query_rsodest = mysql_query("SELECT * FROM scheduler WHERE type = 'appointment' AND patient_id > 0 AND status = 'Pending' AND start_date = '$date' ORDER BY start_date, start_time", $courier);
}else{
$query_rsodest = mysql_query("SELECT * FROM scheduler WHERE type = 'appointment' AND patient_id > 0 AND status = 'Pending' ORDER BY start_date, start_time", $courier);
}
}

$qry = mysql_query("SELECT * FROM users where (security='MEDICAL OFFICER' or security='OPTICIAN' or security='DENTIST') order by title, surname, firstnames", $courier); ?>


<div style="border: none" class="portlet box green">
    <div style="background:#108dbf" class="portlet-title">
        <div   class="caption"><i class="icon-globe"></i>List of Patients Appointments </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('list_of_appointments_today');" href="javascript:;"><i class="icon-refresh"></i> Refresh </a> </li>
                        <li><a onclick="getstep('list_of_appointments');" href="javascript:;"><i class="icon-list"></i> All Appointments </a> </li>
                        <li><a onclick="$('.search_appointment_form').slideToggle('fast');" href="#"><i class="icon-search"></i> Search </a> </li>
                    </ul>
                </div>
            </div>
    </div>
    <div class="portlet-body">
        <form name="form" id="form" class="form-horizontal search_appointment_form" style="display: none;">
            <div class="form-body">
                <div class="row" style="margin-left:-20px;">
                    <div class="col-md-5 search_text hide_all_appointment">
                        <input type="text" name="search_text" id="search_text" class="form-control" placeholder="Search Text">
                    </div>
                    <div class="col-md-5 search_urgency hide_all_appointment" style="display: none">
                        <select name="urgency" id="urgency" class="form-control">
                                   <option value="">Select Appointment Urgency</option>
                                   <option value="Normal">Normal</option>   
                                   <option value="Urgent">Urgent</option>   
                        </select>
                    </div>
                    <div class="col-md-5 search_doctor hide_all_appointment" style="display: none">
                        <select name="doctor_id" id="doctor_id" class="form-control">
                             <option value="">Select Attending Officer</option>
                                            <?php while ($row = mysql_fetch_assoc($qry)) { ?>
                                                <option value="<?php echo $row['id']; ?>"><?php echo $row['title'] . " " . $row['surname'] . " " . $row['firstnames'] . " (" . $row['area_specialisation'] . ")"; ?></option>   
                            <?php } ?>    
                        </select>
                    </div>
                    <div class="col-md-3 pick_date_time hide_all_appointment" style="display: none;">
                        <input type="text" name="schedule_start_date" id="schedule_start_date" class="form-control date_time_picker" placeholder="Start Date">
                    </div>
                    <div class="col-md-3 pick_date_time hide_all_appointment" style="display: none;">
                        <input type="text" name="start_time_picker" id="start_time_picker" class="form-control time_picker" placeholder="Start Time">
                    </div>
                    <div class="col-md-4">
                        <select required="" onchange="$('.hide_all_appointment').hide();if(this.value=='start_date_time'){$('.pick_date_time').show();}else if(this.value=='doctor_id'){$('.search_doctor').show();}else if(this.value=='urgency'){$('.search_urgency').show();}else{$('.search_text').show();}" name="search_by" id="search_by" class="form-control">
                            <option value=""> Search By</option>
                            <option value="patient_name"> Patient Name </option>
                            <option value="patient_no"> Patient ID </option>
                            <option value="urgency"> Appointment Urgency </option>
                            <option value="doctor_id"> Attending Officer </option>
                            <option value="start_date_time"> Appointment Date & Time  </option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <button onclick="getstep('search_for_appointments');return false;" class="btn green"><i class="icon-search"></i> Search </button>
                    </div>
                </div>
            </div>
        </form>
        <table class="table table-hover table-striped table-bordered">
                    <thead>
                        <tr>
                            <th style="background-color: #FBB;color: #000; font-weight:bold" class="count" width="4">&nbsp;</th>
                            <th style="background-color: #FBB;color: #000; font-weight:bold">Patient Name</th>
                            <th style="background-color: #FBB;color: #000; font-weight:bold">Patient ID</th>
                            <th style="background-color: #FBB;color: #000; font-weight:bold">Date</th>
                            <th style="background-color: #FBB;color: #000; font-weight:bold;">Start Time</th>
                            <th style="background-color: #FBB;color: #000; font-weight:bold;">End Time</th>
                            <th style="background-color: #FBB;color: #000; font-weight:bold;text-align: center;">Urgency</th>
                            <th style="background-color: #FBB;color: #000; font-weight:bold;text-align: center;">Action</th>
                        </tr>
                    </thead>
                <tbody>
                    <?php
                    $i=1;
                    if(mysql_num_rows($query_rsodest) > 0){
                        while($row = mysql_fetch_assoc($query_rsodest)){ ?>
                    <tr style="cursor: pointer;">
                                <td onclick="$('.appointment_<?php echo $i;?>').slideToggle('fast');"><?php echo $i.'.'; ?></td>
                                <td onclick="$('.appointment_<?php echo $i;?>').slideToggle('fast');"><?php echo $row['patient_name']?></td>
                                <td onclick="$('.appointment_<?php echo $i;?>').slideToggle('fast');"><?php echo $row['patient_no']?></td>
                                <td onclick="$('.appointment_<?php echo $i;?>').slideToggle('fast');"><?php if($row['start_date'] == date('Y-m-d')){echo 'TODAY, '.date('jS F Y', strtotime($row['start_date']));}else{ echo date('l, jS F Y', strtotime($row['start_date']));} ?></td>
                                <td onclick="$('.appointment_<?php echo $i;?>').slideToggle('fast');"><?php echo date('g:i a', strtotime($row['start_time'])); ?></td>
                                <td onclick="$('.appointment_<?php echo $i;?>').slideToggle('fast');"><?php echo date('g:i a', strtotime($row['end_time'])); ?></td>
                                <td onclick="$('.appointment_<?php echo $i;?>').slideToggle('fast');" style="text-align: center;color: red;font-weight: bold;"><span class="badge badge-<?php if($row['urgency']=='Normal'){echo 'success';}else{echo 'danger';} ?>"><?php echo strtoupper($row['urgency']); ?></span></td>
                                <td onclick="$('.appointment_<?php echo $i;?>').slideToggle('fast');" style="text-align:center;font-weight: bold;color: green;">View Details</td>
                            </tr>
                            <tr style="display: none;" class="appointment_<?php echo $i;?>">
                                <td colspan="8">
                                    <div style="display: none;" class="todo-tasklist appointment_<?php echo $i;?>" id="appointment_<?php echo $i;?>">
                                            <div class="todo-tasklist-item todo-tasklist-item-border-green" id="4002" style="color: rgb(255, 255, 255); background-color: rgb(140, 225, 150);">
                                                <div style="font-size:12px;" class="todo-tasklist-item-title" id="<?php echo $row['patient_id'].'@@@'.$row['patient_no'].'@@@'.$row['patient_name'];?>">
                                                    <u>ATTENDING OFFICER</u> : <?php echo UserFullName($row['doctor_id'], $courier);?>
                                                </div>
                                                <div class="todo-tasklist-item-text">
                                                    Desc : <?php echo $row['description'];?>
                                                </div>
                                                <div class="todo-tasklist-controls pull-right">
                                                    <i style="font-weight: bold;"><span class="todo-tasklist-date"><i class="icon-calendar"></i> <?php echo date('l, jS F Y g:i a', strtotime($row['start_date'].' '.$row['start_time'])).' - '.date('g:i a', strtotime($row['end_time']));?> </span></i>
                                                    <?php if($row['start_date'] == date('Y-m-d')){ ?>
                                                    <label onclick="getstep('view_patient_visits','<?php echo $row['patient_id']."@@@".$row['doctor_id'];?>');" style="background-color: #FFA000;cursor: pointer;" class="todo-tasklist-badge badge danger badge-roundless badge-danger">Go To New Visit Page</label>
                                                   <?php } ?>
                                                    <label onclick="ShowEventDiv('<?php echo $row['id']?>','pop_up_modal_2','fetch_single');" style="background-color: #FFA000;cursor: pointer;" class="todo-tasklist-badge badge badge-roundless">Change Appointment</label>
                                                </div>

                                            </div>
                                        </div> 
                                </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>

<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>