<?php

class ControllerEntegrasyonSetting extends Controller
{
    private $error = array();
    private $check;
    private $token_data;
    private $validate = true;
    private $reg = null;

    public function __construct($registry)
    {

        parent::__construct($registry);
        $this->reg = $registry;
        $this->load->model('entegrasyon/general');
        $this->token_data = $this->model_entegrasyon_general->getToken();

    }

    public function privacy()
    {

        $data['token_link'] = $this->token_data['token_link'];

        $this->response->setOutput($this->load->view('entegrasyon/setting/account/privacy', $data));

    }

    public function auto()
    {


        if (($this->request->server['REQUEST_METHOD'] == 'POST')) {


           // $this->db->query();


        }else {

            $data['action'] = $this->url->link('entegrasyon/setting/auto', 'user_token=' . $this->session->data['user_token'], true);


            $query=  $this->db->query("select * from auto_update where auto_type=2");



            $data['crons']=$query->rows;
            $data['token_link'] = $this->token_data['token_link'];

            $this->document->addScript('https://cdn.jsdelivr.net/npm/sweetalert2@11');
            $this->document->addScript('//cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap3-editable/js/bootstrap-editable.min.js');
            $this->document->addStyle('//cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap3-editable/css/bootstrap-editable.css');
            $this->document->addStyle('https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css');


            $data['header'] = $this->load->controller('common/header');
            $data['column_left'] = $this->load->controller('common/column_left');
            $data['footer'] = $this->load->controller('common/footer');

            $this->response->setOutput($this->load->view('entegrasyon/setting/auto', $data));


        }



    }


    public function update_cron()
    {

        $name=$this->request->post['name'];
        $value=$this->request->post['value'];
        $pk=$this->request->post['pk'];

        $this->db->query("update auto_update SET $name='".$value."' where auto_update_id='".$pk."' ");


        echo json_encode(array('status'=>true));

    }


    public function fast_update_bulk()
    {


        $this->updatedb_for_request();

        $this->load->model('entegrasyon/general');
        $marketplaces = $this->model_entegrasyon_general->getMarketPlaces();
        $this->reg->set('easybulk', new Easybulk($this->reg));
        //$data['code'] =  $this->request->get['code'];
        //$data['marketplace'] =  $this->request->get['marketplace'];

        $data['catalog_url'] = HTTPS_CATALOG;

        $data['marketplace_products_total'] = array();

        foreach ($marketplaces as $marketplace) {
            $filter_data = array();
            $filter_data['market'] = $marketplace['code'];

            $data['marketplaces'][] = array(
                'products_total' => $this->easybulk->getUpdatableProductTotal($filter_data),
                'name' => $marketplace['name'],
                'code' => $marketplace['code'],
                'status' => $marketplace['status'],
                'logo' => $marketplace['logo']
            );


        }


        $this->response->setOutput($this->load->view('entegrasyon/setting/fast_bulk_update2', $data));


    }




    public function index()
    {
        //file_get_contents('https://www.dataaccess.com/webservicesserver/numberconversion.wso?WSDL');return;
        $this->load->language('entegrasyon/setting');

        $data = $this->language->all();


        $this->document->addStyle('view/stylesheet/css/setup.css');
        $this->document->addScript('https://app.shopside.io/_scripts/_common/_setup.js?v=1.2.5');
        $this->load->model("entegrasyon/general");
        $data['permission'] = $this->model_entegrasyon_general->checkPermission();

        //  $this->load->model('setting/extension');
        $this->load->model('setting/setting');
        $this->load->model('tool/image');

        $this->document->setTitle('Entegrasyon Ayarları');

        $this->model_entegrasyon_general->loadPageRequired();

        $data['marketplaces'] = array();
        $marketplaces = $this->db->query("select * from entegrasyon where entegrasyon_group_id=1 and status = 1 ")->rows;
        foreach ($marketplaces as $marketplace) {
            $user_marketplace_info = $this->db->query("select count(*) as total from database_entegrasyon where database_id='" . $this->user->getDbId() . "' and entegrasyon_id='" . $marketplace['entegrasyon_id'] . "'  ");

            $marketplace['is_registered'] = $user_marketplace_info->row['total'];
            $marketplace['image'] = HTTPS_CATALOG . 'image/entegrasyon-images/' . $marketplace['image'];
            $data['marketplaces'][] = $marketplace;

        }


        $data['success'] = '';
        if (isset($this->session->data['success'])) {
            $data['success'] = $this->session->data['success'];
            unset($this->session->data['success']);
        }

        $data['token_link'] = $this->token_data['token_link'];

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $this->response->setOutput($this->load->view('entegrasyon/setting', $data));
    }

    public function tool()
    {

        $data['domain'] = HTTP_CATALOG;
        if (!$this->config->get('module_entegrasyon_status')) {
            $this->response->redirect($this->url->link('entegrasyon/setting/error', '&error=no_module&' . $this->token_data['token_link'], true));

        }


        $this->load->language('entegrasyon/setting');

        $data = $this->language->all();
        $data['easy_visibility'] = $this->config->get('easy_visibility') ? '' : 'hidden';

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

        $this->load->model("entegrasyon/general");

        $data['permission'] = $this->model_entegrasyon_general->checkPermission();


        //  $this->load->model('setting/extension');
        $this->load->model('setting/setting');

        $this->document->setTitle('Entegrasyon Araçları');

        $this->model_entegrasyon_general->loadPageRequired();


        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', $this->token_data['token_link'], true),
        );
        $data['breadcrumbs'][] = array(
            'text' => 'Araçlar',
            'href' => $this->url->link('entegrasyon/setting/tool', $this->token_data['token_link'], true),


        );

