<?php
    class App {
        public $db;
        public $title;
        public $website;
        public $url;
        public $address;
        public $town;
        public $city;
        public $about;
        public $phone;
        public $phone1;
        public $phone2;
        public $phone3;
        public $cellphone;
        public $cellphone1;
        public $cellphone2;
        public $cellphone3;
        public $whatsapp;
        public $email;
        public $fax;
        public $twitter;
        public $facebook;
        public $instagram;
        public $youtube;
        public $map;
        public $keywords;
        public $head_scripts;
        public $footer_scripts;
        public $version;
        public $seo_title;
        public $seo_keywords;
        public $seo_description;

        public $comp_title;
        public $tax_office;
        public $tax_number;
        public $mersis_number;
        public $kep_address;
        public $trade_registary_number;

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

        function get(){
            $query = $this->db->query("SELECT * FROM {$this->db->prefix}config LIMIT 1");
            if($query->num_rows == 1){
                $this->title           = $query->row["sitename"];
                $this->url             = $query->row["url"];
                $this->address         = $query->row["address"];
                //$this->address2        = $query->row["address2"];
                $this->town            = $query->row["town"];
                $this->city            = $query->row["city"];
                $this->phone           = $query->row["phone"];
                $this->phone1          = $query->row["phone1"];
                $this->phone2          = $query->row["phone2"];
                $this->phone3          = $query->row["phone3"];
                $this->cellphone       = $query->row["cellphone"];
                $this->cellphone1      = $query->row["cellphone1"];
                $this->cellphone2      = $query->row["cellphone2"];
                $this->cellphone3      = $query->row["cellphone3"];
                $this->whatsapp        = $query->row["whatsapp"];
                $this->email           = $query->row["email"];
                $this->twitter         = $query->row["twitter"];
                $this->facebook        = $query->row["facebook"];
                $this->instagram       = $query->row["instagram"];
                $this->youtube         = $query->row["youtube"];
                $this->map             = $query->row["map"];
                $this->head_scripts	   = $query->row["head_scripts"];
                $this->footer_scripts  = $query->row["footer_scripts"];
                $this->version         = $query->row["version"];

                $this->comp_title		= $query->row["comp_title"];
                $this->tax_office		= $query->row["tax_office"];
                $this->tax_number		= $query->row["tax_number"];
                $this->mersis_number	= $query->row["mersis_number"];
                $this->kep_address	    = $query->row["kep_address"];
                $this->trade_registary_number = $query->row["trade_registary_number"];
                $this->logo             = "logo.png";

                
                /*$this->seo_title       = $seo_title;
                $this->seo_keywords    = $seo_keywords;
                $this->seo_description = $seo_description;*/
            }
        }

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

            if(isset($data['search_keywords'])){
                $keywords = explode(' ', $data['search_keywords']);
                $searchTermKeywords = array();
                foreach ($keywords as $word) {
                    $word                 = $this->db->escape($word);
                    $searchTermKeywords[] = "(alt_text 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'],
                        'image_mobile'           => $row['image_mobile'],
                        'alt_text'               => $row['alt_text'],
                        'order'                  => $row['order'],
                        'link'                   => $row['link']
                    );
                    array_push($users, $item);
                }
            }

            return $users;
        }

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

            if(isset($data['search_keywords'])){
                $keywords = explode(' ', $data['search_keywords']);
                $searchTermKeywords = array();
                foreach ($keywords as $word) {
                    $word                 = $this->db->escape($word);
                    $searchTermKeywords[] = "(alt_text 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 getSlider($id) {
            $query = "SELECT * FROM {$this->db->prefix}carousel WHERE id=:id";
            $stmt = $this->db->query($query, array(':id'=>$id));
            if($stmt->num_rows > 0) {
                return $stmt->row;
            }

            return array();
        }

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