<?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') ?>

	<input type="hidden" id="identifier">
	<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" id="heading"> Loading... </h3>
                </div>
                <div class="pull-right">
                	
                	<a href="newpack.php" style="float:right;margin-left: 5px" class="btn btn-success"><i class="fa fa-plus-square" aria-hidden="true"></i> Create New</a>
                	<a href="stickerpack.php" style="float:right;" class="btn btn-success"><i class="fa fa-chevron-left" aria-hidden="true"></i> List</a>
                </div>
                </div>
            </div>

            <div class="flex flex-wrap" id="">
                <div class="w-full col-md-12 p-3">
                	<div class="card" id="stickerpacktable">Loading...</div>
                	<br>
                	<div class="card text-right">
                		<div class="card-body">

                			<a href="javascript:;" onclick="SendNotification()"  class="btn btn-warning"><i class="fa fa-bell" aria-hidden="true"></i> Notify About this Pack </a>

                			<a href="javascript:;" onclick="GotoEdit()"  class="btn btn-warning"><i class="fa fa-pen" aria-hidden="true"></i> Edit Pack </a>

                			<a href="javascript:;" onclick="AskConfirm()"  class="btn btn-danger"><i class="fa fa-trash" aria-hidden="true"></i> Delete Pack </a>
                		</div>
                	</div>
                </div>
                
            </div>
        </div>
</div>

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


<script id="table-template" type="text/x-handlebars-template">
	<table class="table table-bordered">
		<tr>
				<td style="width:220px">
					Publisher
				</td>
				<td>
					{{publisher}}
				</td>
		</tr>
		<tr>
				<td>
					Signal Sticker URL
				</td>
				<td>
					<a href="{{SignalStickerPackURL}}"> Click Here </a>
				</td>
		</tr>
		
	</table>

	<div class="stickerarea">
	
		{{#if IsAnimated}}
			{{#stickers}}
				<div class="card" style="width: 120px;margin:5px;float:left;">
					<img src="apis/userdata/{{../identifier}}/{{image_file}}.png">
				</div>
			{{/stickers}}
		{{else}}
			{{#stickers}}
				<div class="card" style="width: 120px;margin:5px;float:left;">
					<img src="apis/userdata/{{../identifier}}/{{image_file}}.webp">
				</div>
			{{/stickers}}
		{{/if}}
		
	</div>
</script>

<script type="text/javascript">
	$(document).ready(function(){
		_readData();
	});


	function GotoEdit()
	{
		window.location.href="editpack.php?id=" + $('#identifier').val() ;
	}

var stickerPack=null;
	function _readData()
	{
		$.ajax({
			url:'apis/alldata.php',
			success:function(data){
				var _stickerData=JSON.parse(data);
				
				
				for(var i=0;i< _stickerData.sticker_packs.length;i++)
				{
					if( parseInt(_stickerData.sticker_packs[i].identifier)== parseInt(gup('id')) )
					{
						stickerPack=_stickerData.sticker_packs[i];
					}	    
				}

				var source = $("#table-template").html(); 
				var template = Handlebars.compile(source); 

				$('#heading').html('<small>(' + stickerPack.identifier +')</small> ' + stickerPack.name + '')
				$('#identifier').val( stickerPack.identifier );

				$('#stickerpacktable').html(template(stickerPack));

				if(gup('remove')=="true")
				{
					AskConfirm();
				}

			}
		});
	}


	function SendNotification()
	{
		bootbox.confirm({
	    message: "Do you Want to Send Notification to all Users Now ?",
	    buttons: {
	        confirm: {
	            label: 'Yes',
	            className: 'btn-success'
	        },
	        cancel: {
	            label: 'No',
	            className: 'btn-danger'
	        }
	    },
	    callback: function (result) {
	        if(result==true)
	        {
	        	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({"to": "/topics/toAll", "notification": {"title":  stickerPack.name ,"body": stickerPack.name +" Stickers Arrived. Click here to Check !"},"data":{"STICKERID":stickerPack.identifier}}),
		            success : function(response) {
		                console.log(response);
		                HoldOn.close();
		                ShowSuccess("Notification Sent");
		            },
		            error : function(xhr, status, error) {
		                console.log(xhr.error);                   
		            }
		        });
	        }
	    }
		});
	}


	function AskConfirm()
	{

		bootbox.confirm({
	    message: "Do you really Want to delete this sticker pack ?",
	    buttons: {
	        confirm: {
	            label: 'Yes',
	            className: 'btn-success'
	        },
	        cancel: {
	            label: 'No',
	            className: 'btn-danger'
	        }
	    },
	    callback: function (result) {
	        //console.log('This was logged in the callback: ' + result);
	        if(result==true)
	        {
	        	_deletePack();
	        }
	    }
		});
	}

	function _deletePack()
	{
		$.ajax({
			url:'apis/removePack.php?PACKID=' + gup('id'),
			method:'get',
			success:function()
			{
				window.location.href="stickerpack.php";
			}
		});
	}

</script>

