<?php

class ControllerCommonNav extends Controller
{
    protected function index()
    {
        $this->load->helper('ftr_geral');
        $this->load->type('tipocomum');

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

        $this->document->addScript(array(
            'view/public/js/common/nav.js',
        ));

        $this->data['showECommerceLinks'] = $this->config->get('config_sistema_ecommerce');
        $this->data['showMarketplaceLinks'] = $this->config->get('config_sistema_marketplace');

        // Novidades da versão
        $result_version = $this->model_setting_efutura->getVersion();
        if ($result_version) {
            $this->data['link_version'] = Type_LinkManual::getNickFromValue(Type_LinkManual::Tp_LinkManualVersion) . $result_version;
        }

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

            $is_Marketplace = array_map(function ($value) use ($route) {
                return in_array(strtolower($value), explode('/', $route)) ? true : false;
            }, Type_MarketPlace::getEnumList());

            if (array_filter($is_Marketplace)) {
                $this->data['current_page'] = 'marketplace';
                $this->data['current_subpage'] = strtolower(explode('/', $route)[1]);
                if ($this->data['current_subpage'] == 'product') {
                    $this->data['current_subpage'] = strtolower(explode('/', $route)[2]);
                    $this->data['current_subsubpage'] = 'product';
                } else {
                    $this->data['current_subsubpage'] = strtolower(explode('/', $route)[2]);
                }
            } else {
                switch (explode('/', $route)[0]) {
                    case 'sale':
                        $this->data['current_page'] = explode('/', $route)[0];
                        $this->data['current_subpage'] = explode('/', $route)[1];
                        if (in_array($this->data['current_subpage'], ['customer', 'customer_group', 'customer_ban_ip'])) {
                            $this->data['current_subpage'] = 'customer';
                            $this->data['current_subsubpage'] = explode('/', $route)[1];
                        }
                        break;
                    case 'payment':
                        if (explode('/', $route)[1] == 'pp_express') {
                            $this->data['current_page'] = 'sale';
                            $this->data['current_subpage'] = 'paypal';
                            if (isset(explode('/', $route)[2])) {
                                $this->data['current_subsubpage'] = explode('/', $route)[2];
                            } else {
                                $this->data['current_subsubpage'] = explode('/', $route)[1];
                            }
                        }

                        if (explode('/', $route)[1] == 'pagseguro') {
                            $this->data['current_page'] = 'extension';
                            $this->data['current_subpage'] = 'pagseguro';
                            if (isset(explode('/', $route)[2])) {
                                $this->data['current_subsubpage'] = explode('/', $route)[2];
                            } else {
                                $this->data['current_subsubpage'] = explode('/', $route)[1];
                            }
                        }

                        break;
                    case 'design':
                    case 'user':
                    case 'localisation':
                    case 'outros':
                    case 'tool':
                        $this->data['current_page'] = 'setting';
                        $this->data['current_subpage'] = explode('/', $route)[0];
                        $this->data['current_subsubpage'] = explode('/', $route)[1];

                        if (in_array($this->data['current_subsubpage'], ['return_status', 'return_action', 'return_reason'])) {
                            $this->data['current_subsubpagereturn'] = 'return';
                            $this->data['current_subsubpagesubreturn'] = $this->data['current_subsubpage'];
                        }

                        break;
                    case 'report':
                        $this->data['current_page'] = explode('/', $route)[0];
                        $this->data['current_subpage'] = explode('/', $route)[1];
                        if (in_array($this->data['current_subpage'], ['product_purchased', 'product_viewed'])) {
                            $this->data['current_subpage'] = 'product';
                            $this->data['current_subsubpage'] = explode('/', $route)[1];
                        }
                        break;
                    default:
                        if (explode('/', $route)[1] == 'pagseguro_abandoned') {
                            $this->data['current_page'] = 'extension';
                            $this->data['current_subpage'] = 'pagseguro';
                            if (isset(explode('/', $route)[2])) {
                                $this->data['current_subsubpage'] = explode('/', $route)[2];
                            } else {
                                $this->data['current_subsubpage'] = explode('/', $route)[1];
                            }
                        } elseif (explode('/', $route)[1] == 'task') {
                            $this->data['current_page'] = 'setting';
                            $this->data['current_subpage'] = explode('/', $route)[1];
                        } else {
                            $this->data['current_page'] = explode('/', $route)[0];
                            $this->data['current_subpage'] = explode('/', $route)[1];
                        }
                }
            }
        }

        if (isset($this->session->data['active_notifications'])) {
            $this->data['active_notifications'] = true;
            $this->data['quantity_notifications'] = $this->session->data['quantity_notifications'];
            unset($this->session->data['quantity_notifications']);
            unset($this->session->data['active_notifications']);
        } else {
            $this->data['active_notifications'] = false;
            $this->data['quantity_notifications'] = 0;
        }


