<?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, '/');
?>

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

<head>

	<title>Assign Beacon to Ad Campaign</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">
							Assign Beacon to Ad Campaign
        					<a href="manage-ad.php" class="btn btn-danger pull-right">Go Back</a>
						 </h1>
						<div class="panel panel-default">
						  <div class="panel-heading">Assign Beacon</div>
						  <div class="panel-body">
							<?php
								if(isset($_POST["btnAddZone"])){
									$flag = false;
									try{
										$qury = new ParseQuery("ads");
										$ad = $qury->get($_POST['ad_id']);
										$objBeaconsForRemoval = new ParseQuery("ad_beacon");
										$objBeaconsForRemoval->equalTo('ad',$ad);
										$objBeaconsResult = $objBeaconsForRemoval->find();
										if(!empty($objBeaconsResult)){
											foreach($objBeaconsResult as $oldBecn){
												$oldBecn->destroy();
											}
										}
										if(!empty($_POST['beacon'])){					
											foreach($_POST['beacon'] as $becn){
												$objBeacons = new ParseQuery("beacon");				
												$beacon = $objBeacons->get($becn);					
												$objAB = ParseObject::create("ad_beacon");
												$objAB->set("beacon",$beacon);
												$objAB->set('ad',$ad);
												$objAB->save();					
											}
										}
										$flag = true;
									} catch (ParseException $ex) {  
									  // Execute any logic that should take place if the save fails.
									  // error is a ParseException object with an error code and message.
									  echo 'Failed to create new object, with error message: ' . $ex->getMessage();
									}			
									if($flag){		
									?>
										 <script>
										
											
											//alertify.alert('Successfully saved', 'New Zone has been added successfully.!', function(){
												alertify.success('Saved'); 
											//	window.location = window.history.back();
											//});
										</script>
								<?php
									}
								 } 
								$qury = new ParseQuery("ads");
								$ads = $qury->get($_GET['uTp']);
								
								
								$objBeacons1 = new ParseQuery("beacon");
								//$objBeacons1 = $objBeacons1->limit(99999)					
								$result1 = $objBeacons1->find();

								$qury = new ParseQuery("ads");
								$ad = $qury->get($_GET['uTp']);
								$objBeaconsForRemoval = new ParseQuery("ad_beacon");
								$objBeaconsForRemoval->equalTo('ad',$ad);
								$objBeaconsResult = $objBeaconsForRemoval->find();
								$selectedBeacons = array();
								if(!empty($objBeaconsResult)){
									foreach($objBeaconsResult as $oldBecn){
										$id = $oldBecn->get('beacon')->getObjectId();
										$selectedBeacons[$id] = $id;
									}
								}

								 ?>
								
								<div class="row">
									<div class="col-md-6">
										<form method="post" >
											
											<div class="form-group">
												<div class="form-group">
													<label for="toZone" class="form-label">Campaign Title</label>
													<input type="hidden" name="ad_id" value="<?php  echo $_GET['uTp'];?>">
													<span><?php echo $ads->get('title'); ?></span>
												</div>
											
                                                <div class="form-group">
													<label for="toZone" class="form-label">Select Beacon</label>
													<select class="form-control " name="beacon[]" id="toZone" data-error-msg="Please select beacon" multiple>
														<?php
															foreach($result1 as $key){
																$getObjectId = $key->getObjectId();
																$selected = '';
																if(in_array($getObjectId,$selectedBeacons)){
																	$selected = "selected='selected'";
																}
															 ?>
															<option value ="<?php echo $getObjectId ;?>" <?php echo $selected; ?>><?php echo $key->get('unique_id'); ?></option>
														<?php } ?>
													</select>
												</div>            
                                                <div class="form-group">
                                                    <input type="submit"  class="btn btn-success" name="btnAddZone" value="Assign Now" />
                                                    
                                                </div>
											</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://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">
      $(document).ready(function(){
      	$('select[multiple]').multiselect({
            columns: 2,
            placeholder: 'Select States',
            search: true,
            searchOptions: {
                'default': 'Search States'
            },
            selectAll: true
        });
        $('#addNewZoness').attrvalidate();
        $('#resetBtn').click(function(){ 
			$('#addNewZone').attrvalidate('reset'); 
		});
      });
    </script>

</body>

</html>
