<?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);
if(!isset($id)){
list($id, $doc_id) = explode("@@@", $_POST['id']);
}

$query_rsodest_patient = "SELECT *, DATE_FORMAT(dateofbirth, '%D %b %Y') as datenew, (DATE_FORMAT(current_date, '%Y') - DATE_FORMAT(dateofbirth, '%Y') - (DATE_FORMAT(current_date, '00-%m-%d') < DATE_FORMAT(dateofbirth, '00-%m-%d'))) AS age FROM guest_patients where id='$id'";

$rsodest_patient = mysql_query($query_rsodest_patient, $courier) or die(mysql_error());
$row_rsodest_patient = mysql_fetch_assoc($rsodest_patient);

$MEMBERSHIPID=$row_rsodest_patient['id'];
$patient_no=$row_rsodest_patient['card_number'];
$PROVIDERID=$_SESSION['provider_id'];
$type_of_client=$row_rsodest_patient['type_of_client'];
$_SESSION['client_type']=$type_of_client;

$query_rsodest_officer = "SELECT u.* FROM users u,duty_roster d where u.id = d.staff_no AND d.duty_date = '".date('Y-m-d')."' AND d.status ='In Progress' AND (security='MEDICAL OFFICER' or security='OPTICIAN' or security='DENTIST')  order by title, surname, firstnames";	     
$rsodest_officer = mysql_query($query_rsodest_officer, $courier) or die(mysql_error());
$row_rsodest_officer=mysql_fetch_assoc($rsodest_officer);
$totalRows_rsodest_officer = mysql_num_rows($rsodest_officer);

$query_rsodest_claim = "select *, DATE_FORMAT(datetimeadded, '%e %b %Y') as dateprocessing from process_claim_no where patient_no='$patient_no' and provider_id='$PROVIDERID' and (patient_status='In Use' or typeofvisit='In-Patient' or consulting_room_status='admitted')";
$rsodest_claim = mysql_query($query_rsodest_claim, $courier) or die(mysql_error());
$row_rsodest_claim = mysql_fetch_assoc($rsodest_claim);
$totalRows_rsodest_claim = mysql_num_rows($rsodest_claim);
$process_claim_no = $row_rsodest_claim['process_claim_no'];
	
	if($totalRows_rsodest_claim>0){$showvisit='yes';}
?>

<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>Patient Profile</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 onclick="getstep('view_normal_patient_visits','<?php echo $id;?>');">
                                        <a href="#">
                                            <i class="icon-refresh"></i> Refresh</a>
                                    </li>
                                    <li onclick="getstep('add_new_normal_patient');">
                                        <a href="#">
                                            <i class="icon-plus"></i> Add New Normal Patient </a>
                                    </li>
                                </ul>
                            </div>
                        </div>
</div>

<!--<h1 class="page-title"> Patient Profile <small></small></h1>-->
                    <!-- END PAGE HEADER-->
                    <div class="row">
                        <div class="col-md-12">
                            <!-- BEGIN PROFILE SIDEBAR -->
                            <div class="profile-sidebar">
                                <!-- PORTLET MAIN -->
                                <div class="portlet light profile-sidebar-portlet ">
                                    <!-- SIDEBAR USERPIC -->
                                    <div class="profile-userpic">
                                          <?php $picpath=$row_rsodest_patient['picpath']; 
										  
										  if(!$picpath){ 
											
											
											if($row_rsodest_patient['insurance_no1']){
											$inspicpath=getpicpath($row_rsodest_patient['insurance_company1']);
											
											if($inspicpath){
											
											$member_id=preg_replace("/[^0-9]/","",$row_rsodest_patient['insurance_no1']);
											
											$picpath=$inspicpath."/".ltrim($member_id,0).".jpg";
											}else{
											$picpath="images/profile.gif";		
											}
											}else{
											$picpath="images/profile.gif";	
											}
										  }
										  
										  ?>
                                        
                                        <img src="<?php echo $picpath;?>" class="img-responsive" alt=""> 
                                    </div>
                                    <div class="profile-usertitle">
                                        <div class="profile-usertitle-name"> <?php echo $row_rsodest_patient['surname']." ".$row_rsodest_patient['othernames'];?> </div>
                                        <div class="profile-usertitle-job"> <?php echo $row_rsodest_patient['card_number'];?> </div>
                                    </div>
