<?php include("session_controller.php"); ?>

<?php

//format date function
function dateformatReverse($datetime){
$received_timestamp = "";
 if($datetime != ""){
 $full_date = explode(" ", $datetime);
 $date = explode("-", $full_date[0]);
 $d = $date[2];
 $mo = $date[1];
 $y = $date[0];
 $time = explode(":",$full_date[1]);
 $h = $time[0];
 $min = $time[1];
 $s = $time[2];
 
 $received_timestamp = $d."/".$mo."/".$y." ".$h.":".$min.":".$s;
 }
 return $received_timestamp;
 }
 
function dateformat($datetime){
$received_timestamp = "";
 if($datetime != ""){
 $full_date = explode(" ", $datetime);
 $date = explode("/", $full_date[0]);
 $d = $date[0];
 $mo = $date[1];
 $y = $date[2];
 $time = explode(":",$full_date[1]);
 $h = $time[0];
 $min = $time[1];
 $s = $time[2];
 
 $received_timestamp = $y."-".$mo."-".$d." ".$h.":".$min.":".$s;
 }
 return $received_timestamp;
 }

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "edit")) {
 /*
  $updateSQL = sprintf("UPDATE exit_records SET `date`=%s, student_adm_no=%s, `class`=%s, nurse=%s, complaint=%s, treatment=%s, duration=%s, bill=%s, report=%s WHERE s_no=%s",
                       GetSQLValueString($_POST['date'], "date"),
                       GetSQLValueString($_POST['adm_no'], "text"),
                       GetSQLValueString($_POST['class'], "text"),
                       GetSQLValueString($_POST['nurse'], "text"),
                       GetSQLValueString($_POST['complaint'], "text"),
                       GetSQLValueString($_POST['treatment'], "text"),
                       GetSQLValueString($_POST['duration'], "text"),
                       GetSQLValueString($_POST['bill'], "double"),
                       GetSQLValueString($_POST['report'], "text"),
                       GetSQLValueString($_POST['s_no'], "int"));
	*/
					   
//echo dateformat($_POST['start_date']);
//echo dateformat($_POST['end_date']);
  $updateSQL = sprintf("UPDATE exit_records SET student_adm_no = %s, `class`=%s, authorizing_officer=%s, reason=%s, location=%s, duration=%s, start_date=%s, end_date=%s WHERE s_no=%s",
                      // GetSQLValueString($_POST['date'], "date"),
                       GetSQLValueString($_POST['adm_no'], "text"),
                       GetSQLValueString($_POST['class'], "text"),
                       GetSQLValueString($_POST['authorizing_officer'], "text"),
                       GetSQLValueString($_POST['reason'], "text"),
                       GetSQLValueString($_POST['location'], "text"),
                       GetSQLValueString($_POST['duration'], "text"),
                       GetSQLValueString(dateformat($_POST['start_date']), "date"),
                       GetSQLValueString(dateformat($_POST['end_date']), "date"),
					   GetSQLValueString($_POST['s_no'], "int"));

  mysqli_select_db($conn, $database_conn);
  $Result1 = mysqli_query($conn, $updateSQL) or die(mysqli_error($conn));

  $updateGoTo = "record.php?id2=".$_POST['adm_no'];
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

$colname_records = "-1";
if (isset($_GET['id'])) {
  $colname_records = $_GET['id'];
}

mysqli_select_db($conn, $database_conn);
$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,s.surname, s.name, s.middle FROM exit_records m JOIN students s ON (m.student_adm_no =s.admission_no) WHERE m.s_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);
?>
<html>
<head>
<title> Gate-Pass </title>
<script type="text/javascript" src="JQuery-Library/jquery-1.11.2.min.js" ></script>
  <link href="bootstrap-3.3.5-dist/css/bootstrap.min.css" rel="stylesheet" type="text/css" media="screen" />
     <script type="text/javascript" src="bootstrap-3.3.5-dist/js/bootstrap.min.js"></script>
     <script type="text/javascript" src="bootstrap-3.3.5-dist/js/moment.min.js"></script>
      <script type="text/javascript" src="bootstrap-3.3.5-dist/js/bootstrap-datetimepicker.min.js"></script>
  <link rel="stylesheet" href="bootstrap-3.3.5-dist/css/bootstrap-datetimepicker.css" type="text/css" media="screen"/>
    <link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<?php //include("links.php");?>

<div class="container">
<div class="row">
<div class="col-sm-6">
<br />
<?php 
	echo '<h4> Premiere Academy Gate Pass </h4>' ;
	echo $row_records['surname'] . ' '.$row_records['name']. ' '.$row_records['middle'] . ': Admission No: '. $row_records['student_adm_no'] . '</h4>';

