<?php

class ControllerEntegrasyonGenel extends Controller
{



    public function currencies()
    {

        $currencies = array();
        $currencies[] = '';
        /* $currencies[] = 'TRY';
         $currencies[] = 'USD';
         $currencies[] = 'EUR';
        */


        foreach ($this->db->query("select code from currency")->rows as $currency) {

            $currencies[]=$currency['code'];
        }

        echo json_encode($currencies);

    }


    public function getjson()
    {


        $filter = $this->request->get['filter_name'];
        $filter = str_replace(' ', '%20', $filter);
        $code = $this->request->get['code'];
        $page = $this->request->get['page'];
        $which=0;
        if (isset($_GET['category_code'])) {

            $category_code = explode('|',$this->request->get['category_code']);
            $url = "https://www.opencart.gen.tr/index.php?route=api/search/" . $code . "_" . $page . "&category_code=$category_code[0]&filter_name=" . $filter;
            $which=0;
        } else {

            $url = "https://www.opencart.gen.tr/index.php?route=api/search/" . $code . "_" . $page . "&filter_name=" . $filter;
            $which=1;

        }





        if (strlen($filter) >= 2) {
            $ch = curl_init($url);

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            $result = curl_exec($ch);

            curl_close($ch);
            echo $result;

        }
    }


    public function membership()
    {

        $filter = $this->request->get['filter_name'];
        $filter = str_replace(' ', '%20', $filter);
        $code = $this->request->get['code'];
        $page = $this->request->get['page'];
        if (isset($_GET['category_code'])) {

            $category_code = $this->request->get['category_code'];
            $url = "http://www.opencart.gen.tr/index.php?route=api/membership/" . $code . "_" . $page . "&category_code=$category_code&filter_name=" . $filter;

        } else {

            $url = "http://www.opencart.gen.tr/index.php?route=api/membership/" . $code . "_" . $page . "&filter_name=" . $filter;

        }

        if (strlen($filter) >= 3) {
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            $result = curl_exec($ch);

            curl_close($ch);
            echo $result;

        }
    }


    public function n11_shipping_templates()
    {
        $post_data['request_data'] = array();


        $result = $this->entegrasyon->clientConnect($post_data, 'shipping_templates', 'n11',false);



        echo json_encode($result['result']);

    }

    public function get_ty_addreses()
    {
        $this->load->model('tool/entegrasyon_api');
        $result = $this->ty->shipping_templates($this->model_tool_entegrasyon_api->getEntegrasyonApiInfo('ty'));

        $adresses=array();
        foreach ($result['result']['supplierAddresses'] as $supplierAddress) {
            if($this->request->get['type'] == $supplierAddress['addressType'] ){
                $adresses[]=array('value'=>$supplierAddress['id'],'text'=>mb_substr($supplierAddress['address'],0,30).'...');

            }
        }


        echo json_encode($adresses);

    }


