<?php
	require 'autoload.php';
	require 'PushNotifications.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>Create New Notification</title>
	<?php include 'includes/head.php'; ?>	    
</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">
                            Create New Notification
							<a href="manage-notifications.php" class="btn btn-danger pull-right">Go Back</a>
                        </h1>
							<?php
								if ( isset($_POST["btnAddAd"]) ) {
									$obj = ParseObject::create( "notifications" );
									$obj->set( "text", $_POST["text"] );
                                	$obj->set("text",$_POST['title']);
									try {
										$obj->save();
                                    	$id = $obj->getObjectId();
                                    	$pushObj = new PushNotifications();
                                    	$pushObj->android(array('mtitle'=>$_POST['title'],'mdesc'=>$_POST["text"],'id'=>$id));                                    
											?>
											<script>
												alertify.alert('Successfully saved', 'New notification has been added successfully.!', function(){									
													window.location = "manage-notifications.php";
												});
											</script>							
									<?php	}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 notification, with error message: ' . $ex->getMessage();
										}
							
							 } 
							?>
						
						<div class="panel panel-default">
						  <div class="panel-heading">Notification Details</div>
						  <div class="panel-body">
								<div class="row">
									<div class="col-md-12">
										<form method="post" id="addNewad" enctype="multipart/form-data" >
											<div class="form-group">
												<label for="tDetails"  class="form-label">Title</label>
												<input type="text" name="title" class="form-control" required="" placeholder="Enter notification title">
											</div>
                            				<div class="form-group">
												<label for="tDetails"  class="form-label">Details</label>
												<textarea class="form-control" onkeyup="calcwords(this)" rows="6" id="text" name="text" required="" placeholder="Enter notification details upto 255 characters"></textarea>
                                                <span style="color:red;display:none" id="error">Character limit is 255 max.</span>
											</div>
											<div class="form-group">
												<input type="submit" id="submt" class="btn btn-success" name="btnAddAd" value="Broadcast & Save" />
												<input type="reset" class="btn btn-info" id="resetBtn" />
											</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>
 	<link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.11/summernote.css" rel="stylesheet">
	<script src="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.11/summernote.js"></script>

   	<script type="text/javascript"> 
   		function calcwords(elem){      		
   			$('#error').hide();
   			$('#submt').removeAttr("disabled");
       		if($(elem).val().length > 255){
            	$('#error').show();
            	$("#submt").attr("disabled", true);            	
            }
    	}
		

      $(document).ready(function(){
   		
   		/*$('#text').summernote({
        	toolbar: [
              ['style', ['style']],
              ['font', ['bold', 'underline', 'clear']],
              ['fontsize', ['fontsize']],
              ['fontname', ['fontname']],
              ['color', ['color']],
              ['para', ['ul', 'ol', 'paragraph']],
              ['table', ['table']],              
              ['view', ['fullscreen', 'codeview']],
              ['insert', ['link', 'picture', 'video']],
          ],
		tooltip:false, 
		});*/
      });
    </script>
</body>
</html>