<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
global $configverssion_id; global $configapp_version; global $userID; global $server; global $LANG; $resellerID = getReseller();
//=== List page stuffs =============
$page = 1; $setLimit = 30;
if(isset($_GET["page"])) $page = (int)$_GET["page"];
$pageLimit = ($page * $setLimit) - $setLimit; 
$pageurl=$_SERVER['REQUEST_URI']."?";
if(count($_GET)) { $pageurl = $_SERVER['REQUEST_URI'].'&'; }
$trans = array("s=d_o" => "", "s=d_n" => "", "s=r_h" => "", "s=r_l" => "", "s=s_r" => "", "s=s_u" => "");
$pageurl = strtr($pageurl,$trans);
$pageurl = str_replace('&&','&',$pageurl);
//====End ==============
define('TITLE','Activities');
if(userRole()>2) {
	header('location: index.php');	
}
$today = date('Y-m-d'); $time = strtotime($today.'-1 months'); $lastMonth = date("Y-m", $time); $time = strtotime($today.'-1 years'); $lastYear = date("Y", $time);
?>
<?php

	$added = '';	
	if(isset($_GET['sc'])) {
		$keywords = mysqli_real_escape_string($server,$_GET['sc']);
		$ser = explode(' ', $keywords);
		$clauses = array();
		foreach($ser as $look){
			$term = trim(preg_replace('/[^a-z0-9]/i', '', $look));   
			if (!empty($term)){
				 $clauses[] = "event LIKE '%$term%' OR ip LIKE '%$term%'  OR date LIKE '%$term%'";
			} else {
				 $clauses[] = " id LIKE '%%' ";
			}
		}
		$added .= ' AND ('.implode(' OR ', $clauses).') ';		
	}
	if(isset($_GET['s'])) {
		switch($_GET['s']) {
			case 'd_o': $added .= " order by date ASC ";
			break;
			case 'd_n': $added .= " order by date DESC ";
			break;
		}
	} else {
		$added .= " order by date DESC ";	
	}

    $query="SELECT * FROM logs WHERE 1 $added limit $pageLimit,$setLimit";
	$result = mysqli_query($server,$query) or die(mysqli_error($server));
	$x = mysqli_num_rows($result);
 ?>
<div class="row no-m-t no-m-b">
 	<div class="col s12 m12 l12">
        <div class="card card-transparent">
            <div class="card-content">
            	<div class="col s12 m4 l4">
                </div>
                <div class="col s12 m8 l8">
                  <div class="col s12 m12 l12">
                    <div class="row no-m-t no-m-b">
                     <form action="alog" method="get">
                     <div class="input-field col s12">
                        <i class="material-icons prefix">search</i>
                        <input type="text" name="sc" value="<?=@$_GET['sc']?>" placeholder="<?=$LANG['Search...']?>">
                     </div>
                     </form>  
                    </div>
                  </div> 
                </div>
            </div>
        </div>
    </div>
 </div>
 
	<div class="row no-m-t no-m-b">
       <div class="col s12 m12 l12">
         <div class="card card-white">
            <div class="card-content">
            <div class="card-options">
            	<ul>
                	<li><a class='dropdown-button btn btn-flat' href='#' data-activates='filter' data-beloworigin='true' data-constrainwidth='false'><i class="fa fa-sort"></i> <?=$LANG['Sort']?></a></li>
                </ul>    
                <ul id='filter' class='dropdown-content'>
                    <li><a href="<?=$pageurl?>s=d_o" ><?=$LANG['Oldest First']?></a></li>
                    <li><a href="<?=$pageurl?>s=d_n" ><?=$LANG['Newest First']?></a></li>
                </ul>
            </div>
            <span class="card-title"></span>
       		<table class="highlight responsive-table">
                <thead>
                	<tr>
                        <th data-field="to"><?=$LANG['Date']?></th>
                        <th data-field="to"><?=$LANG['User']?></th>
                        <th data-field="from"><?=$LANG['IP']?></th>
                        <th data-field="to"><?=$LANG['Event']?></th>
                    </tr>
                 </thead>
                 <tbody>
                    <form action="log" method="post" id="inbox-form">
<?php $xf=0; while ($rows = mysqli_fetch_assoc($result)) { $xf+=1; 
$customer = '<a href="acustomer?v='.$rows['user_id'].'">'.userData($rows['user_id'],'first_name').' '.userData($rows['user_id'],'last_name').'</a>';
if($rows['user_id']==0) $customer = 'System';
?>
						<tr class="">
                             <td><?=date('M j Y h:iA',$rows['date'])?></td>
                             <td><?=$customer?></td>
                             <td><a target="_blank" href="http://www.geoiptool.com/en/?IP=<?=$rows['ip']?>"><?=$rows['ip']?></a></td>
                             <td><?=$rows['event']?></td>
                        </tr>
<?php } ?>
<?php if($xf<1) echo '<tr class="orange lighten-5"><td colspan=10><h5 class="center-align">'.$LANG['No records found!'].'</h5></td></tr>' ?>	
					</form>
                 </tbody>
            </table>  
          </div>
        </div>          
     </div>
  </div>
 <div class="row">
 	<div class="col s12 m12 l12" style="margin-top: 5px;">
        <?php displayPagination($setLimit,$page,$query);  ?>
    </div>
 </div>                       
