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

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

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

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

            if(isset($data['keyword'])){
                $keywords = explode(' ', $data['keyword']);
                $searchTermKeywords = array();
                foreach ($keywords as $word) {
                    $word                 = $this->db->escape($word);
                    $searchTermKeywords[] = " title LIKE '%$word%' OR short_name LIKE '%$word%' OR tax_no 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($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'],
                        'short_name'             => $row['short_name']
                    );
                    array_push($sellers, $item);
                }
            }
            return $sellers;
        }

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

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

            if(isset($data['keyword'])){
                $keywords = explode(' ', $data['keyword']);
                $searchTermKeywords = array();
                foreach ($keywords as $word) {
                    $word                 = $this->db->escape($word);
                    $searchTermKeywords[] = " title LIKE '%$word%' OR short_name LIKE '%$word%' OR tax_no 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 deleteSeller($id) {
            $this->db->query("DELETE FROM `{$this->db->prefix}sellers` WHERE id=:id", array(':id'=>$id));
        }
    }