        $language = $this->language->load('common/header');
        $this->data = array_merge($this->data, $language);


        //Marketplaces menu Inicio
        $this->data['tipo_menu_tab']['mkt'] = Type_TitleModules::Tp_Marketplace;

        /*COMEÇO: Menu para configurações mercado livre - por Dionis Mendanha*/
        $extensions = $this->model_setting_extension->getInstalledByCode('module', 'mercadolivre');
        $this->data['mercadolivre_instaled'] = Type_Status::Tp_Desabilitado;
        $this->data['accesToken'] = false;
        if ($extensions) {
            $this->data['mercadolivre_instaled'] = Type_Status::Tp_Habilitado;
            $this->data['text_mercadolivre_extension'] = $this->language->get('text_mercadolivre_extension');
            $this->data['text_mercadolivre_setting'] = $this->language->get('text_mercadolivre_setting');
            $this->data['text_mercadolivre_helper'] = $this->language->get('text_mercadolivre_helper');
            $this->data['text_search_order'] = $this->language->get('text_search_order');
            if (ftr_geral::isIsset($this->config->get('mercadolivre_access_token'))) {
                $this->data['accesToken'] = true;
            }
        }
        /*FIM: Menu para configurações mercado livre - por Dionis Mendanha*/

        /*COMEÇO: Menu para configurações Skyhub*/
        $extensions = $this->model_setting_extension->getInstalledByCode('module', 'skyhub');
        $this->data['skyhub_instaled'] = Type_Status::Tp_Desabilitado;
        if ($extensions) {
            $this->data['skyhub_instaled'] = Type_Status::Tp_Habilitado;
            $this->data['skyhub_status'] = $this->config->get('skyhub_status');
            $this->data['text_skyhub_setting'] = $this->language->get('text_skyhub_setting');
        }
        /*FIM: Menu para configurações Skyhub*/

        /*COMEÇO: Menu para configurações Galeria Page*/
        $extensions = $this->model_setting_extension->getInstalledByCode('module', 'galeriapage');
        $this->data['galeriapage_instaled'] = Type_Status::Tp_Desabilitado;
        if ($extensions) {
            $this->data['galeriapage_instaled'] = Type_Status::Tp_Habilitado;
            $this->data['text_galeriapage_setting'] = $this->language->get('text_setting');
        }
        /*FIM: Menu para configurações Galeria Page*/

        /*COMEÇO: Menu para configurações Shopee*/
        $extensions = $this->model_setting_extension->getInstalledByCode('module', 'shopee');
        $this->data['shopee_instaled'] = Type_Status::Tp_Desabilitado;
        if ($extensions) {
            $this->data['shopee_instaled'] = Type_Status::Tp_Habilitado;
            $this->data['shopee_status'] = $this->config->get('shopee_status');
            $this->data['text_shopee_setting'] = $this->language->get('text_setting');
        }
        /*FIM: Menu para configurações Shopee*/

        //Marketplaces menu Fim

        $this->data['store_rating_module_status'] = $this->config->get('store_rating_module_status');

        /*Inicio : Validar se o Módulo redirecionamento301 está instalado*/
        $moduleRedirecionamento301 = $this->model_setting_extension->getInstalledByCode('module', 'redirecionamento301');
        $this->data['recionamento301_instaled'] = Type_Status::Tp_Desabilitado;
        if ($moduleRedirecionamento301) {
            $this->data['recionamento301_instaled'] = Type_Status::Tp_Habilitado;
        }
        /*Fim : Validar se o Módulo redirecionamento301 está instalado*/

        /*Inicio : Validar se o Módulo mandae está instalado*/
        $moduleMandae = $this->model_setting_extension->getInstalledByCode('shipping', 'mandae');
        $this->data['mandae_instaled'] = Type_Status::Tp_Desabilitado;
        if ($moduleMandae) {
            $this->data['mandae_instaled'] = Type_Status::Tp_Habilitado;
        }
        /*Fim : Validar se o Módulo mandae está instalado*/

        $this->data['exibe_error_log'] = ($this->user->getUserGroup() == Type_UserGroup::Tp_Admin) ? true : false;
        $this->data['exibe_error_log_ml'] = $this->config->get('mercadolivre_debug');

