<?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';
	
	$objBuildings = new ParseQuery("ads");
					
	$objBuildings->descending('createdAt');
	$result = $objBuildings->find();
	$objPaging = new Paging($result, 50);
	$rows = $objPaging->getRecords();
	$objPaging->url = $objPaging->url;
?>

<!DOCTYPE html>
<html lang="en">
<head>
	<title>Manage Ad Campaigns</title>
	<?php include 'includes/head.php'; ?>
    <style>
    	.glyphicon-signal{
    		background-color: #f8e409;
    	}
    </style>
</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">
                            Manage Ad Campaings
							<a href="add-ad.php" class="btn btn-success pull-right">Create New Ad Campaign</a>
                        </h1>
                        <div class="table-responsive">
                            <table id="listBeacons" class="table table-bordered table-hover table-striped">
                                <thead>
                                    <tr>
                                        <th>Campaign Title</th>
                                        <th>Description</th>
                                        <th>Image</th>
										<th>Start Date</th>
										<th>End Date</th>
        								<th>Beacon</th>
										<th class="text-center actionWidth" >Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    foreach($rows as $d){ 
                                    	$getObjectId = $d->getObjectId(); 
                                    	$startDate = date('m/d Y H:i',strtotime($d->get('start_date')));
                                    	$endDate = date('m/d Y H:i',strtotime($d->get('end_date')));
                                    	$pics = explode(',',$d->get('pic'));
                                    	$url = url();                                    
                                    	$objBeaconsForRemoval = new ParseQuery("ad_beacon");
                                    	$objBeaconsForRemoval->equalTo('ad',$d);
                                		$objBeaconsForRemoval->find();
                                		$objBeaconsResult = $objBeaconsForRemoval->find(); 
                                    	$selectedBeacons = array();
                                		if(!empty($objBeaconsResult)){
                                    		foreach($objBeaconsResult as $oldBecn){                                            	
                                        		$id = $oldBecn->get('beacon')->getObjectId();                                            
                                            	$qury = new ParseQuery("beacon");
                                            	$beacon = $qury->get($id);
                                            	$selectedBeacons[$beacon->get('unique_id')]= $beacon->get('unique_id');
                                    		}
                                		}
                                    
										?>
                                   			<tr>
                                   				<td><?php echo $d->get('title');?></td>
                                   				<td><?php echo strip_tags($d->get('details'));?></td>
                                   				<td>
                                   					<?php foreach($pics as $i=>$pic){?>
                                   							<a target="_blank" href="<?php echo  $url.'/uploads/'.$pic; ?>">View</a>
                                   					<?php } ?>
                                   				</td>
                                   				<td><?php echo $startDate;?></td>
                                   				<td><?php echo $endDate;?></td>
                                                <td><?php echo implode(',',$selectedBeacons);?></td>
                                   				<td align="center" class="actionButtons" width="15%">
													<a data-toggle="tooltip" title="Assign Beacon to Ad"  href="assign-beacon.php?uTp=<?php echo $getObjectId; ?>"><i class="glyphicon glyphicon-signal"></i></a>
													<a data-toggle="tooltip" title="View"  href="view-ad.php?uTp=<?php echo $getObjectId; ?>"><i class="glyphicon glyphicon-eye-open"></i></a>
													<a data-toggle="tooltip" title="Edit"  href="edit-ad.php?uTp=<?php echo $getObjectId; ?>"><i class="glyphicon glyphicon-pencil"></i></a>

													<a data-toggle="tooltip" title="Delete" onclick="return confirm('Are you sure you want to delete this ad?');" href="delete-ad.php?uTp=<?php echo $getObjectId; ?>"><i class="glyphicon glyphicon-trash"></i></a>
													</td>
                                   			</tr>
                                   <?php  } ?>
                                </tbody>
                            </table>
							<?php  echo $objPaging->getPaging(); ?>
                        </div>
                    </div>
                </div>
                <!-- /.row -->
            </div>
            <!-- /.container-fluid -->

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

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

   <?php include 'includes/footer.php'; ?>
   
   <script>
		$(function() {
			$(".deleteObject").click(function(){
				element = $(this);
				var del_id = element.attr("id");
				
				alertify.confirm('Deleing Client', 'Are you sure!.<br>This client is may be assoicated with buildings.', function(){
					$.ajax({
					type:'POST',
					url:'delete.php',
					data:{'objClient':del_id},
					success: function(data){
						 if(data=="deteted"){
							alertify.success('Deleted.');
						 }else{
							//alertify.error('Cancel');
							console.log(data);
						 }
					 }

					});
				},
				function(){
					//alertify.error('Cancel');
				});
			});
		});
   </script>

</body>

</html>
