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

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

        public function getProducts($data = array()) {
            $products = array();
            $query = "SELECT p.id AS 'product_id_', p.selling_price, p.image, p.title, p.model, (SELECT title FROM {$this->db->prefix}brands WHERE id=p.brand_id) AS 'brand', c.* FROM {$this->db->prefix}products p LEFT JOIN {$this->db->prefix}cimri_products c ON c.product_id = p.id WHERE 1";

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

            if(isset($data['brand_id']) && !empty($data['brand_id'])){
                $query .= " AND p.brand_id =".(int)$data['brand_id'];
            }

            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 (ean LIKE '%$word%') OR (isbn LIKE '%$word%') OR (sku LIKE '%$word%') OR (search_keywords LIKE '%$word%'))";
                }
                $query .= ' AND '.implode(' AND ', $searchTermKeywords);
            }

            if(isset($data['featured']) && !empty($data['featured'])){
                $query .= " AND p.featured =".(int)$data['featured'];
            }

            if(isset($data['free_shipping']) && !empty($data['free_shipping'])){
                $query .= " AND p.free_shipping =".(int)$data['free_shipping'];
            }

            if(isset($data['new']) && !empty($data['new'])){
                $query .= " AND p.new =".(int)$data['new'];
            }

            $query .= " GROUP BY p.id";
            
            if(isset($data['sort'])){
                if($data['sort'] == 'price'){
                    $query .= " ORDER BY p.selling_price";
                }else{
                    $query .= " ORDER BY " . $data['sort'];
                }
            }else{
                $query .= " ORDER BY p.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">Var</span>';
                    }else if($status == 0) {
                        $status = '<span class="badge bg-danger">Yok</span>';
                    }else {
                        $status = 'Bilinmiyor';
                    }
                    $row['status_text'] = $status;
                    array_push($products, $row);
                }
            }

            return $products;
        }

        public function getTotalProducts($data = array()){
            $query = "SELECT COUNT(DISTINCT p.id) AS 'total' FROM {$this->db->prefix}products p LEFT JOIN {$this->db->prefix}cimri_products c ON c.product_id = p.id WHERE 1";
            
            if(isset($data['status'])){
                if($data['status'] != ''){
                    $query .= " AND c.status =".$data['status'];
                }
            }

            if(isset($data['brand_id']) && !empty($data['brand_id'])){
                $query .= " AND p.brand_id =".(int)$data['brand_id'];
            }

            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 (ean LIKE '%$word%') OR (isbn LIKE '%$word%') OR (sku LIKE '%$word%') OR (search_keywords LIKE '%$word%'))";
                }
                $query .= ' AND '.implode(' AND ', $searchTermKeywords);
            }

            if(isset($data['featured']) && !empty($data['featured'])){
                $query .= " AND p.featured =".(int)$data['featured'];
            }

            if(isset($data['free_shipping']) && !empty($data['free_shipping'])){
                $query .= " AND p.free_shipping =".(int)$data['free_shipping'];
            }

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

            return $this->total_products;
        }
    }
?>