<?php

class Site extends MY_Controller {

    public $error_message = '';

    function __construct() {
        parent::__construct();
    }

    function index() {
        
    }

    function change_lang($lang) {

        $lang = ($lang != "") ? $lang : "portuguese";
        $this->session->set_userdata('lang', $lang);
        redirect(base_url());
    }

    function admin_login() {

        $this->load->model('user_model');
        $user_data = $this->user_model->get_user_by_credentials($this->input->post('email'), $this->input->post('password'));
        if ($user_data->num_rows > 0) {

            //do login
            $data = array(
                'is_logged_in_impa' => true
            );
            $this->session->set_userdata($data);

            redirect('backOffice');
        } else {

            parent::renderPage("admin_view", array('error_message' => 'Login incorrecto'));
        }
    }

    public function logout() {
        $this->session->sess_destroy();
        redirect();
    }

    function show_backoffice() {
        parent::renderPage("admin_view", array('error_message' => ''));
    }

    function show_aafb() {
        $this->title = $this->lang->line('A_AAFB');
        parent::renderPage("aafb_view");
    }

    function show_atuacao() {
        $this->title = $this->lang->line('atuacao');
        parent::renderPage("atuacao_view");
    }

    function show_quadros() {
        $this->title = $this->lang->line('quadros');
        parent::renderPage("quadros_view");
    }

    function show_servicos() {
        $this->title = $this->lang->line('servicos');
        parent::renderPage("servicos_view");
    }

    
    function show_clientes() {
        $this->title = $this->lang->line('clientes');
        parent::renderPage("clientes_view");
    }
    
    function show_info_institucional() {
        $this->title = $this->lang->line('info_institucional');
        parent::renderPage("info_institucional_view");
    }

    function show_404() {
        $this->title = $this->lang->line('Economistas-consultores');
        parent::renderPage("404_view");
    }

    function subscrever_newsletter() {

        $this->load->model('subscricao_newsletter_model');
        $subscricao_data = $this->subscricao_newsletter_model->get_subscricao($this->input->post('email'));
        if ($subscricao_data->num_rows > 0) {
            echo json_encode(
                    array(
                        'error' => 'exists'
                    )
            );
            return false;
        } else {

            $this->subscricao_newsletter_model->add_subscricao($this->input->post());

            echo json_encode(
                    array(
                        'error' => ''
                    )
            );
            return false;
        }
    }

}

