<?php 
include("../connect.php");
include("../session.php");

$for = $_REQUEST['a'];
$title = "";
$fromDate = "";
$toDate = "";
$id = $_REQUEST['id'];
if ($id == "00") {
	$name = "All";
	$user = "%";
}else {
	$admin = new Administrator($id);
	$name = $admin->Name;
	$user = $admin->Username;
}
$title = " - For: " . $name;
$fromDate = $_REQUEST['fromDate'];
$toDate = $_REQUEST['toDate'];

 ?>
 
 <!DOCTYPE html>
 <html lang="en">
   <head>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <title><?= $config['CompanyName'] ?></title>
 
     <!-- Bootstrap -->
     <link href="../css/bootstrap.min.css" rel="stylesheet">
     <link rel="stylesheet" href="../css/bootstrap-combobox.css" />
 
     <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
     <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
     <!--[if lt IE 9]>
       <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
       <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
       <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
           <!-- Include all compiled plugins (below), or include individual files as needed -->
           <script src="../js/bootstrap.min.js"></script>
       	 <script src="../js/bootstrap-combobox.js"></script>
       	 <script src="../js/excellentexport.js"></script>
           <script type="text/javascript">
             $(document).ready(function(){
               $('.combobox').combobox();
       //        $('.grpline').css("width", "100px");
             });
           </script>

     <![endif]-->
	<link rel="stylesheet" href="../css/styles.css"/>
   </head>
   <body>
   
       <?= include("navbar.php"); ?>     

   
   <!-- Stack the columns on mobile by making one full-width and the other half-width -->
<div class="container " style="padding-top: 10px;">
	<div class="panel panel-default">
	  <div class="panel-heading form-inline">
	  <a class="btn btn-success" role="button" style="float: right;" href="#" download="Log<?= $title . "(" . date("m.d.Y",strtotime($fromDate)) . " - " . date("m.d.Y",strtotime($toDate)) . ")" ?>.xls" href="#" onclick="return ExcellentExport.excel(this, 'eventlog', 'Event Log');" data-toggle="tooltip" data-placement="left" title="Does not work with Internet Explorer">Export To Excel</a>
	  
	  	<div style="margin-right: 10px; float: right;">
	  		<!--
	  			<div class="input-group">
	  				<div id="clearButton" style="display: none;" class="input-group-addon" onclick="clearFilter()">
	  			      	<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
	  			    </div>
	  			    <input type="text" class="form-control" name="filter" placeholder="Order ID" value="" />
	  			    <div class="input-group-addon" onclick="filter()">
	  			    	<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
	  			    </div>
	  			 </div>
	  			 -->
	  	</div>
	  	
	  		    <h3 class="panel-title">Event Log<?= $title . " - <i>From: " . date("m/d/Y",strtotime($fromDate)) . " To: " . date("m/d/Y",strtotime($toDate)) . "</i>"?></h3><br />
	    
	  </div>
	  <div class="panel-body">
	  	<table class="table table-hover table-condensed" id="eventlog">
	  		<thead>
	  			<tr>
	  				<th>Date/Time</th>
	  				<th>User</th>
	  				<th>Action</th>
	  			</tr>
	  			
	  		</thead>
	    <?php 
	    
	    $query = "SELECT * FROM EVENT_LOG WHERE DATE(`DATE_TIME`) BETWEEN '$fromDate' AND '$toDate' AND `USER` LIKE '$user' ORDER BY `DATE_TIME` DESC";
	   	$result = mysqli_query($con, $query);
	   	$total = mysqli_num_rows($result);
	   	
	   	while ($row = mysqli_fetch_array($result)) {
	   		$rowUser = new Administrator($row['USER']);
	   	?>
	   		<tr style="cursor: pointer;"  onclick="getEventDetails(<?= $row['ID'] ?>)">
	   			<td><?= date("m/d/Y g:i a",strtotime($row['DATE_TIME']) - 4*3600) ?></td>
	   			<td><?= $rowUser->Name ?></td>
				<td><?= $row['ACTION'] ?></td>	   			
	   		</tr>
	   	<?php
	   	}
	     ?>
	     </table>
	  </div>
	  <div class="panel-footer" >
	  	<span  style="float: right; text-align: right;">
	  		<strong>Total Entries: </strong><i><?= ($total) ?></i>
	  		
	  	</span>	
	  	<br />
	  </div>
	</div>

