<?php
    class Banner {
        public $db;

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

        public function getBanners($data = array()) {
            $banners = array();
            $query = "SELECT * FROM {$this->db->prefix}banners WHERE 1";
            
            if(isset($data['status'])){
                if($data['status'] != ''){
                    $query .= " AND status =".$data['status'];
                }
            }

            if(isset($data['search_keywords']) && !empty($data['search_keywords'])){
                $keywords = explode(' ', $data['search_keywords']);
                $searchTermKeywords = array();
                foreach ($keywords as $word) {
                    $word                 = $this->db->escape($word);
                    $searchTermKeywords[] = "(title LIKE '%$word%' OR link LIKE '%$word%')";
                }
                $query .= ' AND '.implode(' AND ', $searchTermKeywords);
            }
            
            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){ 
                    $status = $row['status'];
                    if($status == 1) {
                        $status = '<span class="badge bg-success">Aktif</span>';
                    }else if($status == 0) {
                        $status = '<span class="badge bg-danger">Pasif</span>';
                    }else {
                        $status = 'Bilinmiyor';
                    }
                    $item = array(
                        'id'                     => $row['id'],
                        'status'                 => $status,
                        'image'                  => $row['image'],
                        'title'                  => $row['title'],
                        'order'                  => $row['order'],
                        'link'                   => $row['link'],
                        'pos'                    => $row['pos']
                    );
                    array_push($banners, $item);
                }
            }

            return $banners;
        }

        public function getTotalBanners($data = array()){
            $query = "SELECT COUNT(id) AS 'total' FROM {$this->db->prefix}banners WHERE 1";
            
            if(isset($data['status'])){
                if($data['status'] != ''){
                    $query .= " AND status =".$data['status'];
                }
            }

            if(isset($data['search_keywords']) && !empty($data['search_keywords'])){
                $keywords = explode(' ', $data['search_keywords']);
                $searchTermKeywords = array();
                foreach ($keywords as $word) {
                    $word                 = $this->db->escape($word);
                    $searchTermKeywords[] = "(title LIKE '%$word%' OR link LIKE '%$word%')";
                }
                $query .= ' AND '.implode(' AND ', $searchTermKeywords);
            }
            
            $stmt = $this->db->query($query);
            if($stmt->num_rows > 0){
                return $stmt->row['total'];
            }

            return 0;
        }

        public function getBanner($id) {
            $query = "SELECT * FROM {$this->db->prefix}banners WHERE id=:id";
            $stmt = $this->db->query($query, array(':id'=>$id));
            if($stmt->num_rows > 0) {
                return $stmt->row;
            }

            return array();
        }

        public function deleteBanner($id) {
            $this->db->query("DELETE FROM {$this->db->prefix}banners WHERE id=:id", array(':id'=>$id));
        }
    }
?>