<?php 
if (!isset($_SESSION)){
  session_start();
}
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);
$typesearch=$_POST["typesearch"];
$searchtxt=$_POST["searchtext"];


if($action=='search_new_patient_visit' && $searchtxt!=''){
	if($typesearch=='surname'){
   $query_rsodest = "SELECT a.*, DATE_FORMAT(a.dateofbirth, '%d %b %Y') as dob FROM patient_information a where surname like '$searchtxt%' order by surname, othernames";	
 }
 if($typesearch=='othernames'){
   $query_rsodest = "SELECT a.*, DATE_FORMAT(a.dateofbirth, '%d %b %Y') as dob FROM patient_information a where othernames like '%$searchtxt%' order by othernames, surname";	
 }

 if($typesearch=='patientid' || $typesearch=='oldcardno'){
   $query_rsodest = "SELECT a.*, DATE_FORMAT(a.dateofbirth, '%d %b %Y') as dob FROM patient_information a where card_number='$searchtxt' or old_card_no='$searchtxt'  order by surname, othernames";	
 }

 if($typesearch=='member_no'){
   $query_rsodest = "SELECT a.*, DATE_FORMAT(a.dateofbirth, '%d %b %Y') as dob FROM patient_information a where insurance_no1  like '$searchtxt%' or  company_staff_no like '$searchtxt%' order by surname, othernames";	
 }	

 if($typesearch=='tel'){
   $query_rsodest = "SELECT a.*, DATE_FORMAT(a.dateofbirth, '%d %b %Y') as dob FROM patient_information a where tel = '$searchtxt' order by surname, othernames";	
 }	

 $pagerows = 100;
 $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) {
} else {
  $start_row = 0;
}
}
?>


<table class="table table-striped table-bordered table-hover dataTable no-footer">
  <thead>
    <tr>
      <th>ID</th>
      <th>SURNAME</th>
      <th>OTHER NAMES</th>
      <th>PATIENT ID</th>
      <th>OLD CARD NO.</th>
      <th>CLIENT</th>
      <th>CO./INSURANCE</th>
      <th>CO./INS. STATUS</th>
      <th>STAFF/INSURANCE NO.</th>
      <th>SEX</th>
      <th style="text-align: center;">ACTION</th>
    </tr>
  </thead>
  <tbody>
    <?php 
    $i=1;
    if($totalRows_rsodest > 0){
      while($row_rsodest = mysql_fetch_assoc($rsodest)){ ?>
        <tr>
         <td height="23" valign="top"><span><?php echo $i?></span></td>
         <td valign="top" style="cursor: pointer;" onclick="getstep('view_patient_visits',<?php echo $row_rsodest['id']; ?>);"><?php echo strtoupper($row_rsodest['surname']); ?></td>
         <td valign="top" onclick="getstep('view_patient_visits',<?php echo $row_rsodest['id']; ?>);" style="cursor: pointer;"><?php echo strtoupper($row_rsodest['othernames']); ?></td>
         <td valign="top" style="cursor: pointer;" onclick="getstep('view_patient_visits',<?php echo $row_rsodest['id']; ?>);"><?php echo strtoupper($row_rsodest['card_number']); ?></td>
         <td valign="top" style="cursor: pointer;" onclick="getstep('view_patient_visits',<?php echo $row_rsodest['id']; ?>);"><?php echo strtoupper($row_rsodest['old_card_no']); ?></td>
         <td valign="top" onclick="getstep('view_patient_visits',<?php echo $row_rsodest['id']; ?>);" style="cursor: pointer;"><?php echo strtoupper($row_rsodest['type_of_client']); ?></td>
         <td valign="top" onclick="getstep('view_patient_visits',<?php echo $row_rsodest['id']; ?>);" style="cursor: pointer;"><?php 
         if($row_rsodest['type_of_client']=='company'){  
          echo getinsurance($row_rsodest['company']);
        }

        if($row_rsodest['type_of_client']=='insurance'){  
          echo getinsurance($row_rsodest['insurance_company1']);
        }

        ?></td>

        <td valign="top" onclick="getstep('view_patient_visits',<?php echo $row_rsodest['id']; ?>);" style="cursor: pointer; <?php if($row_rsodest['company_status']=='Inactive'){ ?> color: red <?php } ?>">
          <?php 
            if($row_rsodest['type_of_client']=='company'){
              echo strtoupper($row_rsodest['company_status']);
            }else{
              echo strtoupper('N/A');
            }
           ?>
            
          </td>

        <td valign="top" onclick="getstep('view_patient_visits',<?php echo $row_rsodest['id']; ?>);" style="cursor: pointer;">
          <?php
          if($row_rsodest['type_of_client']=='company'){  
            echo strtoupper($row_rsodest['company_staff_no']);
          }

          if($row_rsodest['type_of_client']=='insurance'){  
            echo strtoupper($row_rsodest['insurance_no1']);
          }

          ?>

        </td>
        <td valign="top" style="cursor: pointer;" onclick="getstep('view_patient_visits',<?php echo $row_rsodest['id']; ?>);"><?php if($row_rsodest['sex'] == 'M'){echo 'Male';} elseif($row_rsodest['sex']=='F'){echo 'Female';}else{echo $row_rsodest['sex'];} ?></td>
        <td valign="top" style="cursor: pointer;text-align: center;" onclick="getstep('view_patient_visits',<?php echo $row_rsodest['id']; ?>);"><span style="font-weight:bold; text-align:center;"> <a style="color:blue" href="#" onclick="getstep('view_patient_full',<?php echo $row_rsodest['id']; ?>);"><i class="icon-info"></i></a> </span></td>
      </tr>
      <?php $i++;} } else { ?>
       <tr>
         <td colspan="10" style="text-align: center;color:red;"> No Records Found </td>
       </tr> 
     <?php  } ?>
   </table>