<?php 
    session_start();
	if ( !empty($_SESSION['login']) ) {
        header( 'Location: /' );
        die;
	}

    include 'include/header.php';    
?>

    <div class="wrap">
<?php
    include 'include/database.php';
    
    $is_reset = False;
    if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
        if ( !empty($_POST['user']) ) {
            $connection = connect($database);
            $is_reset = email_reset($connection, $_POST['user']);
            $connection = null;
        } else {
            echo '<strong class="warning center block">All fields must be completed!</strong>';
        }
    }
    
    if ($is_reset) {
        echo '
        <strong class="pad center block info">Please check your email for instructions to reset your password!</strong>
        ';
    } else {
        $user = ( empty($_POST['user']) ? '' : htmlspecialchars($_POST['user']) );
        
        echo '
        <h1 class="pad-top">Password Reset</h1>
        
        <form id="password_reset" method="post" action="">
            <div class="pad-bottom">
                <label class="block" for="user">username:</label>
                <input id="user" type="text" name="user" value="'.$user.'" />
            </div>
            
            <input type="submit" value="reset" />
        </form>
        ';
    }
?>
    
    </div>

<?php include 'include/footer.php'; ?>