    public function save_setting($arg=array())
    {




        $message = '';
        $alert_type = 0;
        $solution_url = '';

        if(isset($arg['name'])){
            $name=$arg['name'];
        }else {
            $name= $this->request->post['name'];
        }
        if(isset($arg['value'])){
            $value=$arg['value'];
        }else {
            $value= $this->request->post['value'];
        }

        if(isset($arg['primary_id'])){
            $primary_id=$arg['primary_id'];
        }else {
            $primary_id= $this->request->post['pk'];
        }

        if(isset($arg['code'])){
            $code=$arg['code'];
        }else {

            $code= $this->request->get['code'];
        }

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



        if ($this->config->get('easy_setting_auto_update_price')){
            if ($name == $code.'_product_sale_price'){

                $marketplace_data = $this->entegrasyon->getMarketPlaceProductForMarket($primary_id, $code);
                if ($marketplace_data){
                    //ÜRÜNÜ GÜNCELLE

                }

                //   $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product WHERE product_id = $primary_id");

                //     $post_data = $query->row;

                //   $model = $query->row['model'];

                //  $query2 = $this->db->query("SELECT * FROM product_to_marketplace WHERE product_id = $primary_id");
                //  $post_data = $query2->row;

                // if ($query2->row['' . $code . '']){

                //    $result = $this->entegrasyon->clientConnect($post_data, 'update_basic', $code, false, false);
                //  print_r($result['status']);

                // }

            }

        }

        $action = isset($this->request->get['action']) ? $this->request->get['action'] : false;
        $attribute_result = array();
        $json_data = array();

        try {


            $query = $this->udb->query("select * from " . $controller."_setting  where " . $controller . "_id='" . $primary_id . "'");

            if ($query->num_rows) {
                $row = $query->row;




                $variable = $row[$code];
                
                if ($variable) {
                    $settings = unserialize($variable);

                    $temp=isset($settings[$name])?$settings[$name]:0;
                    $settings[$name] = $value;

                    if (!$value) {

                        unset($settings[$name]);
                    }


                    try {
                        if ($settings) {


                            $this->udb->query("update " . $controller."_setting  SET $code='" . $this->udb->escape(serialize($settings)) . "', date_modified=NOW() where " . $controller . "_id='" . $primary_id . "' ");
                            $message .= 'Güncellendi';

                        } else {

                            $this->udb->query("update " . $controller."_setting SET $code='', date_modified=NOW() where " . $controller . "_id='" . $primary_id . "' ");
                            $message .= 'Güncellendi';
                        }




                        if($controller == "category" && $name==$code.'_category_id' ){



                            $cat_info=explode('|',$temp);
                            $sql="DELETE FROM attribute where category_id='".$cat_info[0]."' and  `code`='".$code."'";

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

                        }


                    } catch (Exception $exception) {

                        $message .= $exception->getMessage();

                        echo $message;

                    }

                } else {

                    if($name){
                        $insert_data = array($name => $value);

                        $this->udb->query("update " . $controller . "_setting SET $code='" . $this->udb->escape(serialize($insert_data)) . "',date_modified=NOW() where " . $controller . "_id='" . $primary_id . "' ");
                        $message .= 'Güncellendi';
                    }else {

                        $message .= 'Güncellenemedi';
                    }

                }


            } else {
                //Yeni Kayıt Oluştur;
                $insert_data = array($name=>$value);
                $this->udb->query("insert into " . $controller . "_setting  SET $code='" . $this->udb->escape(serialize($insert_data)) . "', " . $controller . "_id='" . $primary_id . "', date_added=NOW() ");
                $message .= 'Eklendi';
            }
        } catch (Exception $exception) {


            echo $exception->getMessage();

        }


        $json_data['status'] = true;
        $json_data['message'] = $message;
        $json_data['alert_type'] = $alert_type;
        if ($solution_url) $json_data['url'] = $solution_url;
        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json_data));

    }


    public function deleteCategory_attributes($code, $category_id)
    {

        // $code='gg';

        //$category_id=504;

        $query =  $this->db->query("select * from category where category_id='".$category_id."'");
        $market_category_info=unserialize($query->row[$code]);

        if(isset($market_category_info[$code.'_category_id'])){

            $final_category_id=explode('|',$market_category_info[$code.'_category_id'])[0];

            $this->db->query("delete from attribute  where category_id='".$final_category_id."'");

        }


    }

    private function deleteAllProductsAndCategorySettingByMatchedCategory($code,$primary_id)
    {
        $filter_data = array(
            // 'filter_marketplace' => $code,
            'filter_category' => $primary_id

        );

        $this->load->model('entegrasyon/product');

        $getProducts = $this->model_entegrasyon_product->getProducts($filter_data);



        if ($getProducts) {

            foreach ($getProducts as $product) {



                $query = $this->db->query("select * from product_setting  where product_id='" . $product['product_id'] . "'");

                if ($query->num_rows) {
                    $row = $query->row;

                    $variable = $row['' . $code . ''];


                    if ($variable) {
                        $settings = unserialize($variable);

                        if (isset($settings['selected_attributes'])) {

                            unset($settings['selected_attributes']);
                            $this->db->query("update product_setting set  $code ='" . $this->db->escape(serialize($settings)) . "' where product_id='" . $product['product_id'] . "' ");

                        }

                    }


                }}

        }


        $this->deleteCategory_attributes($code,$primary_id);
        $this->db->query("update category set ".$code."='' where category_id='".$primary_id."' ");


    }


}





