<?php
	$id = $_GET['id'];
	if($_SERVER['REQUEST_METHOD'] == "POST"){
		$message = safeString($_POST['message']);
	    $tijd = safeString($_POST["tijd"]);
	    $alarmname = safeString($_POST["AlarmName"]);
	    $streamurl = $_POST["StreamUrl"];
	    $streamport = safeString($_POST["StreamPort"]);

		$streamsplit = parse_url($streamurl);
		$streamip = gethostbyname($streamsplit["host"]);
		$streamurl = $streamsplit["path"];
        $id = safeString($_POST['id']);
	    
	   	if($stmt = $mysqli -> prepare("SELECT max(id) FROM alarm")){
        	$stmt -> execute();
        	$stmt -> bind_result($maxid);
        	$stmt -> fetch();
        	$stmt -> close();
        	$maxid++;
        }
	    if($stmt = $mysqli -> prepare("UPDATE alarm SET message=?, name=?, tijd=?, streamurl=?, streamip=?, streamport=?, id=?  WHERE id=?")){
	    	$stmt ->bind_param("sssssiii", $message, $alarmname, $tijd, $streamurl, $streamip, $streamport, $maxid, $id);
	    	$stmt ->execute();
	    	$stmt ->close();
	    }
	    $id++;
	}
	
	//Hier moet een select komen 
	if($stmt = $mysqli -> prepare("SELECT message, name, streamurl, streamport, streamip, tijd FROM alarm WHERE id = ?")){
    	$stmt -> bind_param("i", $id);
    	$stmt -> execute();
    	$stmt -> bind_result($message, $name, $streamurl, $streamport, $streamip, $tijd);
    	$stmt -> fetch();
    	$stmt -> close();
    }
?>
<div class="col-lg-12">
    <div class="ibox float-e-margins">
        <div class="ibox-title">
            <h5>Alarmen overzicht</h5>
            <div class="ibox-tools">
                <a class="collapse-link">
                    <i class="fa fa-chevron-up"></i>
                </a>
                <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                    <i class="fa fa-wrench"></i>
                </a>
                <ul class="dropdown-menu dropdown-user">
                    <li><a href="#">Alarm toevoegen</a></li>
                    <li><a href="#">Alle alarmen verwijderen</a></li>
                </ul>
            </div>
        </div>
        <div class="ibox-content">
            <form method="POST" class="form-horizontal">
                <input name="id" type="hidden" value="<?php echo $id ?>" />
                <div class="form-group"><label class="col-sm-2 control-label">Naam</label>
                    <div class="col-sm-10"><input name="AlarmName" value="<?php echo $name; ?>" type="text" class="form-control"></div>
                </div>
				<div class="form-group">
					<label class="col-sm-2 control-label">Streamurl</label>
					<div class="col-sm-8">
						<input type="url" required name = "StreamUrl" value="<?php echo $streamip.$streamurl ?>" class="form-control">
					</div>
					<div class="col-sm-2">
						<input type="number" min"0" max="9999" value="80" value="<?php echo $streamport ?>" required name = "StreamPort" class="form-control">
					</div>
				</div>
		        <div class="form-group">
                        <div class="col-sm-4 col-sm-offset-2">
                        <button class="btn btn-primary" type="submit">Prefix opslaan</button>
                        <button class="btn btn-primary" type="reset">Reset</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


