<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
error_reporting(0);
session_start();
require_once "config.php";
$staff_id = $_GET["staff_id"];
if($staff_id == "")
	$staff_id = $_POST["staff_id"];
echo $sqlUsernameStaff = "select username from users where id = $staff_id"  ;
$runUsernameStaff = mysqli_query($link, $sqlUsernameStaff);
if($runUsernameStaff === FALSE){
die(mysqli_error($link));
}
$resultUsernameStaff = mysqli_fetch_assoc($runUsernameStaff);
$username = $resultUsernameStaff["username"];
$_SESSION["username_staff"] = $username;
$displaySuccessUpdate = "display:none";
$displayUpdatePassword = "display:block";

if(isset($_POST["submit"])){
	
	 if(empty($_POST["staff_id"]) || empty($_POST["new_password"]))  
	{  
	   echo '<script>alert("Both Fields are required")</script>';  
	}  
	else  
	{  
	  // $username = mysqli_real_escape_string($connect, $_POST["username"]);  
	   $password = mysqli_real_escape_string($connect, $_POST["password"]);  
	   $password = password_hash($password,PASSWORD_BCRYPT);
	   $rememberToken = bin2hex(random_bytes(16));
	  // echo $query = "INSERT INTO users(username, password) VALUES('$username', '$password')";  
	   echo $query = "udpdate users set password = '$password',remember_token = '$rememberToken', updated_at = '".date("Y-m-d H:i:s")."' where id = $staff_id";  
	   if(mysqli_query($connect, $query))  
	   {  
			echo '<script>alert("update password Done")</script>';  
	   } 
		else{
			
			$displaySuccessUpdate = "display:block";
			$displayUpdatePassword = "display:none";
			
		}	   
	}  
	
	/* echo "STAFF ID: " .$staff_id = $_POST["staff_id"];
	echo "STAFF PASSWORD: " .$password = $_POST["password"];
	$password = password_hash($_POST["password"], PASSWORD_DEFAULT);
	$rememberToken = bin2hex(random_bytes(16));
	
	echo $sqlUpdatePassword = "update users set password = '$password', remember_token = '$rememberToken', updated_at = '".date("Y-m-d H:i:s")."' where id = $staff_id ";
	$runUpdatePassword = mysqli_query($link, $sqlUpdatePassword);
	if($runUpdatePassword === FALSE){
	   die(mysqli_error($link));
	}
	else{
		
		$displaySuccessUpdate = "display:block";
		$displayUpdatePassword = "display:none";
		
	} */
}
ELSE {
	ECHO "tak masuk!!!";
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <!-- Required meta tags-->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="au theme template">
    <meta name="author" content="Hau Nguyen">
    <meta name="keywords" content="au theme template">

    <!-- Title Page-->
    <title>Login</title>

    <!-- Fontfaces CSS-->
    <link href="css/font-face.css" rel="stylesheet" media="all">
    <link href="vendor/font-awesome-4.7/css/font-awesome.min.css" rel="stylesheet" media="all">
    <link href="vendor/font-awesome-5/css/fontawesome-all.min.css" rel="stylesheet" media="all">
    <link href="vendor/mdi-font/css/material-design-iconic-font.min.css" rel="stylesheet" media="all">

    <!-- Bootstrap CSS-->
    <link href="vendor/bootstrap-4.1/bootstrap.min.css" rel="stylesheet" media="all">

    <!-- Vendor CSS-->
    <link href="vendor/animsition/animsition.min.css" rel="stylesheet" media="all">
    <link href="vendor/bootstrap-progressbar/bootstrap-progressbar-3.3.4.min.css" rel="stylesheet" media="all">
    <link href="vendor/wow/animate.css" rel="stylesheet" media="all">
    <link href="vendor/css-hamburgers/hamburgers.min.css" rel="stylesheet" media="all">
    <link href="vendor/slick/slick.css" rel="stylesheet" media="all">
    <link href="vendor/select2/select2.min.css" rel="stylesheet" media="all">
    <link href="vendor/perfect-scrollbar/perfect-scrollbar.css" rel="stylesheet" media="all">

    <!-- Main CSS-->
    <link href="css/theme.css" rel="stylesheet" media="all">

	<script text="javascript">
		function validate() {
			var x= document.getElementById("password");
				var y= document.getElementById("retype_password");
			if(x.value==y.value) return;
			else alert("password not same");
		}
	</script>

</head>
<body class="animsition" >
<div class="table-wrapper-scroll-y my-custom-scrollbar">
    <div class="page-wrapper" >
        <div class="page-content--bge5" >
            <div class="container" >
				
                <div class="login-wrap">
                    <div class="login-content">
						
						<!--<div class="login-form" id="staffClick" style='<?php echo $displayStaff?>;font-size:12px'>-->
						<div class="login-form" id="staffClick" style='font-size:12px'>
                            <div class="card">
								<div class="card-header">
									<strong>CHANGE PASSWORD</strong>
								</div>
								<div class="card-body" style="<?php echo $displayUpdatePassword?>">
									<form action="action.php" method="post" >
										<input type="hidden" name="staff_id" value="<?php echo $staff_id?>">
										<div class="form-group">
											<label>USERNAME: <b><?php echo strtoupper($_SESSION["username_staff"]) ?></b></label>
											
											<label></label>
										</div>
										<div class="form-group">
											<label>NEW PASSWORD</label>
											<input class="au-input au-input--full" type="password" id="password" name="new_password" placeholder="Please Insert New Password">
										</div>
										<div class="form-group">
											<label>CONFIRM NEW PASSWORD</label>
											<input class="au-input au-input--full" type="password" id="retype_password" name="confirm_new_password" onblur="validate()" placeholder="Retype Password">
										</div>
										
										<button class="au-btn au-btn--block au-btn--green m-b-20" type="submit" name="submit">LOGIN</button>
									</form>
								</div>
								<div class="card-body" style="<?php echo $displaySuccessUpdate?>">
									SUCCESS UPDATE PASSWORD FOR <?PHP ECHO $_SESSION["username_staff"]?>
								</div>
							</div>
                        </div>
                    </div>
                </div>
                </div>
            </div>
        </div>

    </div>

    <!-- Jquery JS-->
    <script src="vendor/jquery-3.2.1.min.js"></script>
    <!-- Bootstrap JS-->
    <script src="vendor/bootstrap-4.1/popper.min.js"></script>
    <script src="vendor/bootstrap-4.1/bootstrap.min.js"></script>
    <!-- Vendor JS       -->
    <script src="vendor/slick/slick.min.js">
    </script>
    <script src="vendor/wow/wow.min.js"></script>
    <script src="vendor/animsition/animsition.min.js"></script>
    <script src="vendor/bootstrap-progressbar/bootstrap-progressbar.min.js">
    </script>
    <script src="vendor/counter-up/jquery.waypoints.min.js"></script>
    <script src="vendor/counter-up/jquery.counterup.min.js">
    </script>
    <script src="vendor/circle-progress/circle-progress.min.js"></script>
    <script src="vendor/perfect-scrollbar/perfect-scrollbar.js"></script>
    <script src="vendor/chartjs/Chart.bundle.min.js"></script>
    <script src="vendor/select2/select2.min.js">
    </script>

    <!-- Main JS-->
    <script src="js/main.js"></script>

</body>

</html>
<!-- end document-->