<?php

class ControllerModuleFieldsRegisterBrazil extends Controller
{
    private $error = array();

    public function index()
    {
        $language = $this->load->language('module/fields_register_brazil');
        $this->data = array_merge($this->data, $language);

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

        $this->load->model('setting/setting');
        $this->load->helper('ftr_geral');
        $this->load->type('tipocomum');

        $this->document->addScript('admin/public/js/module/fields-register-brazil.js');
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {

            $this->model_setting_setting->editSetting('fields_register_brazil', $this->request->post);
            $this->session->data['success'] = $this->language->get('text_success');

            $this->redirect($this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL'));
        }

        $this->data['ftr_geral'] = new ftr_geral();
        $this->data['Type_Status'] = $this->load->typeForTwig('Type_Status');
        $this->data['Type_CadastroPessoa'] = $this->load->typeForTwig('Type_CadastroPessoa');

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

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

        if (isset($this->error['data_nascimento'])) {
            $this->data['error_data_nascimento'] = $this->error['data_nascimento'];
        } else {
            $this->data['error_data_nascimento'] = '';
        }

        if (isset($this->error['fields_register_brazil_status_sexo'])) {
            $this->data['error_fields_register_brazil_status_sexo'] = $this->error['fields_register_brazil_status_sexo'];
        } else {
            $this->data['error_fields_register_brazil_status_sexo'] = '';
        }

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

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_module'),
            'href' => $this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('module/fields_register_brazil', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['action'] = $this->url->link('module/fields_register_brazil', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL');
        $this->data['cancel'] = $this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL');


        if (isset($this->request->post['fields_register_brazil_tipo_de_pessoa'])) {
            $this->data['fields_register_brazil_tipo_de_pessoa'] = $this->request->post['fields_register_brazil_tipo_de_pessoa'];
        } else {
            $this->data['fields_register_brazil_tipo_de_pessoa'] = $this->config->get('fields_register_brazil_tipo_de_pessoa');
        }


        if (isset($this->request->post['fields_register_brazil_status_telefone'])) {
            $this->data['fields_register_brazil_status_telefone'] = $this->request->post['fields_register_brazil_status_telefone'];
        } else {
            if (!ftr_geral::isIsset($this->config->get('fields_register_brazil_status_telefone'))) {
                $this->data['fields_register_brazil_status_telefone'] = Type_Status::Tp_Habilitado;
            } else {
                $this->data['fields_register_brazil_status_telefone'] = $this->config->get('fields_register_brazil_status_telefone');
            }
        }


        if (isset($this->request->post['fields_register_brazil_status_telefone2'])) {
            $this->data['fields_register_brazil_status_telefone2'] = $this->request->post['fields_register_brazil_status_telefone2'];
        } else {
            $this->data['fields_register_brazil_status_telefone2'] = $this->config->get('fields_register_brazil_status_telefone2');
            if (!isset($this->data['fields_register_brazil_status_telefone2'])) {
                $this->data['fields_register_brazil_status_telefone2'] = Type_Status::Tp_Habilitado;
            }
        }

        if (isset($this->request->post['fields_register_brazil_status_celular'])) {
            $this->data['fields_register_brazil_status_celular'] = $this->request->post['fields_register_brazil_status_celular'];
        } else {
            $this->data['fields_register_brazil_status_celular'] = $this->config->get('fields_register_brazil_status_celular');
            if (!isset($this->data['fields_register_brazil_status_celular'])) {
                $this->data['fields_register_brazil_status_celular'] = Type_Status::Tp_Habilitado;
            }
        }

        if (isset($this->request->post['fields_register_brazil_status_cnpgcpf'])) {
            $this->data['fields_register_brazil_status_cnpgcpf'] = $this->request->post['fields_register_brazil_status_cnpgcpf'];
        } else {
            $this->data['fields_register_brazil_status_cnpgcpf'] = $this->config->get('fields_register_brazil_status_cnpgcpf');
            if (!isset($this->data['fields_register_brazil_status_cnpgcpf'])) {
                $this->data['fields_register_brazil_status_cnpgcpf'] = Type_Status::Tp_Habilitado;
            }
        }

        if (isset($this->request->post['fields_register_brazil_status_sexo'])) {
            $this->data['fields_register_brazil_status_sexo'] = $this->request->post['fields_register_brazil_status_sexo'];
        } else {
            $this->data['fields_register_brazil_status_sexo'] = $this->config->get('fields_register_brazil_status_sexo');
            if (!isset($this->data['fields_register_brazil_status_sexo'])) {
                $this->data['fields_register_brazil_status_sexo'] = Type_Status::Tp_Habilitado;
            }
        }

        if (isset($this->request->post['fields_register_brazil_status_data_nascimento'])) {
            $this->data['fields_register_brazil_status_data_nascimento'] = $this->request->post['fields_register_brazil_status_data_nascimento'];
        } else {
            $this->data['fields_register_brazil_status_data_nascimento'] = $this->config->get('fields_register_brazil_status_data_nascimento');
            if (!isset($this->data['fields_register_brazil_status_data_nascimento'])) {
                $this->data['fields_register_brazil_status_data_nascimento'] = Type_Status::Tp_Habilitado;
            }
        }

        if (isset($this->request->post['fields_register_brazil_status_data_nascimento_required'])) {
            $this->data['fields_register_brazil_status_data_nascimento_required'] = $this->request->post['fields_register_brazil_status_data_nascimento_required'];
        } else {
            $this->data['fields_register_brazil_status_data_nascimento_required'] = $this->config->get('fields_register_brazil_status_data_nascimento_required');
            if (!isset($this->data['fields_register_brazil_status_data_nascimento_required'])) {
                $this->data['fields_register_brazil_status_data_nascimento_required'] = Type_Status::Tp_Habilitado;
            }
        }


        if (isset($this->request->post['fields_register_brazil_status_rg'])) {
            $this->data['fields_register_brazil_status_rg'] = $this->request->post['fields_register_brazil_status_rg'];
        } else {
            $this->data['fields_register_brazil_status_rg'] = $this->config->get('fields_register_brazil_status_rg');
            if (!isset($this->data['fields_register_brazil_status_rg'])) {
                $this->data['fields_register_brazil_status_rg'] = Type_Status::Tp_Habilitado;
            }
        }

        if (isset($this->request->post['fields_register_brazil_status_interests'])) {
            $this->data['fields_register_brazil_status_interests'] = $this->request->post['fields_register_brazil_status_interests'];
        } elseif ($this->config->get('fields_register_brazil_status_interests')) {
            $this->data['fields_register_brazil_status_interests'] = $this->config->get('fields_register_brazil_status_interests');
        } else {
            $this->data['fields_register_brazil_status_interests'] = Type_Status::Tp_Desabilitado;
        }

        if (isset($this->request->post['fields_register_brazil_status_interests_for_old_users'])) {
            $this->data['fields_register_brazil_status_interests_for_old_users'] = $this->request->post['fields_register_brazil_status_interests_for_old_users'];
        } elseif ($this->config->get('fields_register_brazil_status_interests_for_old_users')) {
            $this->data['fields_register_brazil_status_interests_for_old_users'] = $this->config->get('fields_register_brazil_status_interests_for_old_users');
        } else {
            $this->data['fields_register_brazil_status_interests_for_old_users'] = Type_Status::Tp_Desabilitado;
        }

        if (isset($this->request->post['fields_register_brazil_status_interests_subcategory'])) {
            $this->data['fields_register_brazil_status_interests_subcategory'] = $this->request->post['fields_register_brazil_status_interests_subcategory'];
        } elseif ($this->config->get('fields_register_brazil_status_interests_subcategory')) {
            $this->data['fields_register_brazil_status_interests_subcategory'] = $this->config->get('fields_register_brazil_status_interests_subcategory');
        } else {
            $this->data['fields_register_brazil_status_interests_subcategory'] = Type_Status::Tp_Desabilitado;
        }

        if (isset($this->request->post['fields_register_brazil_status_perfil_pj'])) {
            $this->data['fields_register_brazil_status_perfil_pj'] = $this->request->post['fields_register_brazil_status_perfil_pj'];
        } elseif ($this->config->get('fields_register_brazil_status_perfil_pj')) {
            $this->data['fields_register_brazil_status_perfil_pj'] = $this->config->get('fields_register_brazil_status_perfil_pj');
        } else {
            $this->data['fields_register_brazil_status_perfil_pj'] = Type_Status::Tp_Desabilitado;
        }

        if (isset($this->request->post['fields_register_brazil_status_contribuinte_pj'])) {
            $this->data['fields_register_brazil_status_contribuinte_pj'] = $this->request->post['fields_register_brazil_status_contribuinte_pj'];
        } elseif ($this->config->get('fields_register_brazil_status_contribuinte_pj')) {
            $this->data['fields_register_brazil_status_contribuinte_pj'] = $this->config->get('fields_register_brazil_status_contribuinte_pj');
        } else {
            $this->data['fields_register_brazil_status_contribuinte_pj'] = Type_Status::Tp_Desabilitado;
        }

        if (isset($this->request->post['fields_register_brazil_status_ie_pj'])) {
            $this->data['fields_register_brazil_status_ie_pj'] = $this->request->post['fields_register_brazil_status_ie_pj'];
        } elseif ($this->config->get('fields_register_brazil_status_ie_pj')) {
            $this->data['fields_register_brazil_status_ie_pj'] = $this->config->get('fields_register_brazil_status_ie_pj');
        } else {
            $this->data['fields_register_brazil_status_ie_pj'] = Type_Status::Tp_Desabilitado;
        }

        $this->children = array(
            'common/header',
            'common/footer',
        );

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

    public function install()
    {
        $query = $this->db->query("ALTER TABLE `" . DB_PREFIX . "customer`
		ADD `cpf` VARCHAR( 14 ) NOT NULL AFTER `fax` ,
		add `celular` VARCHAR( 32 )NOT NULL after `cpf` ,
		ADD `cnpj` VARCHAR( 18 ) NOT NULL AFTER `celular` ,
		ADD `rg` VARCHAR( 20 ) NOT NULL AFTER `cnpj` ,
		ADD `inscricao_estadual` VARCHAR( 200 ) NOT NULL AFTER `rg` ,
		ADD `contribuinte` CHAR( 1 ) NOT NULL AFTER `inscricao_estadual` ,
		ADD `perfil` int(1) NOT NULL AFTER `contribuinte` ,
		ADD `razao_social` VARCHAR( 200 ) NOT NULL AFTER `perfil` ,
		ADD `data_nascimento` DATE NOT NULL AFTER `inscricao_estadual`,
		ADD `sexo` CHAR( 1 ) NOT NULL AFTER `data_nascimento`;
		");
        $query = $this->db->query("ALTER TABLE `" . DB_PREFIX . "address`
		ADD `apelido` VARCHAR( 128 ) NOT NULL AFTER `lastname`,
		ADD `numero` VARCHAR( 14 ) NOT NULL AFTER `address_1`,
		ADD `complemento` VARCHAR( 18 ) NOT NULL AFTER `address_2`;
		");
        $query = $this->db->query("ALTER TABLE `" . DB_PREFIX . "order`
		ADD `cpf` VARCHAR( 14 ) NOT NULL AFTER `fax` ,
		ADD `cnpj` VARCHAR( 18 ) NOT NULL AFTER `cpf` ,
		ADD `rg` VARCHAR( 20 ) NOT NULL AFTER `cnpj` ,
		ADD `inscricao_estadual` VARCHAR( 200 ) NOT NULL AFTER `rg` ,
		ADD `razao_social` VARCHAR( 200 ) NOT NULL AFTER `inscricao_estadual` ,
		ADD `data_nascimento` DATE NOT NULL AFTER `inscricao_estadual`,
		ADD `sexo` CHAR( 1 ) NOT NULL AFTER `data_nascimento`,
		ADD `payment_apelido` VARCHAR( 128 ) NOT NULL AFTER `sexo`,
		ADD `payment_numero` VARCHAR( 14 ) NOT NULL AFTER `payment_address_1`,
		ADD `payment_complemento` VARCHAR( 18 ) NOT NULL AFTER `payment_address_2`,
		ADD `shipping_apelido` VARCHAR( 128 ) NOT NULL AFTER `payment_code`,
		ADD `shipping_numero` VARCHAR( 14 ) NOT NULL AFTER `shipping_address_1`,
		ADD `shipping_complemento` VARCHAR( 18 ) NOT NULL AFTER `shipping_address_2`;
		");
    }

    public function uninstall()
    {
        $this->load->model('setting/setting');
        $this->model_setting_setting->editSetting('fields_register_brazil', array('fields_register_brazil_status' => '0'));
    }

    private function validate()
    {
        if (!$this->user->hasPermission('modify', 'module/fields_register_brazil')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        if (
            $this->request->post['fields_register_brazil_tipo_de_pessoa'] == Type_CadastroPessoa::Tp_Juridico
            && $this->request->post['fields_register_brazil_status_sexo'] == Type_Status::Tp_Habilitado
        ) {
            $this->error['fields_register_brazil_status_sexo'] = $this->language->get('error_status_sexo');
            $this->error['warning'] = $this->language->get('error_warning');
        }

        if (isset($this->request->post['fields_register_brazil_status_data_nascimento_required']) && ($this->request->post['fields_register_brazil_status_data_nascimento_required'])) {
            if (isset($this->request->post['fields_register_brazil_status_data_nascimento']) && (!$this->request->post['fields_register_brazil_status_data_nascimento'])) {
                $this->error['data_nascimento'] = $this->language->get('error_data_nascimento');
                $this->error['warning'] = $this->language->get('error_warning');
            }
        }

        if(isset($this->request->post['fields_register_brazil_status_telefone']) && ($this->request->post['fields_register_brazil_status_telefone'] == Type_Status::Tp_Desabilitado) &&
          isset($this->request->post['fields_register_brazil_status_celular']) && ($this->request->post['fields_register_brazil_status_celular'] == Type_Status::Tp_Desabilitado)){
            $this->error['warning'] = $this->language->get('error_warning_telephone');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }

}
