<?php
session_start();
$title = "Message History";
$group = "msgHistory";
$menu = "msgHistory";
include('header.php');

$page = 1;
if(isset($_GET['page']))
{
    $page = $_GET['page'];
}

?>
<script>
function getXMLHTTP() { //fuction to return the xml http object
	var xmlhttp=false;	
	try{
		xmlhttp=new XMLHttpRequest();
	}
	catch(e)	{		
		try{			
			xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e){
			try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e1){
				xmlhttp=false;
			}
		}
	}
	 	
	return xmlhttp;
}


function loadRecord(showloading, gotopage) {
	if(showloading == 1)		
	document.getElementById('load').innerHTML='<img src="img/loading.gif"/>';
	var searchkey = document.getElementById('searchkey').value;	
	var no_records = document.getElementById('norecords').value;
	var lastrecord = document.getElementById('lastrecord').value;
	var page = "<?php echo $page?>";
	if(gotopage != null)
		page = gotopage;  
		
	var strURL="../load/load_messages.php?page="+page+"&lastrecord="+lastrecord+"&searchkey="+searchkey+"&no_records="+no_records;
	//alert(strURL);
	var req = getXMLHTTP();
	
	if (req) {
		
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {	
					//alert(req.responseText);	
					var result = JSON.parse(req.responseText);			
					document.getElementById('load').innerHTML=result.text;
					document.getElementById('pagination').innerHTML=result.pagination;	
					document.getElementById('lastrecord').value	= result.lastrecord;	
					document.getElementById('adminCount').innerHTML	= '<span class="text-danger"><i class="la la-trash"></i> ' + result.admin_count + " Record(s) Found!</span>";				
				} else {
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		}			
		req.open("GET", strURL, true);
		req.send(null);
	}		
}

// function Delete(id)
// {
// 	if(confirm("Delete Admin?"))
// 	{
//     	var datastring = {'id':id,'delete':'admins'};
//     	$.ajax({
//     	            type: "POST",
//     	            url: "../include/delete_ajax.php",
//     	            data: datastring,
//     	            dataType: 'json',
//     	            cache: false,
//     	            success: function(data) {
//     	            	if(data.success == 1)
//     	            	{       
//     	            		loadRecord(0);    		
//     	            		setTimeout(function() {
//     	                        toastr.options = {
//     	                            closeButton: true,
//     	                            progressBar: true,
//     	                            positionClass: "toast-top-full-width",
//     	                            showMethod: 'slideDown',
//     	                            timeOut: 4000
//     	                        };
//     	                        toastr.success('Admin deleted successfully');
    	                        
//     	                    }, 1300);
//     	            	}
//     	            	else
//     	            	{
//     	            		setTimeout(function() {
//     	                        toastr.options = {
//     	                            closeButton: true,
//     	                            progressBar: true,
//     	                            positionClass: "toast-top-full-width",
//     	                            showMethod: 'slideDown',
//     	                            timeOut: 4000
//     	                        };
//     	                        toastr.error('Error');
    
//     	                    }, 1300);
//     	            	}
//     	            },
//     	            error: function(){
//     	                  alert('error handling here');
//     	            }
//     	        });
// 	}
// }

$(document).ready(function(){
	loadRecord(1);
	$('.filter').change(function(){
		loadRecord(1);
	})
});


$(document).ready(function(){
	$('#data_5 .input-daterange').datepicker({
	    keyboardNavigation: false,
	    forceParse: false,
	    autoclose: true
	});
})
</script>
   <style>
.input-daterange input {
    text-align: left;
}
.ft-user-check.text-danger
{
	color: #06D755!important;
	font-size: 18px;
}
</style> 
<input type="hidden" id="lastrecord" value="1"/>
<div class="content-body">
        	<div class="row">
                    <div id="recent-transactions" class="col-12">
                        <div class="card">
                            <div class="card-header">
                                <h4 class="card-title">Message Records</h4>
                            </div>
                            <div class="card-content">
                            	<div class="row row-pad">
                                    <div class="col-md-5">
                                        <input type="text" class="form-control filter" id="searchkey" placeholder="Search Title"/>
                                    </div>
                                    <div class="col-md-2">                                	
                                           <select id="norecords" class="form-control pull-left filter">
                                                <option value="20">20 records</option>
                                                <option value="50">50 records</option>
                                                <option value="100" selected>100 records</option>
                                                <option value="200">200 records</option>
                                            </select>                                    
                                    </div>
                                	<div class="col-lg-5"><a class="btn btn-info pull-right" href="message_template">Schedule Message</a></div>
                            	</div>
                                <div class="row row-pad">
                                <div class="col-lg-12">
                                    <div class="table-responsive" style="margin-top: 20px;">
                                       <table class="table table-hover table-advanced smaller">
                                       		<thead>
                                                <tr>
                                                    <th colspan="7" id="adminCount">Message Records</th>                            
                                                </tr>
                                            </thead>
                                            <thead>
                                                <tr>
                                                    <th>S/N</th>
                                                    <th>Message Type</th>
                                                    <th>Email Subject</th>
                                                    <th>Participant</th>
                                                    <th>Date Scheduled</th>
                                                    <th>Sent Status</th>
                                                    <!-- <th>Actions</th> -->
                                                </tr>
                                            </thead>
                                            <tbody id="load">
                                                    
                                            </tbody>
                                            <tfoot>
                                            <tr>
                                                <td colspan="7" class="footable-visible" id="pagination">
                                                    
                                                </td>
                                            </tr>
                                            </tfoot>
                                       </table>
                                   </div>
                                </div>
                            </div>
                            </div>
                        </div>
                    </div>
                </div>

   </div>    
    
    
    <?php include('footer.php');?>

   
