<?php include("session_controller.php"); ?>
<?php
$colname_records = "-1";
if (isset($_GET['id2'])) {
  $colname_records = $_GET['id2'];
}
mysqli_select_db($conn, $database_conn);
//$query_records = sprintf("SELECT m.s_no, m.date, m.student_adm_no, m.class, m.nurse, m.complaint, m.treatment, m.duration, m.bill, m.report, s.surname,s.name,s.middle FROM medical_records m JOIN students s ON (m.student_adm_no = s.admission_no) WHERE student_adm_no = %s ORDER BY `date` DESC", GetSQLValueString($colname_records, "text"));
$query_records = sprintf("SELECT m.s_no, m.date, m.student_adm_no, m.class, m.authorizing_officer, m.reason, m.location, m.duration, m.start_date, m.end_date, m.status FROM exit_records m WHERE student_adm_no = %s ORDER BY `date` DESC", GetSQLValueString($colname_records, "text"));
$records = mysqli_query($conn, $query_records) or die(mysqli_error($conn));
$row_records = mysqli_fetch_assoc($records);
$totalRows_records = mysqli_num_rows($records);

$query_students = NULL;
if(isset($_GET['id2']) && ($_GET['id2'] != "")){
$query_students = "SELECT * FROM students WHERE admission_no =". trim($_GET['id2']);
}

$students = mysqli_query($conn, $query_students) or die(mysqli_error($conn));
$row_students = mysqli_fetch_assoc($students);
$totalRows_students = mysqli_num_rows($students);

?>


<html>
<head>
<title>Student Exit Records</title>

<script>
function check_confirmation(){
var signal = prompt("Type DELETE");
if(signal == "DELETE"){
	return true;		
}
else{
	return false;
}
}

</script>
</head>
<body>

<?php include("links.php");?>
<div class="container">
<div class="row">
<div class="col-sm-12">

<h4> Timestamp:  <?php echo date('D d, M, Y g:i:s',time()); ?> </h4>
<h4> Name: <?php if($query_students != NULL){ echo $row_students['surname'] . " ". $row_students['name']. " ". $row_students['middle'];} ?>,   Admission No: <?php echo $row_students['admission_no']; ?> 
<br/> <br/><font color="red" size="+1"> No of Exits: <?php echo $row_students['no_of_exits']; ?>  </font>
</h4>


<form method="POST" action="add.php" name="add_record"  class="form-group">
<input name="adm_no" type="hidden"  value="<?php echo $row_students['admission_no']; ?>" class="form-control"/>
<input name="name" type="hidden"  value="<?php echo $row_students['surname']; ?> <?php echo $row_students['name']; ?> <?php echo $row_students['middle']; ?>" class="form-control"/>
<input name="submit" type="submit" value="ADD NEW RECORD" class="btn btn-primary" />
</form>




<br />
<div class="table-responsive">
<table width="100%" class="table table-striped table-bordered"> 
    <thead>
    <tr>
      <th width="31">S/N </th><th width="120">Date </th><th width="94">Current Class </th><th width="106">Authorizing Officer </th><th width="125">Reason </th><th width="128">Location </th><th width="50">Duration (days/hours) </th><th width="84">Start date </th><th width="180">End date</th><th> Status </th><?php if(isset($row_records['date']) && ($_SESSION['MM_UserGroup'] == "admin")){ ?>
      <th width="79">Actions 1 </th> <th width="79">Actions 2 </th>
      <?php } ?>
    </tr>
   </thead>
   <tbody> 
 
      <?php 
	  $counter = 0;
	  $d=1;
	  do { ?>
       <tr>
 <td height="40"><?php echo ++$counter; ?></td><td> <?php if(isset($row_records['date'])){echo date('d-m-y g:ia',strtotime($row_records['date'])); } ?></td><td> <?php echo $row_records['class']; ?></td><td><?php echo $row_records['authorizing_officer']; ?></td>
        <td><?php echo $row_records['reason']; ?> </td><td><?php echo $row_records['location']; ?> </td><td><?php echo $row_records['duration']; ?> </td><td><?php echo $row_records['start_date']; ?> </td><td><?php echo $row_records['end_date']; ?> </td><td><?php echo $row_records['status']; 
	/*
		if(strtotime($row_records['end_date']) < time()){
			echo "<a href='print_record.php?id=".$row_records['s_no']."title='Click to Print Gate Pass'>EXPIRED</a>";
		}
		else{
			echo "<a href='print_record.php?id=".$row_records['s_no']."title='Click to Print Gate Pass'>VALID</a>";
		}
	*/	
		?> </td>
		<?php if(isset($row_records['date']) && ($_SESSION['MM_UserGroup'] == "admin")){ ?><td><a href="edit_record2.php?id=<?php echo $row_records['s_no']; ?>">EDIT</a></td>
		<td><a href="delete.php?s_no=<?php echo $row_records['s_no']; ?>" onClick="return check_confirmation();">DELETE </a> </td>
       <?php } ?>
  </tr>
  <?php } while ($row_records = mysqli_fetch_assoc($records)); ?>
 </tbody>
 </table>
</div>
</div></div></div>

<br />
 </body>
 
 </html>


<?php
mysqli_free_result($records);
mysqli_free_result($students);
  
?>