<?php

class ControllerAccountAccount extends Controller
{
    public function index()
    {
        /* 
            $this->data['data']['configurations'][''] -> sintaxe necessária para manipulação dos dados em Vue 
            Uma alternativa secundaria caso necessária é alterar a controller para controllerbasebase e utilizar $this->dataVue
        */

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

            unset($this->session->data['success']);
        } else {
            $this->data['data']['configurations']['success'] = '';
        }
        
        $this->load->controller('common/footer');
        $commonFooter = new ControllerCommonFooter($this->registry);
        $this->data['informations'] = $commonFooter->getInformation();
        $this->data['contact'] = $commonFooter->getContact();

        $this->document->addScriptApp(array(
            'catalog/view/theme/appmobile/js/page.account.js'
        ));

        $this->document->addScript(array(
            'catalog/view/theme/vista/public/js/account/account.js',
            'public/js/menuAccount.js'
        ));

        $this->document->addTypesForJs(array(
            Type_Status::getTypeForJs(),
        ));

        $this->loadscripts();

        if (!$this->customer->isLogged()) {
            $this->session->data['redirect'] = $this->url->link('account/account', '', 'SSL');

            $this->redirect($this->url->link('account/register', '', 'SSL'));
        }

        $language = $this->language->load('account/account');
        $this->data = array_merge($this->data, $language);
        $this->load->type('tipocomum');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home', '', 'NONSSL', 'false'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_account'),
            'href' => $this->url->link('account/account', '', 'SSL'),
            'separator' => $this->language->get('text_separator')
        );

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

        $this->breadcrumbs->setbreadcumbs($this->data['breadcrumbs']);

        $this->data['edit'] = $this->url->link('account/edit', '', 'SSL');
        $this->data['password'] = $this->url->link('account/password', '', 'SSL');
        $this->data['address'] = $this->url->link('account/address', '', 'SSL');
        $this->data['delete'] = $this->url->link('account/delete', '', 'SSL');
        $this->data['interests'] = $this->url->link('account/interests', '', 'SSL');
        $this->data['store_rating_url'] = $this->url->link('account/store_rating', '', 'SSL');
        $this->data['wishlist'] = $this->url->link('account/wishlist');
        if ($this->config->get('listapresente_status')) {
            $this->data['listapresente'] = $this->url->link('listapresente/listapresente');
        }
        $this->data['fidelity_indication_status'] = $this->config->get('fidelity_status') && $this->config->get('indication_status')
            && ($this->config->get('indication_points') > 0);
        if ($this->data['fidelity_indication_status']) {
            $this->data['fidelity_indication_link'] = $this->url->link('fidelity/indication');
        }
        $this->data['order'] = $this->url->link('account/order', '', 'SSL');
        $this->data['download'] = $this->url->link('account/download', '', 'SSL');
        $this->data['return'] = $this->url->link('account/return', '', 'SSL');
        $this->data['transaction'] = $this->url->link('account/transaction', '', 'SSL');
        $this->data['newsletter'] = $this->url->link('account/newsletter', '', 'SSL');
        $this->data['newsletter_value'] = $this->customer->getNewsletter();
        $this->data['recurring'] = $this->url->link('account/recurring', '', 'SSL');
        $this->data['back'] = $this->url->link('account/account', '', 'SSL');

        $this->data['login_facebook'] = ($this->config->get('loginredesociais_facebook_status') == Type_Status::Tp_Habilitado) ? true : false;
        $this->data['facebook_id'] = $this->config->get('loginredesociais_facebook_id');
        $this->data['email_facebook_value'] = (strlen($this->customer->getEmailFacebook()) > 0) ? true : false;
        $this->data['facebook_user_id'] = (strlen($this->customer->getFacebookUserId()) > 0) ? true : false;

        $this->data['login_google'] = ($this->config->get('loginredesociais_google_status') == Type_Status::Tp_Habilitado) ? true : false;
        $this->data['google_id'] = $this->config->get('loginredesociais_google_id');
        $this->data['email_gmail_value'] = (strlen($this->customer->getEmailGmail()) > 0) ? true : false;
        $this->data['gmail_user_id'] = (strlen($this->customer->getGmailUserId()) > 0) ? true : false;

        /* COMECO - Habilita e Desabilita Lista de Desejo e Comparação - por Henrique R. Pohl */
        $this->data['futura_habilita_lista_desejos'] = ($this->config->get('wg24_futura_status_lista_desejos') == '') || ($this->config->get('wg24_futura_status_lista_desejos') == Type_Status::Tp_Habilitado);
        $this->data['futura_habilita_lista_comparacao'] = ($this->config->get('wg24_futura_status_lista_comparacao') == '') || ($this->config->get('wg24_futura_status_lista_comparacao') == Type_Status::Tp_Habilitado);
        /* FIM - Habilita e Desabilita Lista de Desejo e Comparação - por Henrique R. Pohl */

        $this->data['fidelity_indication_status'] = $this->config->get('fidelity_status') && $this->config->get('indication_status')
            && ($this->config->get('indication_points') > 0);

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

        if ($this->config->get('reward_status')) {
            $this->data['reward'] = $this->url->link('account/reward', '', 'SSL');
        } else {
            $this->data['reward'] = '';
        }

        $this->data['customer_interests_remember'] = ($this->config->get('fields_register_brazil_status_interests')) ? true : false;
        $this->data['listapresente_status'] = $this->config->get('listapresente_status');

        $this->data['logout'] = $this->url->link('account/logout', '', 'SSL'); //Usando no app

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/account.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/account/account.tpl';
        } else {
            $this->template = 'default/template/account/account.tpl';
        }

        $this->load->model('account/customer');
        $this->load->type('tipocomum');

        $customer_info = $this->model_account_customer->getCustomer($this->customer->getId());

        $this->data['data']['configurations']['customer_info'] = $customer_info;

        $this->data['pessoa_fisica'] = $this->customer->getCpf() && $this->customer->getCpf() != '' ? true : false;

        $arrayDataExibir = array(
            'fields_register_brazil_status_telefone',
            'fields_register_brazil_status_celular',
            'fields_register_brazil_status_telefone2',
            'fields_register_brazil_status_sexo',
            'fields_register_brazil_status_data_nascimento',
            'fields_register_brazil_status_rg',
            'fields_register_brazil_status_data_nascimento_required'
        );

        foreach ($arrayDataExibir as $dataexibir) {

            if (in_array($dataexibir, ['fields_register_brazil_status_telefone', 'fields_register_brazil_status_celular', 'fields_register_brazil_status_data_nascimento_required']) && $this->ftrgeralregistry->isApp()) {
                $this->data[$dataexibir] = false;
            } else {
                if (!ftr_geral::isIsset($this->config->get($dataexibir)) || $this->config->get($dataexibir)) {
                    $this->data[$dataexibir] = true;
                } else {
                    $this->data[$dataexibir] = false;
                }
            }

        }

        if (isset($this->request->post['contribuinte'])) {
            $this->data['contribuinte'] = $this->request->post['contribuinte'];
        } elseif (isset($customer_info)) {
            $this->data['contribuinte'] = $customer_info['contribuinte'];
        } else {
            $this->data['contribuinte'] = 'S';
        }

        if (isset($this->request->post['perfil'])) {
            $this->data['perfil'] = $this->request->post['perfil'];
        } elseif (isset($customer_info)) {
            $this->data['perfil'] = $customer_info['perfil'];
        } else {
            $this->data['perfil'] = '';
        }

        $perfis = Type_PerfilCustomerJuridico::getEnumlist();
        $this->data['perfis'] = array();
        foreach ($perfis as $key => $perfil) {
            $this->data['perfis'][] = array(
                'value' => $key,
                'name' => $perfil,
                'selected' => ((isset($this->request->post['perfil']) && ($this->request->post['perfil'] == $key)) || ($this->data['perfil'] == $key)) ? true : false
            );
        }

        $contribuintes = Type_PjContribuoIcms::getEnumlist();
        $this->data['contribuintes'] = array();
        foreach ($contribuintes as $key => $contribuinte) {
            $this->data['contribuintes'][] = array(
                'value' => $key,
                'name' => $contribuinte,
                'selected' => ((isset($this->request->post['contribuinte']) && ($this->request->post['contribuinte'] == $key)) || ($this->data['contribuinte'] == $key)) ? true : false
            );
        }

        $this->data['feedback_whatsapp'] = $this->feedback->isEnabled();
        $this->data['feedback_whatsapp_number'] = $this->customer->getFeedbackWhatsappNumber();
        $this->data['feedback_whatsapp_url'] = $this->url->link('feedback/feedback/authorization', '', 'SSL');
        $this->data['feedback_whatsapp_value'] = $this->customer->getFeedbackWhatsappStatus();

        $this->data['link_extract'] = $this->url->link('fidelity/account/extractlist');

        $this->children = array(
            'common/breadcrumbs',
            'common/column_left',
            'common/column_right',
            'common/content_top',
            'common/content_bottom',
            'common/footer',
            'common/header'
        );

        if (!$this->ftrgeralregistry->isApp()) {
            array_push($this->children, 'module/account');
        }

        $this->response->setOutput($this->render('account/account'));
    }

    public function loadscripts()
    {

        if ($this->config->get('loginredesociais_facebook_status')) {
            $this->document->addScript('catalog/view/theme/vista/public/js/facebook.js');
        }

        if ($this->config->get('loginredesociais_google_status')) {
            $this->document->addScript('https://apis.google.com/js/api:client.js', false);
            $this->document->addScript('catalog/view/theme/vista/public/js/google.js');
        }

    }

    public function deleteAccount(){
        $this->load->model('account/customer');
        $this->load->type('tipocomum');

        $customer_info = $this->model_account_customer->getCustomer($this->customer->getId());

        if($this->model_account_customer->deleteCustomer($this->customer->getId())){
            $this->response->setOutputJson(array('success'=> $customer_info));
        }else{
            $this->response->setOutputJson(array('error'=> $customer_info));
        }

    }
    public function unlink_social_media_access()
    {
        $result = false;
        if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
            if(isset($this->request->post['ajax']) and $this->request->post['ajax']){
                if(isset($this->request->post['redesocial']) and $this->request->post['redesocial']) {
                    $this->load->model('account/customer');

                    $customer_id =  $this->customer->getId();
                    $redesocial = $this->request->post['redesocial'];
                    $result = $this->customer->desvincularRedeSocial($redesocial, $customer_id);
                }
            }
        }

        $json = array();
        $json['result'] = $result;

        $this->response->setOutputJson($json);
    }

    public function setImageCustomer(){
        $this->load->model('account/customer');
        $img = $this->request->post['imgCostumer'];
        $this->model_account_customer->updateImgCustomer($this->customer->getId(), $img);
    }
}
