<?php 
include("connect.php");

if (!isset($_REQUEST['key'])) {
	header("Location: index.php");
}else {
	$hash = $_REQUEST['key'];	
}
$query = "SELECT * FROM CUSTOMER WHERE ResetHash = '$hash'";
$result = mysqli_query($con, $query);
$numRows = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);
if ($numRows == 0) {
	header("Location: login.php?error=invalidKey");
	break;
}
if (isset($_POST['submit'])) {
	$passwordA = mysqli_real_escape_string($con, $_POST['inputPassword']);
	$passwordB = mysqli_real_escape_string($con, $_POST['inputPassword2']);
	if (($passwordA != $passwordB) || ($passwordA == "")) {
		$error = "Error!";
		
	}else if (strlen($passwordA) < 6){
		$error = "Error! Password must be at least 6 characters";
		
	}else {
		$passwordA = md5($passwordA);
		$query = "UPDATE CUSTOMER SET Password = '" . $passwordA . "', ResetHash = NULL WHERE AccountNumber = '" . $row['AccountNumber'] . "'";
		mysqli_query($con, $query);

		header("Location: login.php");
		break;
		
	}
	
}
 ?>
 
 <!DOCTYPE html>
 <html lang="en">
   <head>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <title><?= $config['CompanyName'] ?></title>
 
     <!-- Bootstrap -->
     <link href="css/bootstrap.min.css" rel="stylesheet">
     <link rel="stylesheet" href="css/bootstrap-combobox.css" />
 
     <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
     <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
     <!--[if lt IE 9]>
       <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
       <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
       <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
           <!-- Include all compiled plugins (below), or include individual files as needed -->
           <script src="js/bootstrap.min.js"></script>
       	 <script src="js/bootstrap-combobox.js"></script>
           <script type="text/javascript">
             $(document).ready(function(){
               $('.combobox').combobox();
       //        $('.grpline').css("width", "100px");
             });
           </script>

     <![endif]-->
	<link rel="stylesheet" href="css/styles.css"/>
   </head>
   <body>
   
     

   
   <!-- Stack the columns on mobile by making one full-width and the other half-width -->
<div class="container " style="padding-top: 10px;">
	<div class="well">
		<h3>Reset Password</h3>
		<h4><?= $row['AccountNumber'] . " - " . $row['Name'] ?></h4>
		
		<form id="pwResetForm" class="form-horizontal" method="post">
		  <div class="form-group">
		    <label for="inputPassword" class="col-sm-2 control-label">Password</label>
		    <div class="col-sm-10">
		      <input type="password" class="form-control" name="inputPassword" id="inputPassword" placeholder="Password">
		    </div>
		  </div>
		  <div class="form-group">
		    <label for="inputPassword2" class="col-sm-2 control-label">Reenter Password</label>
		    <div class="col-sm-10">
		      <input type="password" class="form-control" name="inputPassword2" id="inputPassword2" placeholder="Password">
		    </div>
		  </div>
		  <div class="form-group">
		    <div class="col-sm-offset-2 col-sm-10">
		      <button  id="submitBtn" name="submit" class="btn btn-default" value="true">Submit</button>
		    </div>
		  </div>
		</form>
		
		
	</div>

</div>
       <script type="text/javascript">
       		<?php 
       		if (isset($error)) {
       			echo("alert(\"$error\");");
       		}
       		
       		 ?>
       		$('#submitBtn').click(function () {
       			if($('#inputPassword').val() != $('#inputPassword2').val()){
       				alert("Passwords doesn't match!");
       				return false;
       			}else {
       				$('#pwResetForm').submit();
       			}
       			
       		});
       </script>
     
   </body>
 </html>