<?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']);

$start = strtotime(dateformat($_POST['start_date']));
$end = strtotime(dateformat($_POST['end_date']));
$difference = ($end - $start)/(60*60*24); //gives me days as the difference
if($difference < 1){
	$difference = ($end - $start)/(60*60);
	$difference = round($difference);
	$difference .=" hours";
}
else{
	$difference = round($difference);
	$difference .=" days";
}

  $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, status=%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($difference, "text"),
                       GetSQLValueString(dateformat($_POST['start_date']), "date"),
                       GetSQLValueString(dateformat($_POST['end_date']), "date"),
					    GetSQLValueString($_POST['status'], "text"),
					   GetSQLValueString($_POST['s_no'], "int"));

  mysqli_select_db($conn, $database_conn);
  $Result1 = mysqli_query($conn, $updateSQL) or die(mysqli_error($conn));
  
   $updateSQL2 = sprintf("UPDATE students SET whereabout = %s WHERE admission_no=%s",
                      // GetSQLValueString($_POST['date'], "date"),
					    GetSQLValueString($_POST['status'], "text"),
					   GetSQLValueString($_POST['adm_no'], "int"));

  mysqli_select_db($conn, $database_conn);
  $Result2 = mysqli_query($conn, $updateSQL2) 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 FROM exit_records m 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 Portal </title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php include("links.php");?>


<div class="container">
<div class="row">
<div class="col-sm-6">
<br />
<?php 
	echo '<font color="blue" size="+2"> Editing new record for  Admission nos: '. $row_records['student_adm_no'] . '</font>';

?>


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

<h3><label>Date:</label> <?php echo date('d-m-y g:i:s',time()); ?></h3>
<i> Note that the current time of adding data is used </i>
  <!-- <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> -->
                            
  <h3><label>Class:</label></h3>
  <select name="class" class="form-control">
							  <option selected="selected" value="">Select</option>
							  <option value="JS1" <?php if($row_records['class'] == "JS1") { ?> selected="selected" <?php } ?> >JS1</option>
							  <option value="JS2" <?php if($row_records['class'] == "JS2") { ?> selected="selected" <?php } ?>>JS2</option>
							   <option value="JS3" <?php if($row_records['class'] == "JS3") { ?> selected="selected" <?php } ?>>JS3</option>
                                <option value="SS1" <?php if($row_records['class'] == "SS1") { ?> selected="selected" <?php } ?>>SS1</option>
							  <option value="SS2" <?php if($row_records['class'] == "SS2") { ?> selected="selected" <?php } ?>>SS2</option>
							   <option value="SS3" <?php if($row_records['class'] == "SS3") { ?> selected="selected" <?php } ?>>SS3</option>
							 
							  
  </select>
  <h3>
    <label>Authorizing Officer:</label></h3>
  <input name="authorizing_officer" type="text"  value="<?php echo $row_records['authorizing_officer']; ?>" class="form-control"/>
  
   <h3><label>Reason:</label></h3>
    <textarea name="reason" rows="4" cols="32" class="form-control" ><?php echo $row_records['reason']; ?></textarea> 
    
    <h3><label>Location:</label></h3>
    <textarea name="location" rows="4" cols="32" class="form-control"><?php echo $row_records['location']; ?></textarea>
    
  <h3><label>Update Status:</label></h3>
    <select name="status"class="form-control"/>
		<option selected="selected" value="">Select</option>
		<option value="School" <?php if($row_records['status'] == "School") { ?> selected="selected" <?php } ?> >School</option>
		<option value="Absent" <?php if($row_records['status'] == "Absent") { ?> selected="selected" <?php } ?>>Absent</option>
							  
  </select>
  
     <h3><label>Start Date:</label></h3>
   
   
            <input type='text' class="form-control" id='datetimepicker3' name="start_date"  value="<?php echo dateformatReverse($row_records['start_date']); ?>"/>
        <script type="text/javascript">
            $(function () {
                $('#datetimepicker3').datetimepicker({
					format: 'DD/MM/YYYY HH:mm:ss'
				});
            });
        </script>
        
     <h3><label>End Date:</label></h3>
 
            <input type='text' class="form-control" id='datetimepicker4' name="end_date" value="<?php echo dateformatReverse($row_records['end_date']); ?>"/>
        <script type="text/javascript">
            $(function () {
                $('#datetimepicker4').datetimepicker({
					format: 'DD/MM/YYYY HH:mm:ss'
				});
            });
        </script>
  
    <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']; ?>" /> 
 <br/>
    <input name="submit" type="submit" value="UPDATE RECORD"  class="btn btn-primary"/>
       <a href="record.php?id2=<?php echo $row_records['student_adm_no']; ?>" > <button type="button" class="btn btn-primary">CANCEL </button> </a>
   
    <input type="hidden" name="MM_insert" value="edit">
    <input type="hidden" name="MM_update" value="edit">
</form>
</div></div></div>
<br />
</body>
 
</html>
<?php
mysqli_free_result($records);
?>
