<?php 
session_start();
include("connectdb.php"); 

$user_id = $_SESSION['userid'];

$query =  mysqli_query($con, "SELECT * FROM testreport WHERE loyalty_id = 'null' ");

if(isset($_GET['run'])){
	$loyalty_id = mysqli_real_escape_string($con, $_GET['loyalty_id']);
	
	$query =  mysqli_query($con, "SELECT * FROM testreport WHERE loyalty_id = '$loyalty_id' ");
}

function testName($test_id){
	global $con;
	$query = mysqli_query($con, "SELECT * FROM labtest WHERE test_id = $test_id ");
	$row = mysqli_fetch_array($query);
	$test_name =  $row['test_name'];
	
	return $test_name;
}

function customerName($loyalty_id){
	global $con;
	$query = mysqli_query($con, "SELECT * FROM loyalty WHERE loyalty_id = '$loyalty_id' ");
	$row = mysqli_fetch_array($query);
	$full_name =  $row['first_name'] . ' ' . $row['last_name'];
	
	return $full_name;
}

?>
<?php include("side_nav.php"); ?>
<?php include("top_nav.php"); ?>


        <!-- page content -->
        <div class="right_col" role="main">
          <div class="">
            <div class="page-title">
              <div class="title_left">
                <h3></h3>
              </div>

            </div>
            <div class="clearfix"></div>

            <div class="row">

              <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
                    <h2><strong>Select a patient </strong></h2><h2 style="margin-left:200px"></h2>
                    <ul class="nav navbar-right panel_toolbox">
                      <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                      </li>
                      <li><a class="close-link"><i class="fa fa-close"></i></a>
                      </li>
                    </ul>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
                    
                    <form method="GET" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
					
					<div class="form-group">
					    <label class="control-label col-md-3 col-sm-3 col-xs-12">Patient Name <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <select name="loyalty_id" required="required" class="form-control col-md-7 col-xs-12 select2">
						    <option value=""> </option>
							<?php $sql = mysqli_query($con, "SELECT * FROM loyalty  ");  
						       while($row = mysqli_fetch_array($sql)):
						    ?>
						    <option value="<?php echo $row['loyalty_id']?> "> <?php echo $row['first_name'] .' '.$row['last_name'].' '.$row['phone']  ?></option>
						    <?php endwhile;  ?>
                          </select>
                        </div>
                      </div>
					  
					   <div class="form-group">
                        <div class=" col-md-offset-11">
                          <button type="submit" name="run" id="run" class="btn btn-primary">! RUN</button>
                      </div>
					 </div>
					  
					</form>
                  
                </div>
              </div>
			  
			  <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>Test History <small></small></h2>
                    <ul class="nav navbar-right panel_toolbox">
                      <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                      </li>
                      <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
                        <ul class="dropdown-menu" role="menu">
                          <li><a href="#">Settings 1</a>
                          </li>
                          <li><a href="#">Settings 2</a>
                          </li>
                        </ul>
                      </li>
                      <li><a class="close-link"><i class="fa fa-close"></i></a>
                      </li>
                    </ul>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
                    <p class="text-muted font-13 m-b-30">
                      The table shows the test history of the selected patient.
                    </p>
					
					<!-- <table id="datatable-buttons" class="table table-striped table-bordered"> -->
                    <table id="datatable" class="table table-striped table-bordered">
                      <thead>
                        <tr>
							<th class="col-md-3 col-sm-3 col-xs-3"><b>Customer name</b></th>
							<th><b>Test </b></th>
							<th><b>Test Date</b></th>
							<th><b>Report Date</b></th>
							<th><b>View report</b></th>
						</tr>
                      </thead>
                     
                      <tbody id="tableData">
					    <?php 
						    while($row = mysqli_fetch_array($query)):
								$test_name = testName($row['test_id']) . '*';
								$test_report_id = $row['testreport_id'];
								$transaction_id = $row['transaction_id'];
														
						?>
						<tr>
							<td><?php echo customerName($row['loyalty_id']); ?></td>
							<td><?php echo testName($row['test_id']) ; ?></td>
							<td><?php echo $row['test_date']; ?></td>
							<td><?php echo $row['date_created']; ?></td>
							<td>
							  <div class='btn-group'>
								 <a href='view-test-report?testrid=<?php echo $test_report_id;  ?>' class='btn btn-primary'><span class='glyphicon glyphicon-pencil'></span></a> 
							  </div>
							</td>
						</tr>
						<?php endwhile; ?>
                      </tbody>
					  
                    </table>
				 </div>
				</div>
				
              </div>
            </div>
          </div>
        </div>
        <!-- /page content -->
 <!-- /footer content -->
      </div>
    </div>
<?php include("footer.php"); ?> 
	</body>
</html>

<!---- Personal Dev -->
<script src="myscripts.js"></script>
<script>
	$('.select2').select2();
</script>