        $data['success'] = '';
        if (isset($this->session->data['success'])) {
            $data['success'] = $this->session->data['success'];
            unset($this->session->data['success']);
        }

        //  print_r($this->config);

        //     return;

        if (isset($this->request->get['tool'])) {

            $data['success'] = '<b>Varyantlar Başarılı Bir Şekilde Yenilendi <b/>- ' . ' <' . date('d.m.Y - H:i:s') . '>';
        }


        $this->load->model('entegrasyon/general');
        $data['marketplaces'] = $this->model_entegrasyon_general->getMarketPlaces();

        $data['token_link'] = $this->token_data['token_link'];
        $data['form'] = $form;

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $this->response->setOutput($this->load->view('entegrasyon/setting/tool', $data));
    }


    public function tool_history_list()
    {
        if (!$this->config->get('module_entegrasyon_status')) {
            $this->response->redirect($this->url->link('entegrasyon/setting/error', '&error=no_module&' . $this->token_data['token_link'], true));

        }


        $this->load->language('entegrasyon/setting');

        $data = $this->language->all();
        $data['easy_visibility'] = $this->config->get('easy_visibility') ? '' : 'hidden';

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

        $this->load->model("entegrasyon/general");

        $data['permission'] = $this->model_entegrasyon_general->checkPermission();


        //  $this->load->model('setting/extension');
        $this->load->model('setting/setting');

        $this->document->setTitle('Entegrasyon Araç Geçmiş İşlemleri');

        $this->model_entegrasyon_general->loadPageRequired();


        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => 'Araçlar',
            'href' => $this->url->link('entegrasyon/setting/tool', $this->token_data['token_link'], true),
        );
        $data['breadcrumbs'][] = array(
            'text' => 'Araç Kullanım Geçmişi',
            'href' => $this->url->link('entegrasyon/setting/tool_history_list', $this->token_data['token_link'], true),


        );


        $this->load->model('entegrasyon/general');
        $data['marketplaces'] = $this->model_entegrasyon_general->getMarketPlaces();

        $data['token_link'] = $this->token_data['token_link'];
        $data['form'] = $form;

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $this->load->model('entegrasyon/tool_history');
        $data['historys'] = $this->model_entegrasyon_tool_history->getHistorys();


        $this->response->setOutput($this->load->view('entegrasyon/setting/tool_history_list', $data));


    }


    public function login()
    {


        $username = $this->request->post['username'];
        $password = $this->request->post['password'];

        if (!$username && !$password) {

            echo json_encode(array('status' => false, 'message' => "Email ve Parolanızı Giriniz"));

            return;
        }

        // $request_data = array();
        $post_data = array('username' => $username, 'password' => $password);

        $result = $this->entegrasyon->clientConnect($post_data, 'login', null, false);


        if ($result['status']) {

            $this->load->model('setting/setting');
            $saved_data = array('mir_login' => true, 'mir_username' => $username, 'mir_password' => $password, 'mir_domain_id' => $result['result']['domain_id'], 'mir_marketplaces' => serialize($result['result']['marketplaces']));
            $this->model_setting_setting->editSetting('mir', $saved_data);


        }

        echo json_encode($result);


    }


    public function install_success()
    {


        $data['token_link'] = $this->token_data['token_link'];

        // print_r(HTTP_CATALOG);
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }

        $email = $this->config->get('config_email');
        $phone = $this->config->get('config_telephone');


        $ch = curl_init("https://www.easyentegre.com/index.php?route=api/installed_domains");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,
            "phone=" . $phone . "&email=" . $email . "&domain=" . HTTP_CATALOG . "&ip=" . $ip . "");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $server_output = curl_exec($ch);

        curl_close($ch);
        $this->response->setOutput($this->load->view('entegrasyon/setting/install_success', $data));

    }

    public function login_success()
    {
        $data['token_link'] = $this->token_data['token_link'];
        $this->load->model('entegrasyon/general');
        $data['marketplaces'] = $this->model_entegrasyon_general->getMarketPlaces();
        $data['marketplace_count'] = count($data['marketplaces']);
        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');
        $data['setting_url'] = $this->url->link('entegrasyon/setting', $this->token_data['token_link'], true);

        $this->response->setOutput($this->load->view('entegrasyon/setting/account/login_success', $data));

    }

    public function register()
    {

        $data['token_link'] = $this->token_data['token_link'];

        if (($this->request->server['REQUEST_METHOD'] == 'POST')) {

            $status = true;
            $message = '';
            $username = $this->request->post['username'];
            $password = $this->request->post['password'];
            $phone = $this->request->post['phone'];
            $password_again = $this->request->post['password_again'];
            $post_data = array('username' => $username, 'phone' => $phone, 'password' => $password);

            $result = $this->entegrasyon->clientConnect($post_data, 'register', null, false);

            if ($result['status']) {

                $this->load->model('setting/setting');
                $saved_data = array('mir_login' => true, 'mir_username' => $username, 'mir_password' => $password, 'mir_domain_id' => $result['result']['domain_id'], 'mir_marketplaces' => serialize($result['result']['marketplaces']));
                $this->model_setting_setting->editSetting('mir', $saved_data);

            }
            echo json_encode($result);

        } else {


            $this->response->setOutput($this->load->view('entegrasyon/setting/account/register_form', $data));


        }
    }

    public function account_info()
    {

        $this->document->setTitle('Entegrasyon Ayarları');

        $this->model_entegrasyon_general->loadPageRequired();


        $data['username'] = $this->config->get('mir_username');
        $data['token_link'] = $this->token_data['token_link'];

        $this->load->model('entegrasyon/general');
        $marketplaces = $this->model_entegrasyon_general->getMarketPlaces();

        $data['marketplaces'] = array();

        foreach ($marketplaces as $marketplace) {
            $last_date = strtotime($marketplace['end_date']);
            $todaday = strtotime(date('Y-m-d'));
            $left_day = ($last_date - $todaday) / 86400;
            $marketplace['left_day'] = floor($left_day);
            $marketplace['left_day'] = $marketplace['left_day'] < 0 ? 0 : $marketplace['left_day'];
            $data['marketplaces'][] = $marketplace;
        }
        $data['token_link'] = $this->token_data['token_link'];

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $this->response->setOutput($this->load->view('entegrasyon/setting//account/account_info', $data));


    }

    public function cart()
    {

        $data['products'] = array();

        if (isset($this->session->data['marketplaces'])) {
            $marketplaces = $this->session->data['marketplaces'];
        } else {
            $marketplaces = array();

        }


        foreach ($marketplaces as $marketplace) {

            $data['products'][] = array(
                'code' => $marketplace['code'],
                'name' => $marketplace['name'],
                'price' => '700 TL',
                'period' => "1 Yıl"
            );
        }

        $total = count($data['products']);
        if ($total == 1) {
            $data['total'] = 700;
            $data['list_price'] = 700;
            $data['discount'] = 0;
        } else if ($total == 2) {
            $data['total'] = 1250;
            $data['list_price'] = 1500;
            $data['discount'] = 250;
        } else if ($total == 3) {
            $data['total'] = 1750;
            $data['list_price'] = 2250;
            $data['discount'] = 500;
        } else if ($total == 4) {
            $data['total'] = 2300;
            $data['list_price'] = 3000;
            $data['discount'] = 700;
        } else if ($total == 5) {
            $data['total'] = 2600;
            $data['list_price'] = 3750;
            $data['discount'] = 1150;
        } else if ($total == 6) {
            $data['total'] = 3000;
            $data['list_price'] = 4500;
            $data['discount'] = 1500;
        } else if ($total == 7) {
            $data['total'] = 2600;
            $data['list_price'] = 3500;
            $data['discount'] = 900;
        }


        $this->response->setOutput($this->load->view('entegrasyon/setting//account/cart', $data));

    }

    public function paid()
    {

        $url = 'https://www.easyentegre.com/odeme.html?m=';
        $query = '';
        $markets = array();
        $domain_id = 0;
        $password = $this->config->get('mir_password');
        foreach ($this->session->data['marketplaces'] as $marketplace) {
            $markets[] = $marketplace['code'];
            $domain_id = $marketplace['domain_id'];


        }
        $query .= implode(',', $markets);
        $query .= '&domain_id=' . $domain_id . '&password=' . $password;
        $query = base64_encode($query);
        $url .= $query;


        echo json_encode(array('status' => true, 'url' => $url));

    }

    public function add_to_cart()
    {
        // unset($this->session->data['marketplaces']);
        $code = $this->request->post['code'];

        $this->load->model('entegrasyon/general');
        if (isset($this->session->data['marketplaces'])) {


            if (!key_exists($code, $this->session->data['marketplaces'])) {
                $this->session->data['marketplaces'][$code] = $this->model_entegrasyon_general->getMarketPlace($code);
            }

        } else {
            $this->session->data['marketplaces'][$code] = $this->model_entegrasyon_general->getMarketPlace($code);


        }

        echo json_encode(array('status' => true));

    }


    public function logout()
    {
        $this->load->model('setting/setting');
        $this->model_setting_setting->deleteSetting('mir');
        $this->response->redirect($this->url->link('entegrasyon/setting', $this->token_data['token_link'], true));

    }





    public function unmatch_all()
    {
        $code = $this->request->get['code'];

        $this->db->query("update product_to_marketplace SET $code='' ");
        $this->db->query("update market_product SET oc_product_id=0 where code='" . $code . "' ");

        echo json_encode(array('status' => true));
    }


    public function edit_n11()
    {


        if (($this->request->server['REQUEST_METHOD'] == 'POST')) {


            $this->load->model('setting/setting');
            $this->model_setting_setting->editSetting('n11_setting', $this->request->post);
            echo json_encode(array('status' => true));
            return;
        }

        $data['token_link'] = $this->token_data['token_link'];
        $data['n11_setting_maximum_order'] = $this->config->get('n11_setting_maximum_order');

        $data['n11_setting_shipping_time'] = $this->config->get('n11_setting_shipping_time');
        $data['n11_setting_shipping_template'] = $this->config->get('n11_setting_shipping_template');
        $data['n11_setting_commission'] = str_replace("%", "", $this->config->get('n11_setting_commission'));
        $data['n11_setting_subtitle'] = $this->config->get('n11_setting_subtitle');
        $data['n11_setting_product_attribute'] = $this->config->get('n11_setting_product_attribute');
        $data['n11_setting_kdv_setting'] = $this->config->get('n11_setting_kdv_setting');
        $data['n11_setting_option_setting'] = $this->config->get('n11_setting_option_setting');
        $data['n11_setting_additional_content'] = $this->config->get('n11_setting_additional_content');
        $data['n11_setting_product_special'] = $this->config->get('n11_setting_product_special');
        $data['n11_setting_shipping_price'] = $this->config->get('n11_setting_shipping_price');
        $data['n11_setting_model_prefix'] = $this->config->get('n11_setting_model_prefix');
        $data['n11_setting_variant'] = $this->config->get('n11_setting_variant');
        $data['n11_setting_barkod_place'] = $this->config->get('n11_setting_barkod_place');
        $data['n11_setting_add_tc'] = $this->config->get('n11_setting_add_tc');
        $data['n11_setting_add_tax'] = $this->config->get('n11_setting_add_tax');
        $data['n11_setting_add_tax_val'] = $this->config->get('n11_setting_add_tax_val');
        $data['n11_setting_domestic'] = $this->config->get('n11_setting_domestic');
        $data['n11_setting_product_category'] = $this->config->get('n11_setting_product_category');
        $data['n11_setting_product_iscurrency'] = $this->config->get('n11_setting_product_iscurrency');
        $data['n11_setting_oc_order'] = $this->config->get('n11_setting_oc_order');
        $data['n11_setting_customer_group'] = $this->config->get('n11_setting_customer_group');
        $data['barkod_places'] = array('sku', 'mpn', 'upc', 'jan', 'ean', 'isbn');
        $data['n11_setting_price_multiplier'] = $this->config->get('n11_setting_price_multiplier');

        $this->load->model('catalog/information');
        $data['informations'] = $this->model_catalog_information->getInformations();


        $post_data['request_data'] = array();
        $tempalates = $this->entegrasyon->clientConnect($post_data, 'shipping_templates', 'n11');

        $data['shipping_templates'] = $tempalates['result'];


        $this->response->setOutput($this->load->view('entegrasyon/setting/mainsetting/n11', $data));


    }


    public function edit_gg()
    {

        if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
            $this->load->model('setting/setting');
            $this->model_setting_setting->editSetting('gg_setting', $this->request->post);
            echo json_encode(array('status' => true));
            return;
        }

        $data['token_link'] = $this->token_data['token_link'];
        $data['gg_setting_city'] = $this->config->get('gg_setting_city');
        $data['gg_setting_shipping_time'] = $this->config->get('gg_setting_shipping_time');
        $data['gg_setting_shipping_template'] = $this->config->get('gg_setting_shipping_template');
        $data['gg_setting_commission'] = str_replace("%", "", $this->config->get('gg_setting_commission'));
        $data['gg_setting_subtitle'] = $this->config->get('gg_setting_subtitle');
        $data['gg_setting_product_attribute'] = $this->config->get('gg_setting_product_attribute');
        $data['gg_setting_kdv_setting'] = $this->config->get('gg_setting_kdv_setting');
        $data['gg_setting_shipping_price'] = $this->config->get('gg_setting_shipping_price');
        $data['gg_setting_extra_shipping_price'] = $this->config->get('gg_setting_extra_shipping_price');
        $data['gg_setting_hour'] = $this->config->get('gg_setting_hour');
        $data['gg_setting_minute'] = $this->config->get('gg_setting_minute');
        $data['gg_setting_show_time'] = $this->config->get('gg_setting_show_time');
        $data['gg_setting_shipping_company'] = $this->config->get('gg_setting_shipping_company');
        $data['gg_setting_additional_content'] = $this->config->get('gg_setting_additional_content');
        $data['gg_setting_product_special'] = $this->config->get('gg_setting_product_special');
        $data['gg_setting_model_prefix'] = $this->config->get('gg_setting_model_prefix');
        $data['gg_setting_variant'] = $this->config->get('gg_setting_variant');
        $data['gg_setting_add_tc'] = $this->config->get('gg_setting_add_tc');
        $data['gg_setting_barkod_place'] = $this->config->get('gg_setting_barkod_place');
        $data['gg_setting_product_category'] = $this->config->get('gg_setting_product_category');
        $data['gg_setting_product_iscurrency'] = $this->config->get('gg_setting_product_iscurrency');
        $data['gg_setting_oc_order'] = $this->config->get('gg_setting_oc_order');
        $data['gg_setting_customer_group'] = $this->config->get('gg_setting_customer_group');
        $data['gg_setting_pay_in_the_basket'] = $this->config->get('gg_setting_pay_in_the_basket');
        $data['gg_setting_default_variant_color'] = $this->config->get('gg_setting_default_variant_color');
        $data['gg_setting_product_status'] = $this->config->get('gg_setting_product_status');
        $data['barkod_places'] = array('sku', 'mpn', 'upc', 'jan', 'ean', 'isbn');
        $data['gg_setting_price_multiplier'] = $this->config->get('gg_setting_price_multiplier');

        $this->load->model('catalog/information');
        $data['informations'] = $this->model_catalog_information->getInformations();

        $data['hours'] = array('00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23');
        $data['minutes'] = array('00', '15', '30', '45');


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

        $data['cargo_company'] = array('aras' => 'Aras Kargo', 'mng' => 'MNG Kargo', 'yurtici' => 'Yurtiçi Kargo', 'ptt' => 'Ptt Kargo', 'surat' => 'Sürat Kargo', 'ups' => 'UPS', 'other' => 'Diğer');


        $data['cities'] = array();

        $post_data['request_data'] = array();


        $get_cities = $this->entegrasyon->clientConnect($post_data, 'get_cities', 'gg', false);


        foreach ($get_cities['result']['cities']['city'] as $city) {
            //print_r($city);
            $data['cities'][] = $city;
        }

        $data['sablonlar'] = array('S' => 'Satıcı Öder', 'B' => 'Alıcı Öder', 'D' => 'İndirilebilir Ürün');// Key ler bulunacak

        $data['show_time'] = array('30' => '30 Gün', '60' => '60 Gün ', '180' => '180 Gün ', '360' => '360 Gün ');


        $this->response->setOutput($this->load->view('entegrasyon/setting/mainsetting/gg', $data));


    }

    public function edit_cs()
    {


        if (($this->request->server['REQUEST_METHOD'] == 'POST')) {

            $this->load->model('setting/setting');
            $this->model_setting_setting->editSetting('cs_setting', $this->request->post);
            echo json_encode(array('status' => true));
            return;
        }

        $data['token_link'] = $this->token_data['token_link'];
        $data['cs_setting_commission'] = str_replace("%", "", $this->config->get('cs_setting_commission'));
        $data['cs_setting_product_attribute'] = $this->config->get('cs_setting_product_attribute');
        $data['cs_setting_kdv_setting'] = $this->config->get('cs_setting_kdv_setting');
        $data['cs_setting_additional_content'] = $this->config->get('cs_setting_additional_content');
        $data['cs_setting_product_special'] = $this->config->get('cs_setting_product_special');
        $data['cs_setting_shipping_price'] = $this->config->get('cs_setting_shipping_price');
        $data['cs_setting_model_prefix'] = $this->config->get('cs_setting_model_prefix');
        $data['cs_setting_variant'] = $this->config->get('cs_setting_variant');
        $data['cs_setting_add_tc'] = $this->config->get('cs_setting_add_tc');
        $data['cs_setting_barkod_place'] = $this->config->get('cs_setting_barkod_place');
        $data['cs_setting_main_product_id'] = $this->config->get('cs_setting_main_product_id');
        $data['cs_setting_product_category'] = $this->config->get('cs_setting_product_category');
        $data['cs_setting_product_iscurrency'] = $this->config->get('cs_setting_product_iscurrency');
        $data['cs_setting_oc_order'] = $this->config->get('cs_setting_oc_order');
        $data['cs_setting_delivery_message_type'] = $this->config->get('cs_setting_delivery_message_type');
        $data['cs_setting_delivery_type'] = $this->config->get('cs_setting_delivery_type');
        $data['cs_setting_customer_group'] = $this->config->get('cs_setting_customer_group');
        $data['barkod_places'] = array('sku', 'mpn', 'upc', 'jan', 'ean', 'isbn');
        $data['cs_setting_price_multiplier'] = $this->config->get('cs_setting_price_multiplier');


        $this->load->model('catalog/information');
        $data['informations'] = $this->model_catalog_information->getInformations();

        $data['deliveryTypes'] = array(1 => 'Servis Aracı İle Gönderim', 2 => 'Kargo İle Gönderim', 3 => 'Kargo+Servis Aracı İle Gönderim');
        $data['deliveryMessageTypes'] = array(

            4 => 'Hediye Kargo Aynı Gün',
            5 => 'Hediye Kargo 1-3 İs Günü',
            6 => 'Hediye Kargo 1-5 Is Günü',
            7 => 'Hediye Kargo 1-7 İs Günü',
            13 => 'Hediye Kargo 3-5 İs Günü',
            18 => 'Hediye Kargo 1-2 İs Günü',
            10 => 'Gurme Kargo Aynı Gün',
            20 => 'Gurme Kargo 1-2 İs Günü',
            12 => 'Gurme Kargo 1-3 İs Günü',
            22 => 'Gurme Kargo 1-5 Is Günü'

        );

        $this->response->setOutput($this->load->view('entegrasyon/setting/mainsetting/cs', $data));


//  print_r($data);

    }


    public function edit_ty()
    {


        if (($this->request->server['REQUEST_METHOD'] == 'POST')) {


            $this->load->model('setting/setting');
            $this->model_setting_setting->editSetting('ty_setting', $this->request->post);
            echo json_encode(array('status' => true));
            return;
        }

        $data['token_link'] = $this->token_data['token_link'];
        $data['ty_setting_commission'] = str_replace("%", "", $this->config->get('ty_setting_commission'));
        $data['ty_setting_color'] = $this->config->get('ty_setting_color');
        $data['ty_setting_product_attribute'] = $this->config->get('ty_setting_product_attribute');
        $data['ty_setting_kdv_setting'] = $this->config->get('ty_setting_kdv_setting');
        $data['ty_setting_additional_content'] = $this->config->get('ty_setting_additional_content');
        $data['ty_setting_product_special'] = $this->config->get('ty_setting_product_special');
        $data['ty_setting_shipping_price'] = $this->config->get('ty_setting_shipping_price');
        $data['ty_setting_model_prefix'] = $this->config->get('ty_setting_model_prefix');
        $data['ty_setting_variant'] = $this->config->get('ty_setting_variant');
        $data['ty_setting_barkod_place'] = $this->config->get('ty_setting_barkod_place');
        $data['ty_setting_main_product_id'] = $this->config->get('ty_setting_main_product_id');
        $data['ty_setting_product_category'] = $this->config->get('ty_setting_product_category');
        $data['ty_setting_product_iscurrency'] = $this->config->get('ty_setting_product_iscurrency');
        $data['ty_setting_oc_order'] = $this->config->get('ty_setting_oc_order');
        $data['ty_setting_customer_group'] = $this->config->get('ty_setting_customer_group');
        $data['ty_setting_add_tc'] = $this->config->get('ty_setting_add_tc');
        $data['ty_setting_shipping_company'] = $this->config->get('ty_setting_shipping_company');
        $data['ty_setting_returning_address'] = $this->config->get('ty_setting_returning_address');
        $data['ty_setting_shipping_address'] = $this->config->get('ty_setting_shipping_address');
        $data['ty_setting_price_multiplier'] = $this->config->get('ty_setting_price_multiplier');


        $data['barkod_places'] = array('sku', 'mpn', 'upc', 'jan', 'ean', 'isbn');


        $kargo = '[{"id":1,"name":"Yurtiçi Kargo","code":"YK","taxNumber":"9860008925"},{"id":2,"name":"MNG Kargo","code":"MNG","taxNumber":"6080712084"},{"id":3,"name":"Aras Kargo","code":"ARAS","taxNumber":"0720039666"},{"id":4,"name":"Yurtiçi Kargo Marketplace","code":"YKMP","taxNumber":"3130557669"},{"id":5,"name":"Aynı Gün Teslimat","code":"AGT","taxNumber":"6090414309"},{"id":6,"name":"Horoz Kargo Marketplace","code":"HOROZMP","taxNumber":"4630097122"},{"id":7,"name":"Aras Kargo Marketplace","code":"ARASMP","taxNumber":"0720039666"},{"id":8,"name":"Yurtiçi Kargo International","code":"INTYK","taxNumber":"9860008925"},{"id":9,"name":"Sürat Kargo Marketplace","code":"SURATMP","taxNumber":"7870233582"},{"id":10,"name":"MNG Kargo Marketplace","code":"MNGMP","taxNumber":"6080712084"},{"id":11,"name":"Trendyol Lojistik","code":"TEX","taxNumber":"8590921777"},{"id":12,"name":"UPS Kargo Marketplace","code":"UPSMP","taxNumber":"9170014856"},{"id":13,"name":"AGT Marketplace","code":"AGTMP","taxNumber":"6090414309"},{"id":14,"name":"Cainiao Marketplace","code":"CAIMP","taxNumber":"0"},{"id":15,"name":"PTT Kargo International","code":"PTTINT","taxNumber":"7320068060"},{"id":16,"name":"Alternative Delivery","code":"ADEL","taxNumber":"0"},{"id":17,"name":"Trendyol Express Marketplace","code":"TEXMP","taxNumber":"8590921777"},{"id":18,"name":"B2CDirect","code":"B2C","taxNumber":"1270476364"},{"id":19,"name":"PTT Kargo Marketplace","code":"PTTMP","taxNumber":"7320068060"},{"id":20,"name":"CEVA Marketplace","code":"CEVAMP","taxNumber":"8450298557"},{"id":21,"name":"Alljoy Lojistics","code":"ALLJOY","taxNumber":"0551206401"},{"id":22,"name":"B2C Lojistics","code":"B2CL","taxNumber":"1270476364"},{"id":23,"name":"ARAMEX","code":"ARAMEX","taxNumber":"0710094760"},{"id":24,"name":"PTS","code":"PTS","taxNumber":"7190041528"},{"id":25,"name":"UPS Kargo","code":"UPS","taxNumber":"9170014856"},{"id":26,"name":"MIGROS","code":"MIGROS","taxNumber":"6220529513"},{"id":27,"name":"PTT","code":"PTT","taxNumber":"7320068060"},{"id":28,"name":"Trendyol Kurye","code":"TYKMP","taxNumber":"3131188775"},{"id":29,"name":"Aykargo","code":"AYK","taxNumber":"5700267300"},{"id":30,"name":"Borusan Lojistik Marketplace","code":"BORMP","taxNumber":"1800038254"},{"id":31,"name":"Surat Kargo","code":"SURAT","taxNumber":"7870233582"}]';
        $data['cargo_company'] = json_decode($kargo, 1);


        $post_data['request_data'] = array();
        $data['address'] = $this->entegrasyon->clientConnect($post_data, 'shipping_templates', 'ty');


        $this->load->model('catalog/information');
        $data['informations'] = $this->model_catalog_information->getInformations();


        $this->response->setOutput($this->load->view('entegrasyon/setting/mainsetting/ty', $data));


        //  print_r($data);

    }


    public function edit_hb()
    {


        if (($this->request->server['REQUEST_METHOD'] == 'POST')) {


            $this->load->model('setting/setting');
            $this->model_setting_setting->editSetting('hb_setting', $this->request->post);
            echo json_encode(array('status' => true));
            return;
        }

        $data['token_link'] = $this->token_data['token_link'];
        $data['hb_setting_commission'] = str_replace("%", "", $this->config->get('hb_setting_commission'));
        $data['hb_setting_shipping_time'] = $this->config->get('hb_setting_shipping_time');
        $data['hb_setting_maximum_order'] = $this->config->get('hb_setting_maximum_order');
        $data['hb_setting_product_attribute'] = $this->config->get('hb_setting_product_attribute');
        $data['hb_setting_kdv_setting'] = $this->config->get('hb_setting_kdv_setting');
        $data['hb_setting_additional_content'] = $this->config->get('hb_setting_additional_content');
        $data['hb_setting_product_special'] = $this->config->get('hb_setting_product_special');
        $data['hb_setting_shipping_price'] = $this->config->get('hb_setting_shipping_price');
        $data['hb_setting_model_prefix'] = $this->config->get('hb_setting_model_prefix');
        $data['hb_setting_variant'] = $this->config->get('hb_setting_variant');
        $data['hb_setting_update_value'] = $this->config->get('hb_setting_update_value');
        $data['hb_setting_barkod_place'] = $this->config->get('hb_setting_barkod_place');
        $data['hb_setting_add_tc'] = $this->config->get('hb_setting_add_tc');
        $data['hb_setting_product_category'] = $this->config->get('hb_setting_product_category');
        $data['hb_setting_product_iscurrency'] = $this->config->get('hb_setting_product_iscurrency');
        $data['hb_setting_oc_order'] = $this->config->get('hb_setting_oc_order');
        $data['hb_setting_customer_group'] = $this->config->get('hb_setting_customer_group');
        $data['hb_setting_auto_approve'] = $this->config->get('hb_setting_auto_approve');
        $data['barkod_places'] = array('sku', 'mpn', 'upc', 'jan', 'ean', 'isbn');
        $data['hb_setting_price_multiplier'] = $this->config->get('hb_setting_price_multiplier');


        $this->load->model('catalog/information');
        $data['informations'] = $this->model_catalog_information->getInformations();

        $this->response->setOutput($this->load->view('entegrasyon/setting/mainsetting/hepsiburada', $data));


    }

    public function edit_easy()
    {


        if (($this->request->server['REQUEST_METHOD'] == 'POST')) {

            $this->load->model('setting/setting');
            $this->model_setting_setting->editSetting('easy_setting', $this->request->post);
            echo json_encode(array('status' => true));
            return;
        }


        if (strstr($this->config->get('easy_setting_img_domain'), '//')) {
            $easy_setting_img_domain = $this->config->get('easy_setting_img_domain');
        } else {
            $easy_setting_img_domain = $this->config->get('easy_setting_img_domain') . "/";

        }
        $data['token_link'] = $this->token_data['token_link'];
        $data['easy_setting_commission'] = str_replace("%", "", $this->config->get('easy_setting_commission'));
        $data['easy_setting_shipping_time'] = $this->config->get('easy_setting_shipping_time');
        $data['easy_setting_notification'] = $this->config->get('easy_setting_notification');
        $data['easy_setting_email'] = $this->config->get('easy_setting_email');
        $data['easy_setting_img_domain'] = $easy_setting_img_domain;
        $data['easy_setting_maximum_order'] = $this->config->get('easy_setting_maximum_order');
        $data['easy_setting_product_attribute'] = $this->config->get('easy_setting_product_attribute');
        $data['easy_setting_kdv_setting'] = $this->config->get('easy_setting_kdv_setting');
        $data['easy_setting_additional_content'] = $this->config->get('easy_setting_additional_content');
        $data['easy_setting_product_special'] = $this->config->get('easy_setting_product_special');
        $data['easy_setting_shipping_price'] = $this->config->get('easy_setting_shipping_price');
        $data['easy_setting_model_prefix'] = $this->config->get('easy_setting_model_prefix');
        $data['easy_setting_variant'] = $this->config->get('easy_setting_variant');
        $data['easy_setting_barkod_place'] = $this->config->get('easy_setting_barkod_place');
        $data['easy_setting_product_category'] = $this->config->get('easy_setting_product_category');
        $data['easy_setting_oc_order'] = $this->config->get('easy_setting_oc_order');
        $data['easy_setting_customer_group'] = $this->config->get('easy_setting_customer_group');
        $data['easy_setting_update_after_oc_sale'] = $this->config->get('easy_setting_update_after_oc_sale');
        $data['easy_setting_order_price_with_tax'] = $this->config->get('easy_setting_order_price_with_tax');
        $data['easy_setting_price_place'] = $this->config->get('easy_setting_price_place');
        $data['easy_setting_add_after_shipping_price'] = $this->config->get('easy_setting_add_after_shipping_price');
        $data['easy_setting_invisible_image_for_bulk'] = $this->config->get('easy_setting_invisible_image_for_bulk');
        $data['easy_critical_stock'] = $this->config->get('easy_critical_stock');
        $data['easy_setting_shipping_logo'] = $this->config->get('easy_setting_shipping_logo');
        $data['easy_setting_auto_update_price'] = $this->config->get('easy_setting_auto_update_price');
        $data['easy_setting_update_after_market_sale'] = $this->config->get('easy_setting_update_after_market_sale');
        $data['easy_setting_list_price'] = $this->config->get('easy_setting_list_price');
        $data['easy_setting_resize_image'] = $this->config->get('easy_setting_resize_image');
        $data['easy_setting_sms_notification'] = $this->config->get('easy_setting_sms_notification');
        $data['easy_setting_marketplace_row_in_orders'] = $this->config->get('easy_setting_marketplace_row_in_orders');
        $data['easy_setting_sms_numbers'] = $this->config->get('easy_setting_sms_numbers');
        $data['easy_setting_critical_stock'] = $this->config->get('easy_setting_critical_stock');
        $data['customer_groups'] = $this->entegrasyon->getCustomerGroups();
        $data['price_places'] = array('sku', 'mpn', 'upc', 'jan', 'ean', 'isbn');
        $data['easy_setting_store_category'] = $this->config->get('easy_setting_store_category');


        $data['store_categories'] = array('Giyim', 'Halı Kilim & Perde', 'Mücevherat', 'Cinsel Ürünler', 'Kitap', 'Elektronik', 'Gıda', 'Hırdavat', 'Diğer');

        $this->load->model('catalog/information');
        $data['informations'] = $this->model_catalog_information->getInformations();

        $this->response->setOutput($this->load->view('entegrasyon/setting/mainsetting/easy', $data));


    }


    public function edit_eptt()
    {


        if (($this->request->server['REQUEST_METHOD'] == 'POST')) {


            $this->load->model('setting/setting');
            $this->model_setting_setting->editSetting('eptt_setting', $this->request->post);
            echo json_encode(array('status' => true));
            return;
        }

        $data['token_link'] = $this->token_data['token_link'];
        $data['eptt_setting_commission'] = str_replace("%", "", $this->config->get('eptt_setting_commission'));
        $data['eptt_setting_product_special'] = $this->config->get('eptt_setting_product_special');

        $data['eptt_setting_product_attribute'] = $this->config->get('eptt_setting_product_attribute');
        $data['eptt_setting_kdv_setting'] = $this->config->get('eptt_setting_kdv_setting');
        $data['eptt_setting_additional_content'] = $this->config->get('eptt_setting_additional_content');
        $data['eptt_setting_shipping_price'] = $this->config->get('eptt_setting_shipping_price');
        $data['eptt_setting_model_prefix'] = $this->config->get('eptt_setting_model_prefix');
        $data['eptt_setting_variant'] = $this->config->get('eptt_setting_variant');
        $data['eptt_setting_barkod_place'] = $this->config->get('eptt_setting_barkod_place');
        $data['eptt_setting_product_category'] = $this->config->get('eptt_setting_product_category');
        $data['eptt_setting_product_iscurrency'] = $this->config->get('eptt_setting_product_iscurrency');
        $data['eptt_setting_add_tc'] = $this->config->get('eptt_setting_add_tc');
        $data['eptt_setting_oc_order'] = $this->config->get('eptt_setting_oc_order');
        $data['barkod_places'] = array('sku', 'mpn', 'upc', 'jan', 'ean', 'isbn');
        $data['eptt_setting_customer_group'] = $this->config->get('eptt_setting_customer_group');
        $data['eptt_setting_price_multiplier'] = $this->config->get('eptt_setting_price_multiplier');

        $data['customer_groups'] = $this->entegrasyon->getCustomerGroups();
        $this->load->model('catalog/information');
        $data['informations'] = $this->model_catalog_information->getInformations();
        $this->response->setOutput($this->load->view('entegrasyon/setting/mainsetting/eptt', $data));


    }


    public function default_setting()
    {

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

        if (($this->request->server['REQUEST_METHOD'] == 'POST')) {

                $this->load->model('setting/setting');
                $this->model_setting_setting->editSetting($code.'_setting', $this->request->post);
                echo json_encode(array('status' => true, 'message' => 'Ayarlarınız kaydedilmiştir.'));

        } else {

            $this->load->model('setting/setting');
            $this->load->model('entegrasyon/setting');
            $data = $this->model_setting_setting->getSetting($code.'_setting');
            $market_setting=$this->model_entegrasyon_setting->{$code}();

            //print_r($data);
            $data=array_merge($data,$market_setting);
            $data['token_link'] = $this->token_data['token_link'];
            $this->response->setOutput($this->load->view('entegrasyon/setting/mainsetting/'.$code, $data));
        }
    }


    public function api_setting()
    {

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

        if (($this->request->server['REQUEST_METHOD'] == 'POST')) {


            $result = $this->{$code}->checkApi($this->request->post);

           

            if ($result['status']) {

                $this->load->model('tool/entegrasyon_api');

                $this->model_tool_entegrasyon_api->addEntegrasyonApi($code, $this->request->post);

                echo json_encode(array('status' => true, 'message' => 'Api Bilgileriniz Doğrulanmıştır.'));

            } else {

                echo json_encode(array('status' => false, 'message' => $result['message']));

            }

        } else {

            $this->load->model('tool/entegrasyon_api');
            $api_info = $this->model_tool_entegrasyon_api->getEntegrasyonApi($code);
            if($api_info){
                $data['api_info']=unserialize($api_info['api']);
            }else {
                $data['api_info']=array();
            }


            $data['token_link'] = $this->token_data['token_link'];
            $this->response->setOutput($this->load->view('entegrasyon/setting/api/.'.$code, $data));
        }
    }



    public function reset()
    {
        $this->load->model('tool/entegrasyon_api');
        $code = $this->request->get['code'];

        $this->model_tool_entegrasyon_api->deleteEntegrasyonApi($code);

        echo json_encode(array('status' => true));


    }


    public function error()
    {
        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');
        $data['token_link'] = $this->token_data['token_link'];
        $error = $this->request->get['error'];


        if ($error == 'no_api') {
            $heading_title = 'Aktif Pazaryeri Bulunamadı!';
            $data['error'] = 'Pazaryeri işlemleri yapabilmek için en az bir pazaryerini aktif hale getirmeniz gerekmektedir. Pazaryeri ayarlarınızı <strong>"Ayarlar"</strong> sayfasınından yapabilirsiniz.';
            $data['solution_link'] = array('title' => 'Ayarlar Sayfasına Git', 'url' => $this->url->link('entegrasyon/setting', $this->token_data['token_link'], true));
        } else if ($error == 'no_user') {
            $heading_title = 'Aktif Kullanıcı Hesabı Bulunamadı!';
            $data['error'] = 'Pazaryeri işlemleri yapabilmek için easyentegreye üye girişi yapmanız gerekmektedir. Henüz bir üye hesabınız yoksa ücretsiz üyelik oluşurabilirsiniz. Üyelik oluştuma ve Üye girişi işlemlerini <strong>"Ayarlar"</strong> sayfasınından yapabilirsiniz.';
            $data['solution_link'] = array('title' => 'Ayarlar Sayfasına Git', 'url' => $this->url->link('entegrasyon/setting', $this->token_data['token_link'], true));
        } else if ($error == 'no_module') {
            $heading_title = 'Pazaryeri Entegrasyon modülü aktif değil!';
            $data['error'] = 'Pazaryeri Entegrasyon sistemin sağlıklı çalışabilmesi için eklentiler/modüller bölümünden Pazaryeri Entegrasyon modülünü aktif etmeniz gerekmektdir.';

            if (VERSION >= 3) {

                $data['solution_link'] = array('title' => 'Modüller Sayfasına Git', 'url' => $this->url->link('marketplace/extension', $this->token_data['token_link'], true));


            } else {

                $data['solution_link'] = array('title' => 'Modüller Sayfasına Git', 'url' => $this->url->link('extension/extension', $this->token_data['token_link'], true));


            }


        }


        $data['heading_title'] = $heading_title;
        $this->document->setTitle($heading_title);
        $this->response->setOutput($this->load->view('entegrasyon/setting/error', $data));

    }


    private function runExecute($sql)
    {
        try {
            $this->db->query($sql);
        } catch (Exception $exception) {

            echo $exception->getMessage();
        }

    }

    public function optimize_all_images()
    {
        $this->load->model('catalog/product');
        $i = 0;
        $this->session->data['chanced_paths'] = array();

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

        foreach ($this->model_catalog_product->getProducts() as $product) {
            $result = $this->model_entegrasyon_tool->optimize_image($product['image'], 'main', $product['product_id']);
            $i++;
            $getProductImages = $this->entegrasyon->getProductImages($product['product_id']);
            foreach ($getProductImages as $productImage) {
                if ($productImage['image']) {
                    $result = $this->model_entegrasyon_tool->optimize_image($productImage['image'], 'product_image', $productImage['product_image_id']);
                    $i++;
                }
            }

        }


        echo json_encode(array('status' => true, 'message' => $i . ' adet Görsel Optimize Edildi!'));

    }

}