<?php

class ModelEntegrasyonCategory extends Model
{


    public function categorySettingDelete($code){



        if ($code == "all" ){

            $sql="DELETE FROM category ";


        }else {
            $sql="UPDATE category SET ".$code."= ''";

        }

        $this->udb->query($sql);

    }


    public function deleteCategory($category_id) {
        $this->udb->query("DELETE FROM " . DB_PREFIX . "category_path WHERE category_id = '" . (int)$category_id . "'");

        $query = $this->udb->query("SELECT * FROM " . DB_PREFIX . "category_path WHERE path_id = '" . (int)$category_id . "'");

        foreach ($query->rows as $result) {
            $this->deleteCategory($result['category_id']);
        }

        $this->udb->query("DELETE FROM " . DB_PREFIX . "category WHERE category_id = '" . (int)$category_id . "'");
        $this->udb->query("DELETE FROM " . DB_PREFIX . "category_description WHERE category_id = '" . (int)$category_id . "'");
        $this->udb->query("DELETE FROM " . DB_PREFIX . "category_filter WHERE category_id = '" . (int)$category_id . "'");
        $this->udb->query("DELETE FROM " . DB_PREFIX . "category_to_store WHERE category_id = '" . (int)$category_id . "'");
        $this->udb->query("DELETE FROM " . DB_PREFIX . "category_to_layout WHERE category_id = '" . (int)$category_id . "'");
        $this->udb->query("DELETE FROM " . DB_PREFIX . "product_to_category WHERE category_id = '" . (int)$category_id . "'");

        $this->cache->delete('category');
    }




    public function getCategory($category_id)
    {
        $query = $this->udb->query("SELECT DISTINCT *, (SELECT GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR '&nbsp;&nbsp;&gt;&nbsp;&nbsp;') FROM " . DB_PREFIX . "category_path cp LEFT JOIN " . DB_PREFIX . "category_description cd1 ON (cp.path_id = cd1.category_id AND cp.category_id != cp.path_id) WHERE cp.category_id = c.category_id AND cd1.language_id = '" . (int)$this->config->get('config_language_id') . "' GROUP BY cp.category_id) AS path FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd2 ON (c.category_id = cd2.category_id) WHERE c.category_id = '" . (int)$category_id . "' AND cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'");

        return $query->row;
    }


    public function getMarketCategory($category_id,$code)
    {

        $query=$this->udb->query("select * from category_setting where category_id='".$category_id."'");

        return unserialize($query->row[$code]);

    }

    public function getCategoriesAuto($data = array())
    {
        $sql = "SELECT category_id, parent_id, name FROM oc_category  where language_id=1 ";

        if (!empty($data['filter_category'])) {
            $sql .= " AND category_id ='" . $data['filter_category'] . "'";
        }

        $sql .= " group by name ";

        $sort_data = array(
            'name',
            'sort_order'
        );



        if (isset($data['order']) && ($data['order'] == 'DESC')) {
            $sql .= " DESC";
        } else {
            $sql .= " ASC";
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }

            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }



        try {

            $query = $this->db->query($sql);

            return $query->rows;
        } catch (Exception $exception) {

            echo $exception->getMessage();
        }

    }
    public function getCategories($data = array())
    {
        $sql = "SELECT c.category_id, c.parent_id, c.name,cs.ty,cs.hb,cs.cs,cs.eptt,cs.n11 FROM product p left join multimarket.oc_category c on(c.category_id=p.category_id) left join category_setting cs on(cs.category_id=p.category_id) where language_id=1 ";

        if (!empty($data['filter_category'])) {
            $sql .= " AND c.category_id ='" . $data['filter_category'] . "'";
        }

        $sql .= " group by c.name ";

        $sort_data = array(
            'name',
            'sort_order'
        );



        if (isset($data['order']) && ($data['order'] == 'DESC')) {
            $sql .= " DESC";
        } else {
            $sql .= " ASC";
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }

            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }



        try {
            $query = $this->udb->query($sql);

            return $query->rows;
        } catch (Exception $exception) {

            echo $exception->getMessage();
        }

    }

    


    public function getTotalCategories($data)
    {

        $sql = "SELECT count(*) as total FROM product p ";

        if ($data['filter_category']) {

            $sql .= " where category_id='" . $data['filter_category'] . "'";
        }

        $sql.=" group by p.category_id";

        $query = $this->udb->query($sql);
        return $query->num_rows;
    }


    public function getAttributes($category_id, $code,$onlyRequired=false,$onlyVarianter=false)
    {


        if(isset($this->request->get['page'])){
            $page=$this->request->get['page'];
        }else {

            $page=1;
        }

        $token=$this->request->get['user_token'];

        $limit= 1;

       // $query = $this->db->query("select category_id from ".DB_PREFIX."category limit ".$limit*$page.",  ".$limit."  ");

        $message = '';
        $status = true;
        $marketplace_id= $this->db->query("select * from entegrasyon where code='".$code."'")->row['entegrasyon_id'];




        $query = $this->db->query("select * from ".DB_PREFIX."category where category_id='".$category_id."'  ");

        //print_r($query->row);return;

        if($query->row[$code]){
            $marketplace_category_id=$query->row[$code];


            $sql="SELECT ca.varianter,ca.required,ca.category_id,a.attribute_id,a.attribute_name,a.market_attribute_id,a.allow_custom,a.hb,a.ty,a.cs,a.ali,a.amz,a.amzus,a.etsy FROM `oc_category_attribute` ca left join oc_attribute a ON(a.market_attribute_id = ca.attribute_id ) where ca.category_id='".$marketplace_category_id."' and a.marketplace_id='".$marketplace_id."'";
            if($onlyRequired){

                $sql.=" and ca.required=1";

            }
            if($onlyVarianter){

                $sql.=" and ca.varianter=1";

            }
        // echo $sql; return;
            $query = $this->db->query($sql);

   

            return array('status' => $status, 'message' => $message, 'result' => $query->rows);

        }else {

            return array('status' => false, 'message' => 'Kategori Eşleşmemiş', 'result' => array());

        }






    }



}