<?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, '/');
	
	if(isset($_GET["uTp"])){
		$beaconID = $_GET["uTp"];
	
		$objBeacons = new ParseQuery("beacon");
		$objBeacons->get($beaconID);
		$result = $objBeacons->find();
		
		$uniqueID = "";
		$PROXIMITY = "";
		$PROXIMITY_UUID = "";
		$macAddress = "";
		$major ="";
		$minor = "";
		$type = "";
    	$name_space_id = "";
    	$instance_id = "";
	//get Data
	foreach($result as $key){
		$uniqueID = $key->get('unique_id');
		$proximityArr = !empty($key->get('proximity')) ? explode(',', $key->get('proximity')) :'';
		$macAddress = $key->get('macAddress');
		$PROXIMITY_UUID = $key->get('proximity_uuid');	
		$major = $key->get('major');	
		$minor = $key->get('minor');
		$type = $key->get('type');
    	$name_space_id = $key->get('name_space_id');
        $instance_id = $key->get('instance_id');
	}
    $ibeacond = $type != 'iBeacon' ? 'style="display:none;"':'';
    $eddy = $type != 'EddyStone' ? 'style="display:none;"':'';
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Edit Beacon</title>
<!DOCTYPE html>
<html lang="en">
<head>
	<title>Add New Beacons</title>
	<?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">
                            Edit Beacon Device
							<a href="manage-beacons.php" class="btn btn-danger pull-right">Go Back</a>
                        </h1>
							<?php
								if ( isset($_POST["btnAddBeacon"])){			
									$objBeacons = new ParseObject("beacon", $beaconID);	
									//$objAdminUsers->set( "userName", $_POST["userName"] );
									$objBeacons->set( "unique_id", $_POST["uniqueID"] );
									$objBeacons->set( "major", $_POST["major"] );
									$objBeacons->set( "minor", $_POST["minor"] );
                                	$objBeacons->set( "name_space_id", $_POST["name_space_id"] );
									$objBeacons->set( "instance_id", $_POST["instance_id"] );
									//$proximity = implode(',',$_POST['proximity']);
									//$objBeacons->set( "proximity", $proximity);
									$objBeacons->set( "macAddress", $_POST["macAddress"]);
									$objBeacons->set( "proximity_uuid", $_POST["PROXIMITY_UUID"]);
									$objBeacons->set( "type", $_POST["type"]);
									$objBeacons->save();
							 ?>
							 <script>
								//alertify.alert('Successfully saved', 'Beacon Device save successfully!');
								alertify.alert('Successfully updated', 'Beacon device has been edit 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" value="<?php echo $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 <?php echo in_array('immediate',$proximityArr) ? 'selected="selected"':'';?> value="immediate">Immediate</option>
													<option <?php echo in_array('near',$proximityArr) ? 'selected="selected"':'';?> value="near">Near</option>
													<option <?php echo in_array('far',$proximityArr) ? 'selected="selected"':'';?> value="far">Far</option>
													<option <?php echo in_array('unknown',$proximityArr) ? 'selected="selected"':'';?> value="unknown">Unknown</option>
												</select>
											</div> -->
											<div class="form-group">
												<label for="type"  class="form-label">Beacon Type</label>
												<select class="form-control" name="type" id="type" onchange="changetype()"  data-error-msg="Please select type">
													<option <?php echo 'iBeacon'== $type ? 'selected="selected"':'';?> value="iBeacon">iBeacon</option>
													<option <?php echo 'EddyStone'== $type ? 'selected="selected"':'';?> value="EddyStone">EddyStone</option>	
												</select>
											</div>
											<div class="form-group iBeacon" <?php echo $ibeacond;?>>
												<label for="PROXIMITY_UUID"  class="form-label">Proximity UUID</label>
												<input type="text" class="form-control"  id="PROXIMITY_UUID" name="PROXIMITY_UUID" value="<?php echo $PROXIMITY_UUID; ?>" placeholder="Enter beacon device PROXIMITY UUID" data-error-msg="Please enter Beacon device PROXIMITY UUID"/>
											</div>

											<div class="form-group iBeacon" <?php echo $ibeacond;?>>
												<label for="major"  class="form-label">Major</label>
												<input type="text" class="form-control"  id="major" name="major" value="<?php echo $major; ?>" placeholder="Enter beacon device major" data-error-msg="Please enter Beacon device major"/>
											</div>

											<div class="form-group iBeacon" <?php echo $ibeacond;?>>
												<label for="minor"  class="form-label">Minor</label>
												<input type="text" class="form-control"  id="minor" name="minor" value="<?php echo $minor; ?>" placeholder="Enter beacon device minor" data-error-msg="Please enter Beacon device minor"/>
											</div>
											
                                            <div class="form-group EddyStone" <?php echo $eddy;?>>
												<label for="major"  class="form-label">Namespace</label>
												<input type="text" class="form-control"  id="name_space_id" value="<?php echo $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" <?php echo $eddy;?>>
												<label for="minor"  class="form-label">Instance</label>
												<input type="text" class="form-control"  id="instance_id" value="<?php echo $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" value="<?php echo $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 changetype(){
   			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
	        });
		});
</script>
</body>
</html>