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

    include 'include/header.php';    
?>

    <div class="wrap">
<?php
    include 'include/database.php';
    
    $check_validated = False;
    $password_set = False;
    if ( $_SERVER['REQUEST_METHOD'] === 'GET' ) {
        if ( !empty($_GET['user']) && !empty($_GET['validation']) ) {
            $connection = connect($database);
            $check_validated = check_validation($connection, $_GET['user'], $_GET['validation']);
            $connection = null;
        }
    } else if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
        if ( !empty($_POST['user']) && !empty($_POST['validation']) && 
             !empty($_POST['pass']) && !empty($_POST['confirm']) ) {
            
            if ( $_POST['pass'] === $_POST['confirm'] ) {
                $connection = connect($database);
                $is_validated = validate_user($connection, $_POST['user'], $_POST['validation']);
                if ($is_validated) {
                    $password_set = update_password($connection, $_POST['user'], $_POST['pass']);
                }
                $connection = null;
            }
            else {
                $connection = connect($database);
                $check_validated = check_validation($connection, $_GET['user'], $_GET['validation']);
                $connection = null;
                echo '<strong class="warning center block">Passwords do not match!</strong>';
            }
        }
    }
    
    if ($check_validated) {
        $user = ( empty($_GET['user']) ? '' : htmlspecialchars($_GET['user']) );
        $validation = ( empty($_GET['validation']) ? '' : htmlspecialchars($_GET['validation']) );
    
        echo '
            <h1 class="pad-top">Set Password</h1>
        
            <form id="password_reset" method="post" action="">
                <div>
                    <label class="block" for="pass">new password:</label>
                    <input id="pass" type="password" name="pass" autocomplete="off" />
                </div>
                
                <div class="pad">
                    <label class="block" for="confirm">confirm password:</label>
                    <input id="confirm" type="password" name="confirm" autocomplete="off" />
                </div>
                
                <input id="user" type="hidden" name="user" value="'. $user .'" />
                <input id="validation" type="hidden" name="validation" value="'. $validation .'" />

                <input type="submit" value="reset" />
            </form>
        ';
    } else if ($password_set) {
        echo '
            <strong class="pad center block info">
                Your password has been successfully reset!
            </strong>
            <small class="pad center block info">
                You will be redirected to login <a href="login.php">(or click here)</a>.
            </small>
        ';
        header( 'Refresh: 5; Url=login.php' );
    } else {
        echo '
            <strong class="warning center block">
                An error occured! Please contact the administrator for support.
            </strong>
        ';
    }
?>
    
    </div>

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