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

<?php
$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 medical_records SET `date`=%s, student_adm_no=%s, `class`=%s, nurse=%s, complaint=%s, treatment=%s, duration=%s, bill=%s, report=%s WHERE student_adm_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"));

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

  $updateGoTo = "record.php?id=".$_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 s_no, `date`, student_adm_no, `class`, nurse, complaint, treatment, duration, bill, report FROM medical_records WHERE s_no = %s", 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> Medical Portal </title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php include("links.php");?>

<table width="80%" align="center">
<tr>
<td> 
<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">

<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">
							  <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>Medical Officer on Duty:</label></h3>
  <input name="nurse" type="text"  value="<?php echo $row_records['nurse']; ?>" size="48"/>
  
   <h3><label>Complaint:</label></h3>
    <textarea name="complaint" rows="4" cols="32" ><?php echo $row_records['complaint']; ?></textarea> 
    
    <h3><label>Treatment:</label></h3>
    <textarea name="treatment" rows="4" cols="32" ><?php echo $row_records['treatment']; ?></textarea>
    
    <h3><label>Duration:</label></h3>
    <input name="duration" type="text"  value="<?php echo $row_records['duration']; ?>" size="48"/>
    
     <h3><label>Bill:</label></h3>
    <input name="bill" type="text"  value="<?php echo $row_records['bill']; ?>" size="48"/>
     <h3><label>Report:</label></h3>
    <textarea name="report" rows="4" cols="32" ><?php echo $row_records['report']; ?></textarea>
    <input name="adm_no" type="hidden"  value="<?php echo $row_records['student_adm_no']; ?>" size="48"/>
    <input name="name" type="hidden"  value="<?php echo $row_records['name']; ?>" size="48"/>
    <input name="date" type="hidden"  value="<?php echo $row_records['date']; ?>" />
    <input name="s_no" type="hidden"  value="<?php echo $row_records['s_no']; ?>" size="48" /> 
 <br/>
    <input name="submit" type="submit" value="UPDATE RECORD" />
       <a href="record.php?id=<?php echo $row_records['student_adm_no']; ?>" > <button type="button">CANCEL </button> </a>
   
    <input type="hidden" name="MM_insert" value="edit">
    <input type="hidden" name="MM_update" value="edit">
</form>
</td></tr>
</table>


<br />
</body>
 
</html>
<?php
mysqli_free_result($records);
?>
