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

<?php
//format date function
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_insert"])) && ($_POST["MM_insert"] == "add")) {	
//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";
}
  $insertSQL = sprintf("INSERT INTO exit_records (student_adm_no, `class`, authorizing_officer, reason, location, duration, start_date, end_date, status) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %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('Absent', "text"));

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

  mysqli_select_db($conn, $database_conn);
  $Result2 = mysqli_query($conn, $updateSQL2) or die(mysqli_error($conn));

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



?>
<html>
<head>
<title>Schedule Exit Notice</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'> 
<?php if(isset($_POST['name']) && isset($_POST['adm_no'])){
	echo '<h3> Entering Exit record for ' .$_POST['name'].', Admission nos: '. $_POST['adm_no'] . '</h3>';
}
?>

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

<h4><label>Date:</label> <?php echo date('d-m-y g:i:s',time()); ?></h4>
<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">JS1</option>
							  <option value="JS2">JS2</option>
							   <option value="JS3">JS3</option>
                                <option value="SS1">SS1</option>
							  <option value="SS2">SS2</option>
							   <option value="SS3">SS3</option>
							 
							  
  </select>
  <h3><label>Authorizing Officer:</label></h3>
  <input name="authorizing_officer" type="text"  value="" class="form-control"/>
  
   <h3><label>Reason:</label></h3>
    <input name="reason"  type="text" class="form-control" /> 
    
    <h3><label>Location:</label></h3>
     <input name="location"  type="text" class="form-control" /> 
     
  <!--  <h3><label>Duration:</label></h3>
    <input name="duration" type="text"  value="" class="form-control"/>
    -->
     <h3><label>Start Date: &nbsp;&nbsp; (DD/MM/YYYY HH:mm:ss) </label></h3>
    <input type='text' class="form-control" id='datetimepicker1' name="start_date" />
        <script type="text/javascript">
            $(function () {
                $('#datetimepicker1').datetimepicker({
					format: 'DD/MM/YYYY HH:mm:ss'
				});
            });
        </script>
   
   <!--
                 <div class='input-group date' id='datetimepicker1'>
                    <input type='text' class="form-control" name="start_date"/>
                    <span class="input-group-addon">
                        <span class="glyphicon glyphicon-calendar"></span>
                    </span>
                </div>
        <script type="text/javascript">
            $(function () {
                $('#datetimepicker1').datetimepicker({
					format: 'DD/MM/YYYY HH:mm:ss'
				});
            });
        </script>
   -->
    <!-- pick only time eg 3:45pm
    <div class="container">
    <div class="row">
        <div class='col-sm-6'>
            <div class="form-group">
                <div class='input-group date' id='datetimepicker3'>
                    <input type='text' class="form-control" />
                    <span class="input-group-addon">
                        <span class="glyphicon glyphicon-time"></span>
                    </span>
                </div>
            </div>
        </div>
        <script type="text/javascript">
            $(function () {
                $('#datetimepicker3').datetimepicker({
                    format: 'LT'
                });
            });
        </script>
    </div>
</div>
-->

<!--click on the input box to pick date, no icon used

<div class="container">
    <div class="row">
        <div class='col-sm-6'>
            <input type='text' class="form-control" id='datetimepicker4' />
        </div>
        <script type="text/javascript">
            $(function () {
                $('#datetimepicker4').datetimepicker();
            });
        </script>
    </div>
</div>
-->

     <h3><label>End Date: &nbsp;&nbsp; (DD/MM/YYYY HH:mm:ss)</label></h3>
  <!--  
                <div class='input-group date' id='datetimepicker2'>
                    <input type='text' class="form-control"  name="end_date"/>
                    <span class="input-group-addon">
                        <span class="glyphicon glyphicon-calendar"></span>
                    </span>
            </div>
        <script type="text/javascript">
            $(function () {
               // $('#datetimepicker2').datetimepicker(); uses default eg: 12/07/2017 3:34 PM
			    $('#datetimepicker2').datetimepicker({
					format: 'DD/MM/YYYY HH:mm:ss'
				});
            });
        </script>
   --> 
    
            <input type='text' class="form-control" id='datetimepicker2' name="end_date" />
        <script type="text/javascript">
            $(function () {
                $('#datetimepicker2').datetimepicker({
					format: 'DD/MM/YYYY HH:mm:ss'
				});
            });
        </script>
    
    

    <input name="adm_no" type="hidden"  value="<?php if(isset($_POST['adm_no'])){ echo $_POST['adm_no']; }?>" size="48"/>
    <input name="name" type="hidden"  value="<?php if(isset($_POST['name'])){echo $_POST['name'];} ?>" size="48"/>
   <!-- <input name="date" type="hidden"  value="<?php //echo date('y-m-d G:i:s',time()); ?>" size="48" /> -->
 <br/>
    <input name="submit" type="submit" value="ADD RECORD"  class="btn btn-primary"/>
    <a href="record.php?id2=<?php if(isset($_POST['adm_no'])){ echo $_POST['adm_no']; }?>" > <button type="button"  class="btn btn-primary">CANCEL </button> </a>
   
    
    <input type="hidden" name="MM_insert" value="add">
</form>
</td></tr>
</div></div></div>

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