<!--                                    <div class="profile-userbuttons">
                                        <button type="button" class="btn btn-circle red btn-sm">EDIT INFO</button>
                                    </div>-->
                                    <div class="profile-usermenu">
                                        <ul class="nav">
                                            <li class="patient-sub-menu active" id="patient_visit" onclick="getstep('view_normal_patient_visits','<?php echo $id; ?>',this.id);">
                                                <a> <i class="icon-home"></i> Patient Visits </a>
                                            </li>
                                            <li class="patient-sub-menu " id="patient_info" onclick="getProcess('view_normal_patient_information', '#patient_page_wrapper', '<?php echo $id; ?>',this.id);">
                                                <a> <i class="icon-info"></i> Patient Information </a>
                                            </li>
                                            <li class="patient-sub-menu " id="patient_edit" onclick="getProcess('edit_normal_patient_information', '#patient_page_wrapper', '<?php echo $id; ?>',this.id);">
                                                <a> <i class="icon-pencil"></i> Edit Patient Info </a>
                                            </li>
                                            <?php if(!empty($process_claim_no)){ ?>
                                            <li class="patient-sub-menu " id="patient_administraitve_charges" onclick="getProcess('add_administrative_charges', '#patient_page_wrapper', '<?php echo $id.'@@@'.$process_claim_no; ?>',this.id);">
                                                <a> <i class="fa fa-dollar"></i> Client Bills </a>
                                            </li>
                                            <?php } ?>
                                        </ul>
                                    </div>
                                    <!-- END MENU -->
                                </div>
                            </div>
                            <div class="profile-content">
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="portlet light " id="patient_page_wrapper" style="min-height:800px;">
                                            <div class="portlet-title tabbable-line">
                                                <div class="caption caption-md">
                                                    <i class="icon-globe theme-font hide"></i>
                                                    <span class="caption-subject font-blue-madison bold uppercase">PATIENT VISIT INFORMATION</span>
                                                </div>
                                                <ul class="nav nav-tabs">
                                                    <li class="active">
                                                        <a href="#tab_1_1" data-toggle="tab"> List of Patient Visits </a>
                                                    </li>
                                                    <li>
                                                        <a href="#tab_1_2" data-toggle="tab">  New Patient Visit </a>
                                                    </li>
                                                </ul>
                                            </div>
                                            <div class="portlet-body form">
                                                <div class="tab-content">
                                                    <div class="tab-pane active" id="tab_1_1">
                                                      <?php
                                                        $patient_no = $row_rsodest_patient['card_number'];
                                                        $query_rsodest_claims_invoice2 = mysql_fetch_assoc(mysql_query("select *, DATE_FORMAT(datetimeadded, '%e %b %Y') as datenew from process_claim_no where patient_no='$patient_no' order by id desc limit 1", $courier));
                                                        $query_rsodest_claims_invoice = "select *, DATE_FORMAT(datetimeadded, '%e %b %Y') as datenew from process_claim_no where patient_no='$patient_no' order by id desc";
                                                        $rsodest_claims_invoice = mysql_query($query_rsodest_claims_invoice, $courier) or die(mysql_error());
                                                        $row_rsodest_claims_invoice = mysql_fetch_assoc($rsodest_claims_invoice);
                                                        $totalRows_claims_invoice = mysql_num_rows($rsodest_claims_invoice); ?>
                                                       <table class="table table-striped table-bordered table-hover dataTable no-footer">
										  <thead>
										    <tr>
										      <th class="count">&nbsp;</th>
										      <th>Date of Visit</th>
										      <th>Visit No</th>
										      <th>Attending Officer</th>
										      <th>Area</th>
										      <th>Status</th>
									        </tr>
									      </thead>
										  <tbody>  <?php if ($totalRows_claims_invoice> 0) { ?>
										    <?php
                                                    do {
                                                        $start_row=$start_row+1;
                                                        ?>
										    <tr>
										      <td height="23" valign="top" class="count"><span><?php echo $start_row; ?></span></td>
										      <td valign="top" style="cursor: pointer;" onclick="getclaimdetails('view_patient_visit_list','<?php echo $row_rsodest_claims_invoice['process_claim_no']; ?>','claimsno');"><?php if($checkstatus==''){$checkstatus=$row_rsodest_claims_invoice['patient_status'];}echo $row_rsodest_claims_invoice['datenew']; ?></td>
										      <td valign="top" onclick="getclaimdetails('view_patient_visit_list','<?php echo $row_rsodest_claims_invoice['process_claim_no']; ?>','claimsno');" style="cursor: pointer;"><?php echo $row_rsodest_claims_invoice['process_claim_no']; ?></td>
										      <td valign="top" onclick="getclaimdetails('view_patient_visit_list','<?php echo $row_rsodest_claims_invoice['process_claim_no']; ?>','claimsno');" style="cursor: pointer;"><?php echo $row_rsodest_claims_invoice['attending_officer']; ?></td>
										      <td valign="top" onclick="getclaimdetails('view_patient_visit_list','<?php echo $row_rsodest_claims_invoice['process_claim_no']; ?>','claimsno');" style="cursor: pointer;"><?php echo $row_rsodest_claims_invoice['area_specialisation'];?></td>
										      <td valign="top" style="cursor: pointer;" onclick="getclaimdetails('view_patient_visit_list','<?php echo $row_rsodest_claims_invoice['process_claim_no']; ?>','claimsno');">
                                              
                                              <?php if($row_rsodest_claims_invoice['patient_status']=='In Use'){$statuscolor='green';}
				  if($row_rsodest_claims_invoice['patient_status']=='Done'){$statuscolor='red';}
					  
					  echo "<span style='color:$statuscolor; font-weight:bold;'>".$row_rsodest_claims_invoice['patient_status']."</span>";?>
                                              
                                              </td>
									        </tr>
										    
										    <?php } while ($row_rsodest_claims_invoice = mysql_fetch_assoc($rsodest_claims_invoice)); ?>
											
											<?php }else{?>
                                            <tr>
										      <td height="23" colspan="6" valign="top" class="count">Sorry, No Patient Visits Recorded Yet.</td>
									        </tr>
                                            
                                            <?php }?>
									      </tbody>
								    </table> 
                                                        </div>
                                                    <div class="tab-pane" id="tab_1_2">
                                                        <form name="form" class="form-horizontal" role="form" id="patient_generated_visit_no">
                                                       <?php $checkstatus = $query_rsodest_claims_invoice2['patient_status'];
                                                       if($checkstatus!='In Use'){?>      
                                                            <div class="form-body">
                                                                <div class="form-group">
                                                                    <label class="col-md-4 control-label">Attending Officer</label>
                                                                    <div class="col-md-8">
                                                                        <input type="hidden" name="patient_no" id="patient_no" value="<?php echo $row_rsodest_patient['card_number']?>">
                                                                        <select class="form-control" name="attending_officer" id="attending_officer">
                                                                            <option value="" selected>Please Select Attending Officer to Assign</option>
                                                                            <?php if ($totalRows_rsodest_officer > 0) {
                                                                                do {
                                                                                    $fullname = $row_rsodest_officer['title'] . " " . $row_rsodest_officer['firstnames'] . " " . $row_rsodest_officer['surname'] . " (" . $row_rsodest_officer['area_specialisation'] . ") - " . getnoofpatients($row_rsodest_officer['id']) . " Patients"; ?>
                                                                                    <option <?php if ($doc_id == $row_rsodest_officer['id']) {
                                                                                echo 'selected=""';
                                                                            } ?> value="<?php echo $row_rsodest_officer['id']; ?>"> <?php echo $fullname; ?></option>
                                                                             <?php } while ($row_rsodest_officer = mysql_fetch_assoc($rsodest_officer)); } ?>
                                                                        </select>
                                                                    </div>
                                                                </div>
                                                                <div class="form-group">
                                                                    <label class="col-md-4 control-label">Service Category</label>
                                                                    <div class="col-md-8">
                                                                        <select class="form-control" name="service_category" id="service_category">
                                                                            <option value="" selected>Please Select Service Category</option>
                                                                            <option value="Part Service">Part Service</option>
                                                                            <option value="Frame Repair">Frame Repair</option>
                                                                        </select>
                                                                    </div>
                                                                </div>
                                                                <div class="form-group">
                                                                    <label class="col-md-4 control-label">Mode of Payment</label>
                                                                    <div class="col-md-8">
                                                                        <select class="form-control" name="mode_of_payment" id="mode_of_payment" onchange="showtext(this.value);if(this.value=='insurance'){$('#generate_visit_no').hide()}else{$('#generate_visit_no').show();}">
        <option value="cash" <?php if($row_rsodest_patient['type_of_client']=='cash' || !$row_rsodest_patient['type_of_client']){ echo "selected";}?>>CASH</option>
        </select>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                            <div class="form-actions right">
                                                                <button type="reset" class="btn default">Reset</button>
                                                                <button type="submit" class="btn green" onclick="getProcess('patientvisitno', '#patient_generated_visit_no', '<?php echo $id; ?>');return false;">Request New Visit No.</button>
                                                            </div>
                                                       <?php }else{
                                                           include("process_visit_no.php");
                                                       } ?> 
                                                            <input type="hidden" id="ins_company" name="ins_company" value="<?php echo $row_rsodest['ins_company']; ?>">
                                                            <input type="hidden" id="member_no" name="member_no" value="<?php echo $row_rsodest['member_no']; ?>">
                                                            <input type="hidden" id="user_id" name="user_id" value="<?php echo $_SESSION['myMM_Userid']; ?>">
                                                            <input type="hidden" id="process_claim_no" name="process_claim_no" value="<?php echo $process_claim_no; ?>"> 
                                                        </form>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>