<?php 
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);
$searchtxt=$_POST["searchtext"];
$typesearch=$_POST["typesearch"];
$datatable_products_length = $_POST['datatable_products_length'];
if(empty($datatable_products_length)){$datatable_products_length = 50;}

$membership_no=$_POST["membership_no"];
$insurance_company=$_POST["insurance_company"];
$id=$_POST["id"];
$branch_id = $_SESSION['branch_id'];

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 = "SELECT * FROM scheduler WHERE type = 'appointment' AND patient_id > 0 AND status = 'Pending' AND $search ORDER BY start_date, start_time";
  
  }  
    }else{
  $date = date('Y-m-d');
  if($action == 'list_of_appointments_today'){
  $query_rsodest = "SELECT * FROM scheduler WHERE type = 'appointment' AND patient_id > 0 AND status = 'Pending' AND start_date = '$date' ORDER BY start_date, start_time";
  }else{
  $query_rsodest = "SELECT * FROM scheduler WHERE type = 'appointment' AND patient_id > 0 AND status = 'Pending' ORDER BY start_date, start_time";
  }
  }
  $qry = mysql_query("SELECT * FROM users where (security='MEDICAL OFFICER' or security='OPTICIAN' or security='DENTIST') order by title, surname, firstnames", $courier);
  

$pagerows = $datatable_products_length;
$rsodest = mysql_query($query_rsodest, $courier) or die(mysql_error());
$totalRows_rsodest = mysql_num_rows($rsodest);

$total_pages = ceil($totalRows_rsodest / $pagerows);
$pagenum = $_POST['page_num'];
if (!(isset($pagenum))) {
    $pagenum = 1;
}
if ($pagenum < 1) {
    $pagenum = 1;
} elseif ($pagenum > $total_pages) {
    $pagenum = $total_pages;
}
$start_row = (($pagenum - 1) * $pagerows) + 1;
$end_row = $start_row + $pagerows;

if ($end_row > $totalRows_rsodest) {
    $end_row = $totalRows_rsodest;
}
$rsodest = mysql_query($query_rsodest . ' limit ' . (($pagenum - 1) * $pagerows) . ',' . $pagerows);
if ($totalRows_rsodest > 0) {
    // $row_rsodest = mysql_fetch_assoc($rsodest);
} else {
    $start_row = 0;
}

?>


<div class="page-bar">
                        <ul class="page-breadcrumb">
                            <li>
                                <i class="icon-home"></i>
                                <a href="index.html">Home</a>
                                <i class="fa fa-angle-right"></i>
                            </li>
                            <li>
                                <span>Front Desk</span>
                                <i class="fa fa-angle-right"></i>
                            </li>
                            <li>
                                <span>List of Appointments</span>
                            </li>
                        </ul>
                        <div class="page-toolbar">
                            <div class="btn-group pull-right">
                                <button type="button" class="btn btn-fit-height grey-salt dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-delay="1000" data-close-others="true"> Actions
                                    <i class="fa fa-angle-down"></i>
                                </button>
                                <ul class="dropdown-menu pull-right" role="menu">
                                    <li id="refresh_items" onclick="getstep('list_of_appointments_today');">
                                        <a href="#">
                                            <i class="icon-refresh"></i> Refresh</a>
                                    </li>
                                    <li id="refresh_items" onclick="getstep('list_of_appointments');">
                                        <a href="#">
                                            <i class="icon-list"></i> All Appointments</a>
                                    </li>
                                </ul>
                            </div>
                        </div>
</div>