        if (!$this->user->isLogged() || !isset($this->request->get['token']) || !isset($this->session->data['token']) || ($this->request->get['token'] != $this->session->data['token'])) {
            $this->data['logged'] = '';

            $this->data['home'] = $this->url->link('common/login', '', 'SSL');
        } else {
            $this->data['pagseguro_status'] = $this->config->get('pagseguro_status');

            $info_parametros = $this->db->query("SELECT idempresa FROM `parametros_efutura` WHERE 1 order by id desc limit 1")->row;

            $this->data['infos_empresa'] = $info_parametros['idempresa'] . " - " . substr($this->config->get('config_owner'), 0, 20);

            $this->data['token'] = $this->session->data['token'];

            $this->data['logged'] = sprintf($this->language->get('text_logged'), $this->user->getUserName());
            $this->data['pp_express_status'] = $this->config->get('pp_express_status');

            $this->data['home'] = $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL');

            if ($this->config->get('pagseguro_status')) {
                $this->data['text_pagseguro'] = $this->language->get('text_pagseguro');
                $this->data['text_pagseguro_order_abandoned'] = $this->language->get('text_pagseguro_order_abandoned');
                $this->data['text_pagseguro_settings'] = $this->language->get('text_pagseguro_settings');
                $this->data['pagseguro_order_abandoned'] = $this->url->link('module/pagseguro_abandoned', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Pagamento, 'SSL');
                $this->data['pagseguro_settings'] = $this->url->link('payment/pagseguro', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Pagamento, 'SSL');
            }

            $this->data['affiliate'] = $this->url->link('sale/affiliate', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['attribute'] = $this->url->link('catalog/attribute', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['attribute_group'] = $this->url->link('catalog/attribute_group', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['backup'] = $this->url->link('tool/backup', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['banner'] = $this->url->link('design/banner', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['colors'] = $this->url->link('design/colors', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['visual'] = $this->url->link('design/visual', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['custom_layout'] = $this->url->link('design/layout_personalizado', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['category'] = $this->url->link('catalog/category', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['country'] = $this->url->link('localisation/country', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['coupon'] = $this->url->link('sale/coupon', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['currency'] = $this->url->link('localisation/currency', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['customer'] = $this->url->link('sale/customer', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['customer_fields'] = $this->url->link('sale/customer_field', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['customer_group'] = $this->url->link('sale/customer_group', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['customer_ban_ip'] = $this->url->link('sale/customer_ban_ip', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['custom_field'] = $this->url->link('design/custom_field', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['download'] = $this->url->link('catalog/download', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['error_log'] = $this->url->link('tool/error_log', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['feed'] = $this->url->link('extension/feed', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['analyze'] = $this->url->link('setting/analyze', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['link_theme'] = $this->url->link(
                "common/nav/changeTheme",
                'token=' . $this->session->data['token'],
                'SSL'
            );

            /*COMEÇO: Menu para configurações mercado livre - por Dionis Mendanha*/
            $this->data['mercadolivre_link_product'] = $this->url->link('module/' . strtolower(Type_MarketPlace::getNickFromConstValue(Type_MarketPlace::Tp_Mercadolivre)) . '/product', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Marketplace, 'SSL');
            $this->data['mercadolivre_link_setting'] = $this->url->link('module/mercadolivre/settings', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Marketplace, 'SSL');
            $this->data['mercadolivre_link_search_order'] = $this->url->link('module/mercadolivre/searchorder', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Marketplace, 'SSL');
            $this->data['mercadolivre_link_error'] = $this->url->link('module/mercadolivre/error', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Marketplace, 'SSL');
            $this->data['mercadolivre_link_question'] = $this->url->link('module/mercadolivre/question', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Marketplace, 'SSL');
            $this->data['mercadolivre_link_noticies'] = $this->url->link('module/mercadolivre/noticies', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Marketplace, 'SSL');
            $this->data['mercadolivre_link_log'] = $this->url->link('tool/error_log_mercadolivre', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Marketplace, 'SSL');
            $this->data['mercadolivre_link_auth'] = $this->urlloja->link('', '', 'NONSSL', false) . 'mercadolivreauth';
            /*FIM: Menu para configurações mercado livre - por Dionis Mendanha*/

            /*COMEÇO: Menu para configurações skyhub*/
            $this->data['skyhub_link_product'] = $this->url->link('module/skyhub/product', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Marketplace, 'SSL');
            $this->data['skyhub_link_setting'] = $this->url->link('module/skyhub/settings', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Marketplace, 'SSL');
            $this->data['skyhub_link_searchorder'] = $this->url->link('module/skyhub/searchorder', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Marketplace, 'SSL');
            $this->data['skyhub_link_error'] = $this->url->link('module/skyhub/error', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Marketplace, 'SSL');
            /*FIM: Menu para configurações skyhub*/

            /*COMEÇO: Menu para configurações galeria page*/
            $this->data['galeriapage_link_setting'] = $this->url->link('module/galeriapage/settings', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Marketplace, 'SSL');
            $this->data['galeriapage_link_product'] = $this->url->link('module/galeriapage/product', ' token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Marketplace . '&filter_barcod=' . Type_DsimDnao::Tp_Sim, 'SSL');
            /*FIM: Menu para configurações galeria page*/

            /*COMEÇO: Menu para configurações shopee*/
            $this->data['shopee_link_setting'] = $this->url->link('module/shopee/settings', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Marketplace, 'SSL');
            $this->data['shopee_link_product'] = $this->url->link('module/shopee/product',  'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Marketplace, 'SSL');
            $this->data['shopee_link_searchorder'] = $this->url->link('module/shopee/searchorder',  'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Marketplace, 'SSL');
            /*FIM: Menu para configurações shopee*/

            $this->data['filter'] = $this->url->link('catalog/filter', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['geo_zone'] = $this->url->link('localisation/geo_zone', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['information'] = $this->url->link('catalog/information', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['layout'] = $this->url->link('design/layout', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['contact'] = $this->url->link('sale/contact', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['manager'] = $this->url->link('extension/manager', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['manufacturer'] = $this->url->link('catalog/manufacturer', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['module'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['option'] = $this->url->link('catalog/option', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['order'] = $this->url->link('sale/order', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['order_status'] = $this->url->link('localisation/order_status', 'token=' . $this->session->data['token'], 'SSL');

            $this->data['processo_venda_instaled'] = $this->model_setting_extension->getInstalledByCode('module', 'processo_venda');
            $this->data['processo_venda'] = $this->url->link('localisation/processo_venda', 'token=' . $this->session->data['token'], 'SSL');

            $this->data['payment'] = $this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['product'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['profile'] = $this->url->link('catalog/profile', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['report_sale_order'] = $this->url->link('report/sale_order', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['report_coupon_used'] = $this->url->link('report/coupon_used', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['report_sale_tax'] = $this->url->link('report/sale_tax', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['report_sale_return'] = $this->url->link('report/sale_return', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['report_product_viewed'] = $this->url->link('report/product_viewed', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['report_searchterms'] = $this->url->link('report/searchterms', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['report_product_purchased'] = $this->url->link('report/product_purchased', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['report_customer_online'] = $this->url->link('report/customer_online', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['report_customer_order'] = $this->url->link('report/customer_order', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['report_affiliate_commission'] = $this->url->link('report/affiliate_commission', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['review'] = $this->url->link('catalog/review', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['store_rating'] = $this->url->link('catalog/store_rating', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['return'] = $this->url->link('sale/return', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['return_action'] = $this->url->link('localisation/return_action', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['return_reason'] = $this->url->link('localisation/return_reason', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['return_status'] = $this->url->link('localisation/return_status', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['shipping'] = $this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['setting'] = $this->url->link('setting/setting', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['task'] = $this->url->link('module/task', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Config, 'SSL');
            $this->data['store'] = $this->urlloja->link('', '', 'NONSSL', false);
            $this->data['stock_status'] = $this->url->link('localisation/stock_status', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['total'] = $this->url->link('extension/total', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['user'] = $this->url->link('user/user', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['user_group'] = $this->url->link('user/user_permission', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['redirecionamento301'] = $this->url->link('outros/redirecionamento301', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Outros, 'SSL');
            $this->data['message_status'] = $this->url->link('outros/message_status', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['mandae'] = $this->url->link('shipping/mandae', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Envio, 'SSL');
            $this->data['voucher'] = $this->url->link('sale/voucher', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['voucher_theme'] = $this->url->link('sale/voucher_theme', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['weight_class'] = $this->url->link('localisation/weight_class', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['length_class'] = $this->url->link('localisation/length_class', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['zone'] = $this->url->link('localisation/zone', 'token=' . $this->session->data['token'], 'SSL');
            $this->data['text_theme'] =
                $this->user->getTheme()
                    ? Type_Appearance::getNickFromValue(Type_Appearance::Tp_Light)['text']
                    : Type_Appearance::getNickFromValue(Type_Appearance::Tp_Dark)['text'];

            $this->data['paypal_express'] = $this->url->link('payment/pp_express', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Pagamento, 'SSL');
            $this->data['paypal_express_search'] = $this->url->link('payment/pp_express/search', 'token=' . $this->session->data['token'] . '&type=' . Type_TitleModules::Tp_Pagamento, 'SSL');
        }

        $this->render('common/nav');
    }

    public function changeTheme()
    {
        $this->load->model('user/user');

        $appearance = empty($this->user->getTheme()) ? Type_Appearance::Tp_Dark : '';
        $text =
            empty($this->user->getTheme())
                ? Type_Appearance::getNickFromValue(Type_Appearance::Tp_Light)['text']
                : Type_Appearance::getNickFromValue(Type_Appearance::Tp_Dark)['text'];

        $this->model_user_user->editTheme($this->user->getId(), $appearance);

        $this->response->setOutputJson([
            'appearance' => $appearance,
            'text' => $text
        ]);
    }
}
