<?php
    class Faq {
        protected $db;
        protected $utils;
        public function __construct(Database $db, Utils $utils){
            $this->db = $db;
            $this->utils = $utils;
        }

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

        public function getFaqs($data = array()) {
            $posts = array();
            $query = "SELECT f.*, c.title AS 'category_name' FROM {$this->db->prefix}faq f LEFT JOIN {$this->db->prefix}faq_categories c ON c.id=f.category WHERE 1";
            
            if(isset($data['category']) && $data['category'] != 0){
                $query .= " AND category =".(int)$data['category'];
            }

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

            if(isset($data['keyword'])){
                $keywords = explode(' ', $data['keyword']);
                $searchTermKeywords = array();
                foreach ($keywords as $word) {
                    $word                 = $this->db->escape($word);
                    $searchTermKeywords[] = " f.question LIKE '%$word%'";
                }
                $query .= ' AND '.implode(' AND ', $searchTermKeywords);
            }
            
            if(isset($data['sort'])){
                $query .= " ORDER BY " . $data['sort'];
            }else{
                $query .= " ORDER BY f.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($row['status'] == 1) {
                        $status = '<span class="badge bg-success">Aktif</span>';
                    }else if($row['status'] == 0) {
                        $status = '<span class="badge bg-danger">Pasif</span>';
                    }else {
                        $status = 'Bilinmiyor';
                    }
                    $item = array(
                        'id'                     => $row['id'],
                        'status'                 => $row['status'],
                        'status_text'            => $status,
                        'question'               => $row['question'],
                        'answer'                 => $row['answer'],
                        'category'               => $row['category'],
                        'category_name'          => $row['category_name']
                    );
                    array_push($posts, $item);
                }
            }
            return $posts;
        }

        public function getTotalFaqs($data = array()){
            $query = "SELECT COUNT(f.id) AS 'total' FROM {$this->db->prefix}faq f LEFT JOIN {$this->db->prefix}faq_categories c ON c.id=f.category WHERE 1";
            
            if(isset($data['category']) && $data['category'] != 0){
                $query .= " AND category =".(int)$data['category'];
            }

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

            if(isset($data['keyword'])){
                $keywords = explode(' ', $data['keyword']);
                $searchTermKeywords = array();
                foreach ($keywords as $word) {
                    $word                 = $this->db->escape($word);
                    $searchTermKeywords[] = " f.question 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 getCategory($id){
            $page = array();
            $stmt = $this->db->query("SELECT * FROM {$this->db->prefix}faq_categories WHERE id=:id LIMIT 1", array(':id'=>$id));
            if($stmt->num_rows > 0){
                $page = $stmt->row;
            }
            return $page;
        }

        public function getCategories() {
            $types = array();
            $query = "SELECT *, (SELECT url FROM {$this->db->prefix}urls WHERE url_type='faq-category' AND element=c.id) AS 'url' FROM {$this->db->prefix}faq_categories c";

            $stmt = $this->db->query($query);
            if($stmt->num_rows > 0) {
                foreach($stmt->rows as $row) {
                    $status = $row['status'];
                    if($row['status'] == 1) {
                        $status = '<span class="badge bg-success">Aktif</span>';
                    }else if($row['status'] == 0) {
                        $status = '<span class="badge bg-danger">Pasif</span>';
                    }else {
                        $status = 'Bilinmiyor';
                    }
                    $item = array(
                        'id'                     => $row['id'],
                        'status'                 => $row['status'],
                        'status_text'            => $status,
                        'title'                  => $row['title'],
                        'image'                  => $row['image'],
                        'url'                    => $row['url']
                    );
                    array_push($types, $item);
                }
            }

            return $types;
        }

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

        public function deleteCategory($id) {
            $this->db->query("DELETE FROM `{$this->db->prefix}faq_categories` WHERE id=:id", array(':id'=>$id));
            $this->db->query("DELETE FROM `{$this->db->prefix}urls` WHERE element=:id AND url_type='faq-category'", array(':id'=>$id));
        }
    }