<?php
 	include_once('includes/db_connect.php');
	include_once('includes/global_functions.php');
?>
<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Streamit Alarmradio</title>

    <link href="includes/css/bootstrap.min.css" rel="stylesheet">
    <link href="includes/font-awesome/css/font-awesome.css" rel="stylesheet">

    <!-- Morris -->
    <link href="includes/css/plugins/morris/morris-0.4.3.min.css" rel="stylesheet">

    <link href="includes/css/animate.css" rel="stylesheet">
    <link href="includes/css/style.css" rel="stylesheet">
    
	<link href="includes/js/plugins/colorpicker/bootstrap-colorpicker.min.css" rel="stylesheet">
</head>

<body class="fixed-sidebar gray-bg">
    <div class="middle-box text-center loginscreen animated fadeInDown">
        <div>
            <div>
                <h1 class="logo-name">S®</h1>
            </div>
            <h3>SaltyRadio</h3>
            <p>Registeer pagina</p>
            <?php
            	if($_SERVER['REQUEST_METHOD'] == "POST"){
            		//Get username and password
            		$username  = safeString($_POST['username']);
            		$password = safeString($_POST['password']);
                    $radiotoken = safeString($_POST['radiotoken']);
                    $firstname = safeString($_POST['firstname']);
                    $lastname = safeString($_POST['lastname']);
                    $email = safeString($_POST['email']);
            
            
            		//Check if user exists
            		if($stmt1 = $mysqli -> prepare("SELECT users.uid FROM users LEFT JOIN login ON users.uid = login.uid WHERE login.username=? OR users.radiotoken = ?")){
            			$stmt1 -> bind_param('ss', $username, $radiotoken);
            			$stmt1 -> execute();
            			$stmt1 -> bind_result($id);
            			$stmt1 -> fetch();
            			$stmt1 -> close();
            		}
            		if(!isset($id) || $id != ""){
            		    print('	    <div style="color: red" class="alert alert-danger alert-dismissable">
                                        <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>Gebruikersnaam of radiotoken al in gebruik.
                                    </div>');
            		}else{
            			include("includes/crypt.php");
            			$phash = password_hash($password, PASSWORD_DEFAULT);
            			if($stmt = $mysqli -> prepare("INSERT INTO login(username, password) VALUES (?,?)")){
            				$stmt	-> bind_param('ss', $username, $phash);
            				$stmt	-> execute();
            				$uidd = $stmt->insert_id;
            				$stmt 	-> close();		
            			}
            			if($stmt2 = $mysqli -> prepare("INSERT INTO users(uid, radiotoken, first_name, last_name, email) VALUES (?, ?,?,?,?)")){
            				$stmt2	-> bind_param('issss', $uidd, $radiotoken, $firstname, $lastname,$email);
            				$stmt2	-> execute();
            				$stmt2	-> close();
            			}
            			logout("register");
            		}
            	}            
            
            ?>
            <form class="m-t" role="form" method="POST" action="">
                <div class="form-group">
                    <input type="text" name="firstname" class="form-control" placeholder="Voornaam" required="">
                </div>
                <div class="form-group">
                    <input type="text" name="lastname" class="form-control" placeholder="Achternaam" required="">
                </div>
                <div class="form-group">
                    <input type="email"name="email" class="form-control" placeholder="Email" required="">
                </div>
                <div class="hr-line-dashed"></div>
                <div class="form-group">
                    <input type="text" name="username" class="form-control" placeholder="Gebruikersnaam" required="">
                </div>
                <div class="form-group">
                    <input type="password" name="password" class="form-control" placeholder="Wachtwoord" required="">
                </div>
                <div class="form-group">
                    <input type="text" name="radiotoken" class="form-control" placeholder="Radio serial nummer" maxlength="6" required="">
                    <span class="help-block m-b-none">Het radio nummer staat onder op het apparaat</span>
                </div>                
                <div class="form-group">
                        <div class="checkbox i-checks"><label> <input type="checkbox"><i></i> Accepteer de voorwaarde </label></div>
                </div>
                <button type="submit" class="btn btn-primary block full-width m-b">Registreer</button>

                <p class="text-muted text-center"><small>Al een account?</small></p>
                <a class="btn btn-sm btn-white btn-block" href="login.php">Login</a>
            </form>
            <p class="m-t"> <small>StreamIT &copy; 2016</small> </p>
        </div>
    </div>
<?php 	include_once("includes/footer.php"); ?>
