<?php
	
	
	require 'autoload.php';
	use Parse\ParseClient;
	use Parse\ParseObject;
	use Parse\ParseQuery;
	use Parse\ParseACL;
	use Parse\ParsePush;
	use Parse\ParseUser;
	use Parse\ParseInstallation;
	use Parse\ParseException;
	use Parse\ParseAnalytics;
	use Parse\ParseFile;
	use Parse\ParseCloud;
	
	include 'includes/func.php';
	include 'config.php';
	
	ParseClient::initialize($ApplicationID, $RestAPIKEY, $MasterKey);
	ParseClient::setServerURL($serverURLB4APP, '/');
	
	require 'includes/Url.php';
	require 'includes/Paging.php';
	
	$objBeacons = new ParseQuery("beacon");
												
	$objBeacons->descending('createdAt');
	$result = $objBeacons->find();

	$objPaging = new Paging($result, 1);
	$rows = $objPaging->getRecords();
	
	$objPaging->url = $objPaging->url;

?>

<!DOCTYPE html>
<html lang="en">

<head>

	<title>Add New Beacon</title>
	<?php include 'includes/head.php'; ?>
	<?php include 'includes/head.php'; ?>
	<link rel="stylesheet" href="../assets/css/jquery.multiselect.css" />
</head>

<body>

    <div id="wrapper">

        <?php include 'includes/header.php';?>
        <div id="page-wrapper">

            <div class="container-fluid">
                <div class="row">
					<div class="col-lg-12">
                         <h1 class="page-header">
                            Add New Beacon Device
							<a href="manage-beacons.php" class="btn btn-danger pull-right">Go Back</a>
                        </h1>
						
							<?php
								if ( isset($_POST["btnAddBeacon"]) ) {
			
									// setting and saving
									$testObject = ParseObject::create( "beacon" );
									$testObject->set( "unique_id", $_POST["uniqueID"] );
									$testObject->set( "major", $_POST["major"] );
									$testObject->set( "minor", $_POST["minor"] );
                                	$testObject->set( "name_space_id", $_POST["name_space_id"] );
                                	$testObject->set( "instance_id", $_POST["instance_id"] );
									//$implode = implode(',',$_POST['proximity']);
									//$testObject->set( "proximity", $implode );
									$testObject->set( "proximity_uuid", $_POST["PROXIMITY_UUID"] );
									$testObject->set( "macAddress", $_POST["macAddress"] );
									$testObject->set( "type", $_POST["type"] );
									$testObject->save();
								
								//header('Location: manage-beacons.php');
							 ?>
							 <script>
								//alertify.alert('Successfully saved', 'Beacon Device save successfully!');
								alertify.alert('Successfully saved', 'New beacon device has been saved successfully.!', function(){
									//alertify.success('Ok'); 
									window.location = "manage-beacons.php";
								});
							</script>
							<?php }?>
						
						<div class="panel panel-default">
						  <div class="panel-heading">Beacon Details</div>
						  <div class="panel-body">
								<div class="row">
									<div class="col-md-6">
										<form method="post" >
											<div class="form-group">
												<label for="uniqueID" class="form-label">Unique ID</label>
												<input type="text" class="form-control" id="uniqueID" name="uniqueID" placeholder="Enter beacon device unique id"  required data-error-msg="Please enter Beacon device Unique ID"  />
											</div>
											<!-- <div class="form-group">
												<label for="buildingAddress" class="form-label">Proximity</label>
												<select class="form-control search" name="proximity[]" id="proximity" data-error-msg="Please select Proximity" multiple>
														<option value="immediate">Immediate</option>
														<option value="near">Near</option>
														<option value="far">Far</option>
														<option value="unknown">Unknown</option>
												</select>
											</div> -->

											<div class="form-group">
												<label for="type" class="form-label">Beacon Type</label>
												<select class="form-control" onchange="changeSelection()" id="type" name="type" id="type" data-error-msg="Please select Type" >
														<option value="iBeacon">iBeacon</option>
														<option value="EddyStone">EddyStone</option>
												</select>
											</div>
											
											<div class="form-group iBeacon">
												<label for="PROXIMITY_UUID"  class="form-label">Proximity UUID</label>
												<input type="text" class="form-control"  id="PROXIMITY_UUID" name="PROXIMITY_UUID" placeholder="Enter beacon device PROXIMITY UUID" data-error-msg="Please enter Beacon device PROXIMITY UUID"/>
											</div>

											<div class="form-group iBeacon">
												<label for="major"  class="form-label">Major</label>
												<input type="text" class="form-control"  id="PROXIMITY_UUID" name="major" placeholder="Enter beacon device major" data-error-msg="Please enter Beacon device major"/>
											</div>

											<div class="form-group iBeacon">
												<label for="minor"  class="form-label">Minor</label>
												<input type="text" class="form-control"  id="minor" name="minor" placeholder="Enter beacon device minor" data-error-msg="Please enter Beacon device minor"/>
											</div>
											
                                            <div class="form-group EddyStone" style="display:none">
												<label for="major"  class="form-label">Namespace</label>
												<input type="text" class="form-control"  id="name_space_id" name="name_space_id" placeholder="Enter name space id" data-error-msg="Please enter name space id"/>
											</div>

											<div class="form-group EddyStone" style="display:none">
												<label for="minor"  class="form-label">Instance</label>
												<input type="text" class="form-control"  id="instance_id" name="instance_id" placeholder="Enter instance id" data-error-msg="Please enter instance"/>
											</div>
											
											
											<div class="form-group">
												<label for="macAddress"  class="form-label">MAC Address</label>
												<input type="text"  class="form-control" id="macAddress" name="macAddress" placeholder="Enter beacon device MAC address" />
											</div>
											
											<div class="form-group">
												<input type="submit"  class="btn btn-success" name="btnAddBeacon" value="Save" />
												 <input type="reset" class="btn btn-info" id="resetBtn" />
												<a href="manage-beacons.php" class="btn btn-danger">Cancel</a>
											</div>
										</form>
									</div>
								</div>
						  </div>
						</div>
                    </div>
                </div>
                <!-- /.row -->

              

               

            </div>
            <!-- /.container-fluid -->

        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->

   <?php include 'includes/footer.php'; ?>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"></script>
	<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
	<script type="text/javascript" src="../assets/js/jquery.multiselect.js"></script>
   <script type="text/javascript">
   	function changeSelection(){
   		var type = $('#type').val();
   		$('.iBeacon,.EddyStone').hide();
   		$('.iBeacon input,.EddyStone input').val('');	
   		$('.'+type).show();
   }
      $(document).ready(function(){
      	$('select#proximity').multiselect({
            columns: 2,
            placeholder: 'Select Proximity',
            search: true,
            searchOptions: {
                'default': 'Search Proximity'
            },
            selectAll: true
        });
        $('#addBeaconDevice').attrvalidate();
        $('#resetBtn').click(function(){ 
			//alertify.alert('Alert Title', 'Beacon Device save successfully!', function(){ alertify.success('Ok'); });
			
			$('#addBeaconDevice').attrvalidate('reset'); });

      });
    </script>

</body>

</html>