?>


<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="edit" class="form-group">

<h3><label>Timestamp:</label> <?php echo date('d-m-y g:i:s',time()); ?></h3>

  <!-- <table width="259"  >
						 <tr>
						 <td width="73"> Day</td>
					
						 <td width="101"> Month</td> 
						 <td width="69"> Year</td>
						 
						 </tr>
						  <tr>
						  <td width="73"> <select name="day" >
							  <option selected="selected" value="">Select</option>
							  <option value="1" >1</option>
							  <option value="2">2</option>
							   <option value="3">3</option>
							  <option value="4">4</option>
							   <option value="5">5</option>
							  <option value="6">6</option>
							   <option value="7">7</option>
							  <option value="8">8</option>
							   <option value="9">9</option>
							  <option value="10">10</option>
							  <option value="11">11</option>
							   <option value="12">12</option>
							    <option value="13" >13</option>
							  <option value="14">14</option>
							   <option value="15">15</option>
							  <option value="16">16</option>
							   <option value="17">17</option>
							  <option value="18">18</option>
							   <option value="19">19</option>
							  <option value="20">20</option>
							   <option value="21">21</option>
							  <option value="22">22</option>
							  <option value="23">23</option>
							   <option value="24">24</option>	
							    <option value="25">25</option>
							  <option value="26">26</option>
							   <option value="27">27</option>	
							    <option value="28">28</option>
							  <option value="29">29</option>
							   <option value="30">30</option>	
							    <option value="31">31</option>			 
					  </select></td>
  						    <td width="101"> <select name="month" >
							  <option selected="selected" value="">Select</option>
							  <option value="1" >January</option>
							  <option value="2">February</option>
							   <option value="3">March</option>
							  <option value="4">April</option>
							   <option value="5">May</option>
							  <option value="6">June</option>
							   <option value="7">July</option>
							  <option value="8">August</option>
							   <option value="9">September</option>
							  <option value="10">October</option>
							  <option value="11">November</option>
							   <option value="12">December</option>			 
					  </select></td> 

					   <td width="69"> <select name="year" >
							  <option selected="selected" value="">Select</option>
							  <option value="2016">2016</option>
							  <option value="2017">2017</option>
							   <option value="2016">2018</option>
							 
							  
						    </select></td></tr></table> -->
 <table width="100%" class="table table-striped table-bordered"> 
 <tr>                           
 <td> <h3><label>Class:</label></h3></td>
 <td> <?php echo $row_records['class']; ?></td>
 </tr>
 <tr>
 <td> <h3>
    <label>Authorizing Officer:</label></h3> </td>
    <td>
  <?php echo $row_records['authorizing_officer']; ?>
  </td>
  </tr>
  <tr>
  <td>
   <h3><label>Reason:</label></h3>
   </td>
   <td> <?php echo $row_records['reason']; ?>
    </td>
    </tr>
    <tr>
    <td><h3><label>Location:</label></h3>
   </td>
   <td><?php echo $row_records['location']; ?>
    </td>
    </tr>
    <tr>
    <td><h3><label>Duration:</label></h3>
   </td>
   <td><?php echo $row_records['duration']; ?>
    </td>
    </tr>
    <tr>
     <td><h3><label>Start Date:</label></h3>
   </td>
   <td>
          <?php echo dateformatReverse($row_records['start_date']); ?>
       </td>
       </tr>
       <tr>
       <td> 
     <h3><label>End Date:</label></h3>
 </td>
      <td>     <?php echo dateformatReverse($row_records['end_date']); ?>
    </td></tr>
 <tr>
     <td><button type="button" class="btn btn-primary" onClick="javascript:print();">PRINT GATEPASS</button>
      </td>
      <td> <a href="record.php?id2=<?php echo $row_records['student_adm_no']; ?>" > <button type="button" class="btn btn-primary">CANCEL </button> </a>
   </td>
   </tr></table>
    <input type="hidden" name="MM_insert" value="edit">
    <input type="hidden" name="MM_update" value="edit">
     <input name="adm_no" type="hidden"  value="<?php echo $row_records['student_adm_no']; ?>" />
    <input name="name" type="hidden"  value="<?php echo $row_records['name']; ?>"/>
    <input name="date" type="hidden"  value="<?php echo $row_records['date']; ?>" />
    <input name="s_no" type="hidden"  value="<?php echo $row_records['s_no']; ?>" /> 
</form>
</div></div></div>
<br />
</body>
 
</html>
<?php
mysqli_free_result($records);
?>