<div class="row">
    <div class="col-lg-12 col-md-12 col-xs-12 col-sm-12">
        <div class="portlet light ">
            <div class="portlet-title">
                <div class="caption">
                    <span class="caption-subject bold uppercase font-dark">LIST OF APPOINTMENTS</span>
                </div>
            </div>
            <div class="portlet-body form">
                    <div class="table-container">
                        <div id="datatable_products_wrapper" class="dataTables_wrapper dataTables_extended_wrapper no-footer">
                            <div class="row">
                                <div class="col-md-7 col-sm-12">
                                    <div class="dataTables_paginate paging_bootstrap_extended" id="datatable_products_paginate">
                                        <div class="pagination-panel"> Page 
                                            <a onClick="getpage('<?php echo $action?>','1','<?php echo $totalRows_rsodest; ?>','','<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>')" href="#" class="btn btn-sm default prev" title="First"><i class="fa fa-angle-double-left"></i></a>
                                            <a onClick="getpage('<?php echo $action?>','<?php echo ($pagenum-1); ?>','<?php echo $totalRows_rsodest; ?>','','<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>')" href="#" class="btn btn-sm default prev" title="Prev"><i class="fa fa-angle-left"></i></a>
                                            <input readonly="" value="<?php echo $pagenum; ?>" type="text" class="pagination-panel-input form-control input-mini input-inline input-sm" maxlenght="5" style="text-align:center; margin: 0 5px;">
                                            <a onClick="getpage('<?php echo $action?>','<?php echo ($pagenum+1); ?>','<?php echo $totalRows_rsodest; ?>','','<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>')" href="#" class="btn btn-sm default next" title="Next"><i class="fa fa-angle-right"></i></a> 
                                            <a onClick="getpage('<?php echo $action?>','<?php echo $total_pages; ?>','<?php echo $totalRows_rsodest; ?>','','<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>')" href="#" class="btn btn-sm default next" title="Next"><i class="fa fa-angle-double-right"></i></a> 
                                            of <span class="pagination-panel-total"><?php echo $total_pages;?></span>
                                        </div>
                                    </div>
                                    <div class="dataTables_length" id="datatable_products_length">
                                     <form name="form" id="form" method="post">
                                        <label> 
                                            <span class="seperator">|</span>View By 
                                            <select onchange="getstep('<?php echo $action; ?>');" name="datatable_products_length" id="datatable_products_length" aria-controls="datatable_products" class="form-control input-xsmall input-sm input-inline">
                                                <option <?php if($pagerows == 10){echo 'selected=""';} ?> value="10">10</option>
                                                <option <?php if($pagerows == 20){echo 'selected=""';} ?> value="20">20</option>
                                                <option <?php if($pagerows == 50){echo 'selected=""';} ?> value="50">50</option>
                                                <option <?php if($pagerows == 100){echo 'selected=""';} ?> value="100">100</option>
                                                <option <?php if($pagerows == 150){echo 'selected=""';} ?> value="150">150</option>
                                            </select> 
                                            records</label>
                                        <span class="seperator">|</span> Found : <b><?php echo $totalRows_rsodest; ?> records </b> 
                                    </form>
                                    </div>
                                </div>
                                <div class="col-md-5 col-sm-12">
                                    <div class="table-group-actions pull-right">
                                        <form name="form" id="form" method="post" action="" onsubmit="getstep('<?php echo $action; ?>'); return false;">
                                        <input type="text" name="searchtext" id="searchtext" class="table-group-action-input form-control input-inline input-small input-sm">
                                        <select class="table-group-action-input form-control input-inline input-small input-sm" name="typesearch" id="typesearch">
                                            <option value="">Select...</option>
                                              <?php if($action!='list_guest_patients'){ ?>
                                                     <option value="patientid" <?php if($typesearch=="patientid" ||($typesearch=='')){echo "selected='selected'";}?>>Patient ID/Old Card No.</option>
                                                     <option value="member_no"  <?php if(($typesearch=="member_no")){echo "selected='selected'";}?>>Insurance/Staff No.</option> <?php } ?>
                                                     <option value="surname"  <?php if($typesearch=="surname"){echo "selected='selected'";}?>>Surname</option>
                                                     <option value="othernames" <?php if($typesearch=="othernames"){echo "selected='selected'";}?>>Othernames</option>
                                        </select>
                                        <button class="btn btn-sm green table-group-action-submit"><i class="fa fa-search"></i> Search </button>
                                        </form>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="table-scrollable">
                                <table class="table table-striped table-bordered dataTable no-footer" id="datatable_products" aria-describedby="datatable_products_info" role="grid">
                                     <thead>
                                     <tr>
                                     <th class="count" width="4">&nbsp;</th>
                                     <th>Patient Name</th>
                                     <th>Patient ID</th>
                                     <th>Date</th>
                                     <th>Start Time</th>
                                     <th>End Time</th>
                                     <th>Action</th>
                                 </tr>
                                    </thead>
                                    <tbody>
                                         <?php if($totalRows_rsodest>0){ 
                                             $i=1;
                                    while ($row_rsodest = mysql_fetch_assoc($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_rsodest['patient_name']?></td>
                <td onclick="$('.appointment_<?php echo $i;?>').slideToggle('fast');"><?php echo $row_rsodest['patient_no']?></td>
                <td onclick="$('.appointment_<?php echo $i;?>').slideToggle('fast');"><?php if($row_rsodest['start_date'] == date('Y-m-d')){echo 'TODAY, '.date('jS F Y', strtotime($row_rsodest['start_date']));}else{ echo date('l, jS F Y', strtotime($row_rsodest['start_date']));} ?></td>
                <td onclick="$('.appointment_<?php echo $i;?>').slideToggle('fast');"><?php echo date('g:i a', strtotime($row_rsodest['start_time'])); ?></td>
                <td onclick="$('.appointment_<?php echo $i;?>').slideToggle('fast');"><?php echo date('g:i a', strtotime($row_rsodest['end_time'])); ?></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_rsodest['patient_id'].'@@@'.$row_rsodest['patient_no'].'@@@'.$row_rsodest['patient_name'];?>">
                                    <u>ATTENDING OFFICER</u> : <?php echo UserFullName($row_rsodest['doctor_id'], $courier);?>
                                </div>
                                <div class="todo-tasklist-item-text">
                                    Desc : <?php echo $row_rsodest['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_rsodest['start_date'].' '.$row_rsodest['start_time'])).' - '.date('g:i a', strtotime($row_rsodest['end_time']));?> </span></i>
                                    <?php if($row_rsodest['start_date'] == date('Y-m-d')){ ?>
                                    <label onclick="getstep('view_patient_visits','<?php echo $row_rsodest['patient_id']."@@@".$row_rsodest['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_rsodest['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 { echo '<tr><td colspan="13" style="color:red;text-align:center;">No Records Found.</td></tr>';} ?>
                                    </tbody>
                                </table>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-7 col-sm-12">
                                    <div class="dataTables_paginate paging_bootstrap_extended" id="datatable_products_paginate">
                                        <div class="pagination-panel"> Page 
                                            <a onClick="getpage('<?php echo $action?>','1','<?php echo $totalRows_rsodest; ?>','','<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>')" href="#" class="btn btn-sm default prev" title="First"><i class="fa fa-angle-double-left"></i></a>
                                            <a onClick="getpage('<?php echo $action?>','<?php echo ($pagenum-1); ?>','<?php echo $totalRows_rsodest; ?>','','<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>')" href="#" class="btn btn-sm default prev" title="Prev"><i class="fa fa-angle-left"></i></a>
                                            <input readonly="" value="<?php echo $pagenum; ?>" type="text" class="pagination-panel-input form-control input-mini input-inline input-sm" maxlenght="5" style="text-align:center; margin: 0 5px;">
                                            <a onClick="getpage('<?php echo $action?>','<?php echo ($pagenum+1); ?>','<?php echo $totalRows_rsodest; ?>','','<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>')" href="#" class="btn btn-sm default next" title="Next"><i class="fa fa-angle-right"></i></a> 
                                            <a onClick="getpage('<?php echo $action?>','<?php echo $total_pages; ?>','<?php echo $totalRows_rsodest; ?>','','<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>')" href="#" class="btn btn-sm default next" title="Next"><i class="fa fa-angle-double-right"></i></a> 
                                            of <span class="pagination-panel-total"><?php echo $total_pages;?></span>
                                        </div>
                                    </div>
                                    <div class="dataTables_length" id="datatable_products_length">
                                        <label> 
                                            <span class="seperator">|</span>View By 
                                            <select onchange="getstep('<?php echo $action; ?>');" name="datatable_products_length_2" id="datatable_products_length_2" aria-controls="datatable_products" class="form-control input-xsmall input-sm input-inline">
                                                <option <?php if($pagerows == 10){echo 'selected=""';} ?> value="10">10</option>
                                                <option <?php if($pagerows == 20){echo 'selected=""';} ?> value="20">20</option>
                                                <option <?php if($pagerows == 50){echo 'selected=""';} ?> value="50">50</option>
                                                <option <?php if($pagerows == 100){echo 'selected=""';} ?> value="100">100</option>
                                                <option <?php if($pagerows == 150){echo 'selected=""';} ?> value="150">150</option>
                                            </select> 
                                            records</label>
                                        <span class="seperator">|</span> Found : <b><?php echo $totalRows_rsodest; ?> records </b> </div>
                                </div>
                                <div class="col-md-5 col-sm-12"></div>
                            </div>
                        </div>
                    </div>

            </div>
        </div>
    </div>
</div>


<div class="modal fade bs-modal-lg" id="large" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                <h4 class="modal-title">Job Activities</h4>
            </div>
            <div class="modal-body" id="job_activities_wrapper">

            </div>
            <div class="modal-footer">
                <button type="button" class="btn red btn-outline" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>


<div class="modal fade bs-modal-lg" id="pop_up_modal" tabindex="-1" role="dialog" aria-hidden="true">
                                        <div class="modal-dialog modal-lg">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                                                    <h4 class="modal-title">Appointment Booking / Events</h4>
                                                </div>
                                                <div class="modal-body" id="pop_up_modal_2"></div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn dark btn-outline" data-dismiss="modal">Close</button>
                                                    <button id="delete_event" type="button" class="btn red btn-outline">Delete</button>
                                                    <button id="create_event" type="button" class="btn green">Save changes</button>
                                                </div>
                                            </div>
                                        </div>
</div>