</div>       

<div class="modal fade" id="eventDetail" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="eventDetailTitle">Event Detail</h4>
      </div>
      <div class="modal-body">
        <form>
          <div class="form-group">
            <label for="eventDetailDate">Date/Time</label>
            <input type="text" class="form-control" readonly id="eventDetailDate" placeholder="Data Not Available">
          </div>
          <div class="form-group">
            <label for="eventDetailUser">User</label>
            <input type="text" class="form-control" readonly id="eventDetailUser" placeholder="Data Not Available">
          </div>
          <div class="form-group">
            <label for="eventDetailAction">Action</label>
            <input type="text" class="form-control" readonly id="eventDetailAction" placeholder="Data Not Available">
          </div>
          <div class="form-group">
            <label for="eventDetailDetails">Details</label>
            <textarea readonly class="form-control" id="eventDetailDetails" rows="10"></textarea>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">

$('[name=filter]').on('keypress', function(e) {
	if (e.which == 13) {
		filter();
	}

});
function clearFilter() {
	$('[name=filter]').val("");
	$('#clearButton').css('display', 'none');
	$('tr').css('display', '');
	
}
function filter() {
	var	name = $('[name=filter]').val();
	filterByName(name);
}
function filterByName(name) {
	$('#clearButton').css('display', '');
	for (var i = 0; i < $('td').length; i=i+6) {
	
		var text = $($('td')[i]).text();
		
		var result = text.toUpperCase().indexOf(name.toUpperCase());
		if (text != name) {
			$($('tr')[i/6+1]).css('display', 'none');
		}else {
			$($('tr')[i/6+1]).css('display', '');
		}
		
	}
}
function editCustomer(AccountNumber) {
	clearModal();
	$.get("functions.php?action=getCustomerInfo&customer=" + AccountNumber, function(data){
	 	var customer;
		customer = JSON.parse(data);
		var lastLogin = customer.LastLogin;
		if (lastLogin == null) {
			lastLogin = "Never";
		}else{
			lastLogin = new Date(lastLogin);
			lastLogin = lastLogin.format("M j, Y g:i a");
		}
		$('#isNew').val("0");
		$('#AddEditTitle').text("Edit Customer");
		$('#CustomerAccount').val(customer.AccountNumber);
		$('#CustomerName').val(customer.Name);
		$('#CustomerCity').val(customer.City);
		$('#CustomerEmail').val(customer.Email);
		$('#CustomerPassword').val("NOCHANGE");
		$('#CustomerSalesman').val(customer.Salesman.ID);
		$('#active').prop('checked', parseInt(customer.Active));
		$('#LastLogin').text(lastLogin);
		$('#AddEditCustomer').modal()
		$('#deleteButton').css('display', "");
		
	});
}
function clearModal() {
	$('#eventDetailDate').val('');
	$('#eventDetailUser').val('');
	$('#eventDetailAction').val('');
	$('#eventDetailDetails').val('');
}
function getEventDetails(id) {
	clearModal();
	$.get("functions.php?action=getEventDetails&id=" + id, function(data){
	 	var LogEvent;
		LogEvent = JSON.parse(data);
		console.log(LogEvent);
		var EventDate = LogEvent.DATE_TIME;
		EventDate = new Date(EventDate);
		EventDate.addHours(-4);
		//EventDate = EventDate.format("M j, Y g:i a");
		console.log(LogEvent);
		$('#eventDetailDate').val(EventDate);
		$('#eventDetailUser').val(LogEvent.USER);
		$('#eventDetailAction').val(LogEvent.ACTION);
		$('#eventDetailDetails').val(LogEvent.DATA);		
	});
	$('#eventDetail').modal('toggle');
}
Date.prototype.addHours= function(h){
    this.setHours(this.getHours()+h);
    return this;
}
</script>

   </body>
   
 </html>