<?php 
include('includes/connectdb.php');

if(!isset($_GET['user_id'])){
	header('location:index');
}

require("admin/production/class/cusers.php");
require("admin/production/class/cemail.php");

$output = '';
if(isset($_POST['submit'])){
	$user_id = $_GET['user_id'];
	
	$password  =  $_POST['password'];
	$cpassword =  $_POST['cpassword'];
	$user = new Users('account', $user_id);
	$firstname = $user->getFirstName();
	$email = $user->getUserEmail();
	
	$send_email = new Email();
	
	if($password == $cpassword){
		if($user->updateUserPassword($password)){
			$send_email->sendPasswordChangeConfirmation($email, $firstname, $user_id);
			$output =   '<div class="alert alert-success alert-intro" role="alert">
							Password reset successful. <a href="login">Click here to login</a>
						</div>';
		}else{
			$output = '<div class="alert alert-danger alert-intro" role="alert">
						Failed to reset password. Please try again.
					</div>';
		}
	}else{
		$output = '<div class="alert alert-danger alert-intro" role="alert">
						Password not the same.
					</div>';
	}
}

include('includes/head.php');

?>

<body>
    <div class="page-wrapper">
        <?php include('includes/header.php') ?>

        <main class="main">
            <nav aria-label="breadcrumb" class="breadcrumb-nav">
                <div class="container">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="index"><i class="icon-home"></i></a></li>
                        <li class="breadcrumb-item active" aria-current="page">Forgot Password</li>
                    </ol>
                </div><!-- End .container -->
            </nav>

            <div class="container">
                <div class="heading mb-4">
                    <h2 class="title">Change Password</h2>
                    <p>Please fill the information below to change your password.</p>
                </div><!-- End .heading -->

                <form action="reset-password?user_id=<?php echo $_GET['user_id']; ?>" method="post">
				    
					<div class="form-group required-field col-lg-5">
						<?php echo $output;  ?>
					</div><!-- End .form-group -->
					
                    <div class="form-group required-field">
                        <label for="reset-email">New Password</label>
                        <input type="password" class="form-control" id="password" name="password" required>
                    </div><!-- End .form-group -->
					
					<div class="form-group required-field">
                        <label for="reset-email">Confirm Password</label>
                        <input type="password" class="form-control" id="cpassword" name="cpassword" required>
                    </div><!-- End .form-group -->

                    <div class="form-footer">
                        <button type="submit" name="submit" class="btn btn-primary">Submit</button>
                    </div><!-- End .form-footer -->
                </form>
            </div><!-- End .container -->

            <div class="mb-10"></div><!-- margin -->
        </main><!-- End .main -->

        <?php include('includes/footer.php') ?>
    </div><!-- End .page-wrapper -->

    <div class="mobile-menu-overlay"></div><!-- End .mobil-menu-overlay -->

    <?php include('includes/mobile-menu.php') ?>

    <a id="scroll-top" href="#top" title="Top" role="button"><i class="icon-angle-up"></i></a>

    <!-- Plugins JS File -->
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/js/bootstrap.bundle.min.js"></script>
    <script src="assets/js/plugins.min.js"></script>

    <!-- Main JS File -->
    <script src="assets/js/main.min.js"></script>
</body>

</html>