<?php
	$id = $_GET['id'];
	if($_SERVER['REQUEST_METHOD'] == "POST"){
		$message = safeString($_POST['message']);
		$snoozetijd = safeString($_POST['snoozetijd']);

	    $tijd = safeString($_POST["tijd"]);
	    $alarmname = safeString($_POST["AlarmName"]);
	    $streamurl = $_POST["StreamUrl"];
	    $streamport = safeString($_POST["StreamPort"]);

		$streamdomain = $streamurl;
		$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=?, snoozetijd=?, streamdomain=?  WHERE id=?")){
	    	$stmt ->bind_param("sssssiiisi", $message, $alarmname, $tijd, $streamurl, $streamip, $streamport, $maxid, $snoozetijd, $streamdomain, $id);
	    	$stmt ->execute();
	    	$stmt ->close();
	    }
	    $id = $maxid;
	    $maxid++;
	    if($stmt = $mysqli -> prepare("ALTER TABLE alarm AUTO_INCREMENT = ?")){
	    	$stmt ->bind_param("i", $maxid);
	    	$stmt ->execute();
	    	$stmt ->close();
	    }

	    print('
	    <div class="alert alert-success alert-dismissable">
            <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>Alarm succesvol gewijzigd
        </div>
	    ');
	}
	
	//Hier moet een select komen 
	if($stmt = $mysqli -> prepare("SELECT message, name, streamurl, streamport, streamip, tijd, snoozetijd, streamdomain FROM alarm WHERE id = ?")){
    	$stmt -> bind_param("i", $id);
    	$stmt -> execute();
    	$stmt -> bind_result($message, $name, $streamurl, $streamport, $streamip, $tijd, $snoozetijd, $streamdomain);
    	$stmt -> fetch();
    	$stmt -> close();
    }
?>
<div class="col-lg-12">
    <div class="ibox float-e-margins">
        <div class="ibox-title">
            <h5>Alarm wijzigen</h5>
            <div class="ibox-tools">
                <a class="collapse-link">
                    <i class="fa fa-chevron-up"></i>
                </a>
            </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">Tijd</label>
                     <div class="col-sm-10">
                        <div class="input-group bootstrap-touchspin">
                            <span class="input-group-btn">
                                <button class="btn btn-white bootstrap-touchspin-down" type="button">-</button>
                            </span>
                            <span class="input-group-addon bootstrap-touchspin-prefix" style="display: none;"></span>
                            <input class="touchspin1 form-control" type="time" value="<?php echo $tijd; ?>" name="tijd" style="display: block;">
                            <span class="input-group-addon bootstrap-touchspin-postfix" style="display: none;"></span>
                            <span class="input-group-btn">
                                <button class="btn btn-white bootstrap-touchspin-up" type="button">+</button>
                            </span>
                        </div>
                    </div>
                </div>
				<div class="form-group">
					<label class="col-sm-2 control-label">Message</label>
					<div class="col-sm-10">
						<input type="text" value="<?php echo $message ?>" required class="form-control" name = "message" maxlength="16">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label">Snooze tijd</label>
					<div class="col-sm-10">
						<input type="number" required class="form-control" name = "snoozetijd" max="10" min="0" value="<?php echo $snoozetijd; ?>"/>
					</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 $streamdomain ?>" 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">Alarm opslaan</button>
                        <button class="btn btn-primary" type="reset">Reset</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


