<?php
    class SystemLog {

        public function __construct(Database $db){
            $this->db = $db;
        }

        public function getLogs($data = array()) {
            $rows = array();
            $query = "SELECT * FROM {$this->db->prefix}logs WHERE 1";

            if(isset($data['search_keywords']) && $data['search_keywords'] != ""){
                $keywords = explode(' ', $data['search_keywords']);
                $searchTermKeywords = array();
                foreach ($keywords as $word) {
                    $word                 = $this->db->escape($word);
                    $searchTermKeywords[] = "(message LIKE '%$word%' OR request_uri LIKE '%$word%' OR type LIKE '%$word%')";
                }
                $query .= ' AND '.implode(' AND ', $searchTermKeywords);
            }

            if(isset($data['type']) && $data['type'] != ""){
                $query .= " AND type='" . $data['type'] . "'";
            }

            if(isset($data['user_id']) && $data['user_id'] != ""){
                $query .= " AND user_id='" . (int)$data['user_id'] . "'";
            }
            
            if(isset($data['sort'])){
                $query .= " ORDER BY " . $data['sort'];
            }else{
                $query .= " ORDER BY id";
            }
    
            if(isset($data['order'])){
                $query .= " ".$data['order'];
            }else{
                $query .= " DESC";
            }
    
            if (isset($data['offset']) || isset($data['limit'])) {
                if($data['offset'] < 0){
                    $data['offset'] = 0;
                }
    
                if($data['limit'] < 1){
                    $data['limit'] = 24;
                }
    
                $query .= " LIMIT " . (int)$data['offset'] . "," . (int)$data['limit'];
            }
            
            $stmt = $this->db->query($query);
            if($stmt->num_rows > 0) {
                foreach ($stmt->rows as $row){
                    $item = array(
                        'id'                     => $row['id'],
                        'type'                   => $row['type'],
                        'message'                => $row['message'],
                        'user_id'                => $row['user_id'],
                        'request_uri'            => $row['request_uri'],
                        'date'                   => $row['date']
                    );
                    array_push($rows, $item);
                }
            }
            return $rows;
        }

        public function getTotalLogs($data = array()) {
            $total_rows = array();
            $query = "SELECT COUNT(id) AS 'total' FROM {$this->db->prefix}logs WHERE 1";

            if(isset($data['search_keywords']) && $data['search_keywords'] != ""){
                $keywords = explode(' ', $data['search_keywords']);
                $searchTermKeywords = array();
                foreach ($keywords as $word) {
                    $word                 = $this->db->escape($word);
                    $searchTermKeywords[] = "(message LIKE '%$word%' OR request_uri LIKE '%$word%' OR type LIKE '%$word%')";
                }
                $query .= ' AND '.implode(' AND ', $searchTermKeywords);
            }

            if(isset($data['type']) && $data['type'] != ""){
                $query .= " AND type='" . $data['type'] . "'";
            }

            if(isset($data['user_id']) && $data['user_id'] != ""){
                $query .= " AND user_id='" . (int)$data['user_id'] . "'";
            }
            
            $stmt = $this->db->query($query);        
            if($stmt->num_rows > 0){
                $total_rows      = $stmt->row['total'];
            }else{
                $total_rows      = 0;
            }

            return $total_rows;
        }

        public function deleteLog($id) {
            $this->db->query("DELETE FROM {$this->db->prefix}logs WHERE id=:id", array(":id"=>$id));
        }
    }