<?php

if(isset($_POST['btnLogout']))
{
	session_start();
	session_destroy();
} 
include('rights.php');
include('config.php');
include('includes/_admin_top.php');
?>

<div class="flex flex-col md:flex-row">
	<?php include('includes/_admin_menus.php') ?>
	<div class="main-content flex-1 bg-gray-100 md:mt-2 pb-24 md:pb-5">

		<div class="bg-blue-800 p-2 shadow text-xl text-white">
                <div class="d-table">
                	<div class="pull-left">
                	<h3 class="font-bold pl-2" > Notification </h3>
                </div>
                <div class="pull-right">

                </div>
                </div>
        </div>

        <div class="flex flex-wrap" id="stickerpackstable">
        	<div class="w-full col-md-12 p-3">
        		<div class="card">
        			<div class="card-body">
        				<div class="form-group">
								<label for="exampleInputEmail1">Notification Title</label>
								<input type="text" class="form-control" id="NotificationTitle" name="NotificationTitle" placeholder="Big Emoji">
						</div>

						<div class="form-group">
								<label for="exampleInputEmail1">Notification Body</label>
								<input type="text" class="form-control" id="NotificationBody" name="NotificationBody" placeholder="Big Emoji stickers are arrived ">
						</div>

						<div class="form-group">
								<label for="exampleInputEmail1">Sticker Pack Id <small>(optional)</small> </label>
								<input type="text" class="form-control" id="StickerPackId" name="StickerPackId" placeholder="123">
						</div>

					

						<div class="d-table">
							<button class="btn btn-success btn-sm pull-right" onclick="SendNotification()"> Send </button>
						</div>

        			</div>
        		</div>
        	</div>
        </div>

	</div>

</div>

<?php  include('includes/_admin_bottom.php')?>

<script type="text/javascript">

	
	
	function validate()
	{
		if($('#NotificationTitle').val()=="")
		{
			ShowError("Please enter notification title");
			return false;
		}
		if($('#NotificationBody').val()=="")
		{
			ShowError("Please enter notification Body");
			return false;
		}
		return true;
	}

	function GetFotmatedDateTime()
	{
		var Idate=new Date($('#Schedule').val());
		return Idate.getFullYear() +'-'+ parseInt( Idate.getMonth() +1) +'-' + Idate.getDate() + ' ' + Idate.getHours() + ':' + Idate.getMinutes() + ':' + Idate.getSeconds();
	}

	function SendNotification()
	{
		if(validate()==true)
		{

			var strim={
					"to": "/topics/toAll", 
					"notification": 
						{	"title":   $('#NotificationTitle').val() ,
							"body":  $('#NotificationBody').val() 
							//"data":{"STICKERID":"305"
						},
						"data":{}
					};

			if($('#StickerPackId').val() !="")
			{
				strim.data={"STICKERID": ""+ $('#StickerPackId').val() +""};
			}
			

			HoldOn.open()

			$.ajax({        
		            type : 'POST',
		            url : "https://fcm.googleapis.com/fcm/send",
		            headers : {
		                Authorization : 'key=<?php echo $__FCM_SERVER_KEY ?>'
		            },
		            contentType : 'application/json',
		            dataType: 'json',
		            data: JSON.stringify(strim),
		            success : function(response) {
		                console.log(response);
		                HoldOn.close();
		                $('#NotificationTitle').val('');
		                $('#NotificationBody').val('');
		                $('#StickerPackId').val('');
		                ShowSuccess("Notification Sent");
		            },
		            error : function(xhr, status, error) {
		                console.log(xhr.error);                   
		                HoldOn.close();
		            }
		        });
		}
	}

</script>