<?php
	if($_SERVER['REQUEST_METHOD'] == "POST"){
		$message = safeString($_POST['message']);
	    $tijd = safeString($_POST["tijd"]);
	    $alarmname = safeString($_POST["AlarmName"]);
	    $streamurl = $_POST["StreamUrl"];
	    $streamport = safeString($_POST["StreamPort"]);
	    $snoozetijd = safeString($_POST["snoozetijd"]);
		$onlyOnce = safeString($_POST["onlyOnce"]);
		if (filter_var($streamurl, FILTER_VALIDATE_URL) === FALSE) {
			 print('
		    <div class="alert alert-danger alert-dismissable">
	            <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>Stream url ongeldig
	        </div>
		    ');
		}else{
			$streamdomain = $streamurl;
			$streamsplit = parse_url($streamurl);
			$streamip = gethostbyname($streamsplit["host"]);
			$streamurl = $streamsplit["path"];
			
		    if($stmt = $mysqli -> prepare("INSERT INTO alarm(message,name,streamip, streamurl, streamport, tijd, snoozetijd, streamdomain, onlyOnce, uid) VALUES(?,?,?,?,?,?,?,?,?,?)")){
		    	$stmt ->bind_param("ssssisisii", $message, $alarmname, $streamip,  $streamurl, $streamport, $tijd, $snoozetijd, $streamdomain,$onlyOnce, $userid);
		    	$stmt ->execute();
		    	$stmt ->close();
		    }
		    reDirect("alarmen.php?i=overzicht");			
		}
	}
?>

<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>
			</div>
		</div>
		<div class="ibox-content">
			<form method="POST" action="" class="form-horizontal">
				<div class="form-group">
					<label class="col-sm-2 control-label">Naam</label>
					<div class="col-sm-10">
						<input type="text" required name = "AlarmName" 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 required class="touchspin1 form-control" type="time" value="" 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" required class="form-control" name = "message" maxlength="16">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label">Snooze tijd (minuten)</label>
					<div class="col-sm-10">
						<input type="number" required class="form-control" name = "snoozetijd" max="10" min="0" value="5">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label">Streamurl</label>
					<div class="col-sm-8">
						<input type="url" maxlength="48" required name = "StreamUrl" class="form-control">
					</div>
					<div class="col-sm-2">
						<input type="number" min"0" max="9999" value="80" required name = "StreamPort" class="form-control">
					</div>
				</div>
				<div class="form-group">
				<label class="col-sm-2 control-label">Eenmalige-Alarm</label>
				<div class="switch">
					<div class="col-sm-4">
	                    <div class="onoffswitch">
	                           <input type="checkbox" value="1" checked="" class="onoffswitch-checkbox" name = "onlyOnce" id="example1">
	                        <label class="onoffswitch-label" for="example1">
	                       	    <span class="onoffswitch-inner"></span>
	                        	<span class="onoffswitch-switch"></span>
	                        </label>
	                    </div>
                    </div>
                </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>