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

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

        if (count(array($record)) == 1 ) {
            $data = mysqli_fetch_array($record);
            $dept_off = $data['Department/Office'];
			$usertype = $data['Usertype'];
            $status = $data['Status'];
        }
        $error = "";

		if($usertype == "admin"){
            $selected_admin = "selected";
            $selected_member = "";
        }
        else{
            $selected_admin = "";
            $selected_member = "selected";
        }

        if($status == "Active"){
            $selected1 = "selected";
            $selected2 = "";
        }
        else{
            $selected1 = "";
            $selected2 = "selected";
        }
    }
    if($_SERVER["REQUEST_METHOD"] == "POST") {
        // username and password sent from form 
        
		$upd_dept_off = mysqli_real_escape_string($db,$_POST['dept_off']);
        $upd_usertype = mysqli_real_escape_string($db,$_POST['usertype']);
        $upd_status = mysqli_real_escape_string($db,$_POST['status']); 

        $sql = "UPDATE usertype Set `Department/Office` = '$upd_dept_off', `Usertype` = '$upd_usertype', `Status` = '$upd_status' WHERE ID='$ID'"; 

        $result = mysqli_query($db,$sql);
        // If result matched atleast one, table row must be 1 row
        
            if($result) {
                echo '<script>alert("Usertype was updated successfully!");</script>';
                header("location: usertype.php");
                exit();
            }else {
            $error = "Failed to update!";
            }
    }

	if($error != "")
	{
		// $lname = $my_lname;
        // $fname = $my_fname;
        // $m_initial = $my_m_initial;
        // $birthdate = $my_birthdate;
        // $gender = $my_gender;
        // $address = $my_address;
        // $contact = $my_contact;
        // $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%; margin-top: -50px;">
			<div class="wrap-login100 p-l-55 p-r-55 p-t-65 p-b-54" style="width: 100%;"><br><br>
            <center><h4 style="font-weight: bold; margin-top: -45px;">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 = "Usertype is required">
						<span class="label-input100">Department/Office</span>
						<input class="input100" value="<?php echo $dept_off;?>" type="text" name="dept_off" placeholder="Enter usertype" maxlength = "50">
						<span class="focus-input100"></span>
					</div>
					<span class="label-input100">Usertype</span><br>
					<select name="usertype" class="field input" style="width: 200px; float: left; border-color: white; border-radius: 5px; background-color: white; border: 2px solid #e8e8e8; height: 40px; font-size: 17px; margin-top: 5px; margin-left: 0px; padding:5px;" required>
                        <option value="">Select Usertype :</option>
                        <option value="admin" <?php echo $selected_admin; ?>>admin</option>
                        <option value="member" <?php echo $selected_member; ?>>member</option>
                        </select>
					<br><br>
					<span class="label-input100">Status</span><br>
                    <select name="status" class="field input" style="width: 392px; border-color: white; border-radius: 5px; background-color: white; border: 2px solid #e8e8e8; height: 40px; font-size: 17px; margin-top: 5px; padding:5px;" required>
							<option value="">Select Status :</option>
                            <option value="Active" <?php echo $selected1; ?>>Active</option>
                            <option value="Inactive" <?php echo $selected2;?>>Inactive</option>
						</select>

					<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="usertype.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>