<?php
if (isset($_POST['change_password_btn'])) {
    $username = inject_checker($connection, $_POST['username']);
    $new_password = inject_checker($connection, $_POST['new_password']);
    
    $hashed_password = password_hash($new_password, PASSWORD_DEFAULT);

    if (empty($username) || empty($new_password)) {
        $_SESSION['msg'] = "All fields are required";
        $_SESSION['type'] = "warning";
    } else {
        // Check staff table (admin/staff combined)
        $stmt = $connection->prepare("SELECT id FROM `staff` WHERE `username` = ?");
        $stmt->bind_param("s", $username);
        $stmt->execute();
        $stmt->store_result();

        if ($stmt->num_rows === 1) {
            $stmt->close();

            // Update password
            $stmt = $connection->prepare("UPDATE `staff` SET `password` = ? WHERE `username` = ?");
            $stmt->bind_param("ss", $hashed_password, $username);

            if ($stmt->execute()) {
                $_SESSION['msg'] = "Staff Password reset successfully.";
		        $_SESSION['type'] = "success";
            } else {
                $_SESSION['msg'] = "Failed to reset staff password.";
		        $_SESSION['type'] = "warning";          
            }
            $stmt->close();
        } else {
            $stmt->close();

            // Check students table
            $stmt = $connection->prepare("SELECT id FROM `students` WHERE `reg_number` = ?");
            $stmt->bind_param("s", $username);
            $stmt->execute();
            $stmt->store_result();

            if ($stmt->num_rows === 1) {
                $stmt->close();

                $stmt = $connection->prepare("UPDATE `students` SET `gen_password` = ? WHERE `reg_number` = ?");
                $stmt->bind_param("ss", $hashed_password, $username);

                if ($stmt->execute()) {
                    $_SESSION['msg'] = "Student Password reset successfully.";
			        $_SESSION['type'] = "success";  
                } else {
                	$_SESSION['msg'] = "Failed to reset student password.";
			        $_SESSION['type'] = "error"; 
                }
                $stmt->close();
            } else {
            	$_SESSION['msg'] = "User/Staff does not exist.";
			    $_SESSION['type'] = "error"; 
            }
        }
    }
    header("Location: reset_password");
    exit();
}
?>


<div class='page-title'>
    <div>
        <h1><i class='fa fa-key'></i> Reset Password</h1>
        <p class='text-center'>Recover forgotten password</p>
    </div>    
</div>

<div class="card">
    <form method = 'POST' action = ''>	
        <section>
            <div class = 'row'>
                <div class = 'col-xs-12 col-sm-12 col-md-12 shift'>
                    <div class = 'panel panel-primary ch'>
                        <div class = 'panel-heading'>
                            <h4 class = 'txt'>Staff/Student Password Reset</h4>
                        </div><br/>
                        <div class = 'panel-body'>
                            <div class='row'>
                                <div class = 'col-xs-12 col-sm-12 col-md-5'>
                                    <div class = 'input-group'>
                                        <span class = 'input-group-addon' id = 'sizing-addon2'>Username/Reg Number:</span>
                                        <input type='text' name='username' class='form-control' value='' required>
                                    </div><br/>
                                </div>
                                
                                <div class = 'col-xs-12 col-sm-12 col-md-4'>
                                    <div class = 'input-group'>
                                        <span class = 'input-group-addon' id = 'sizing-addon2'>New Password:</span>
                                        <input type='password' name='new_password' class='form-control' value='' required>
                                    </div><br/>
                                </div>
                                
                                <div class = 'col-xs-12 col-sm-12 col-md-3'>
                                    <button type = 'submit' name = 'change_password_btn' class = 'btn btn-primary btn-md' style=''><span class = 'glyphicon glyphicon-user'></span>&nbsp; Reset Password</button><br/> 
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class = 'col-xs-12 col-sm-12 col-md-4'></div>
            </div>
        </section>
    </form>
</div>

