<?php
	include("config.php");
	session_start();

    // fetch usertype from database
	$sql_usertype = mysqli_query($db, "SELECT * FROM usertype");

    // display all the data from database to input boxes
	if (isset($_GET["ID"])){
        $ID = $_GET["ID"];
        $record = mysqli_query($db, "SELECT * FROM members WHERE ID='$ID'");

        if (count(array($record)) == 1 ) {
            $data = mysqli_fetch_array($record);
                $lname = $data['Last_Name'];
                $fname = $data['First_Name'];
                $m_initial = $data['Middle_Initial'];
                $email = $data['Email'];
                $password = $data['Password'];
                $usertype = $data['Department/Office'];
				$priv = $data['Usertype'];
				$priv = $data['Usertype'];
				$campus = $data['Campus'];

                if($gender == "Male"){
                    $selected_m = "selected";
                    $selected_fm = "";
                }
                else{
                    $selected_m = "";
                    $selected_fm = "selected";
                }
        }
        $error = "";
    }
    if($_SERVER["REQUEST_METHOD"] == "POST") {
        // username and password sent from form 
        
        $my_lname = mysqli_real_escape_string($db,$_POST['lname']);
        $my_fname = mysqli_real_escape_string($db,$_POST['fname']); 
        $my_m_initial = mysqli_real_escape_string($db,$_POST['middle_in']); 
        $my_usertype = mysqli_real_escape_string($db,$_POST['usertype']); 
		
		$date = new DateTime($my_birthdate);
		$now = new DateTime();
		$age = $now->diff($date);
		$age_now = $age->y;

		if (ctype_alpha($my_m_initial)) {
					$sql = "UPDATE members Set `Last_Name` = '$my_lname', `First_Name` = '$my_fname', `Middle_Initial` = '$my_m_initial', `Department/Office`='$my_usertype' WHERE ID='$ID'"; 
			
					$result = mysqli_query($db,$sql);
					// If result matched atleast one, table row must be 1 row
					
						if($result) {
							header("location: accounts_main-page.php");
							exit();
						}else {
						$error = "Failed to update!";
						}
			} 
			else {
				$error = "Invalid Middle Initial! It only accepts letters";
			}
    }

	if($error != "")
	{
		if($my_gender == "Male"){
			$selected_m = "selected";
			$selected_fm = "";
		}
		else{
			$selected_m = "";
			$selected_fm = "selected";
		}

		$lname = $my_lname;
        $fname = $my_fname;
        $m_initial = $my_m_initial;
        $usertype = $my_usertype;
		
	}

?>

<!DOCTYPE html>
<html lang="en">
<head>
	<title>Sign up</title>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
<!--===============================================================================================-->	
	<link rel="icon" type="image/png" href="images/icons/minsu-icon.ico"/>
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="vendor/bootstrap/css/bootstrap.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="fonts/font-awesome-4.7.0/css/font-awesome.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="fonts/iconic/css/material-design-iconic-font.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="vendor/animate/animate.css">
<!--===============================================================================================-->	
	<link rel="stylesheet" type="text/css" href="vendor/css-hamburgers/hamburgers.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="vendor/animsition/css/animsition.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="vendor/select2/select2.min.css">
<!--===============================================================================================-->	
	<link rel="stylesheet" type="text/css" href="vendor/daterangepicker/daterangepicker.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="css/util.css">
	<link rel="stylesheet" type="text/css" href="css/main.css">
<!--===============================================================================================-->
</head>
<body>
	
	<div class="limiter" style="width: 100%;">
		<div class="container-login100" style="width: 100%;">
			<div class="wrap-login100 p-l-55 p-r-55 p-t-65 p-b-54" style="width: 100%;">
                <center><h4 style="font-weight: bold; margin-top: -15px;">UPDATE</h4><br></center>
				<form class="login100-form validate-form" action="" method="post" style="width: 100%;">
					<div class="wrap-input100 validate-input m-b-23" data-validate = "Lastname is required" >
						<span class="label-input100">Lastname</span>
						<input class="input100" value="<?php echo $lname;?>" type="text" name="lname" placeholder="Type your last name" maxlength = "50">
						<span class="focus-input100"></span>
					</div>

                    <div class="wrap-input100 validate-input m-b-23" data-validate = "First Name is required">
						<span class="label-input100">First Name</span>
						<input class="input100" value="<?php echo $fname;?>" type="text" name="fname" placeholder="Type your first name" maxlength = "50">
						<span class="focus-input100"></span>
					</div>

                    <div class="wrap-input100 m-b-23">
						<span class="label-input100">Middle Initial</span>
						<input class="input100" value="<?php echo $m_initial;?>" type="text" name="middle_in" placeholder="Type your middle initial" maxlength = "1" >
						<span class="focus-input100"></span>
					</div>

					<br>
					<label style="">Email : <p style="font-size: 15px; font-weight: bold; margin-left: 35px; margin-top: 10px;"><?php echo $email;?></p></label>
					<br><br>
					<label style="">Campus : <p style="font-size: 15px; font-weight: bold; margin-left: 35px; margin-top: 10px;"><?php echo $campus;?></p></label>
					<br><br>
					<label style="">Usertype : <p style="font-size: 15px; font-weight: bold; margin-left: 35px; margin-top: 10px;"><?php echo $priv;?></p></label>
					<br><br>
					<label>Department/Office : <p style="font-size: 15px; font-weight: bold; margin-left: 35px; margin-top: 10px;"><?php echo $usertype; ?></p></label>

					<div class="container-login100-form-btn m-t-20" style="color:red;">
						<?php echo $error;?> <hr>
					</div>
					
					<button class="login100-form-btn" style="background-color: #24a0ed; width: 150px; border-radius: 5px; height: 40px; float:right">Save</button>
				</form>
				<a href="accounts_main-page.php" style="color: white;"><button style="background-color: gray; width: 150px; margin-right: 5px; border-radius: 5px; height: 40px; color: white; float:right;">CANCEL</button></a>
			</div>
		</div>
	</div>
	

	<div id="dropDownSelect1"></div>

	<!--<script> 
	function add(bd){ 
    //calculate month difference from current date in time  
    var month_diff = Date.now() - dob.getTime();  
    //convert the calculated difference in date format  
    var age_dt = new Date(month_diff);   
    //extract year from date      
    var year = age_dt.getUTCFullYear();
	var my_year = dob.getUTCFullYear();  
    //now calculate the age of the user  
    var age = Math.abs(year - my_year);
	
	if(age < 18)
	{
		alert("Invalid birthdate!");
	}
	} 

</script> -->  

<!--===============================================================================================-->
	<script src="vendor/jquery/jquery-3.2.1.min.js"></script>
<!--===============================================================================================-->
	<script src="vendor/animsition/js/animsition.min.js"></script>
<!--===============================================================================================-->
	<script src="vendor/bootstrap/js/popper.js"></script>
	<script src="vendor/bootstrap/js/bootstrap.min.js"></script>
<!--===============================================================================================-->
	<script src="vendor/select2/select2.min.js"></script>
<!--===============================================================================================-->
	<script src="vendor/daterangepicker/moment.min.js"></script>
	<script src="vendor/daterangepicker/daterangepicker.js"></script>
<!--===============================================================================================-->
	<script src="vendor/countdowntime/countdowntime.js"></script>
<!--===============================================================================================-->
	<script src="js/main.js"></script>

</body>
</html>