<?php 
defined('BASEPATH') OR exit('Ação não permitda');

class Auth extends CI_Controller {

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

        $this->load->model('Email_model', 'email_model');
    }

    public function index() {

        $this->load->view('restricted/auth/login');
    }

    public function create_account(){
        $data = array(
            'groups' => $this->ion_auth->groups()->result(),
        );
        $this->load->view('restricted/auth/create_account', $data);
    }

    public function register(){
        //Add user
        $this->form_validation->set_rules('first_name', 'Nome', 'trim|required|min_length[2]|max_length[45]');
        $this->form_validation->set_rules('last_name', 'Sobrenome', 'trim|required|min_length[2]|max_length[50]');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|min_length[4]|max_length[200]|valid_email|callback_valid_email');
        $this->form_validation->set_rules('phone', 'Telefone', 'trim|required|min_length[4]|max_length[30]');
        $this->form_validation->set_rules('password', 'Senha', 'trim|required|min_length[4]|max_length[200]');
        $this->form_validation->set_rules('confirm_password', 'Confirme a senha', 'required|min_length[4]|max_length[200]|matches[password]');

        if ($this->form_validation->run()) {
            $username = '';
            $password = $this->input->post('password');
            $email = $this->input->post('email');
            $additional_data = array(
                'first_name' => $this->input->post('first_name'),
                'last_name' => $this->input->post('last_name'),
                'phone' => $this->input->post('phone'),
            );
            $group = array($this->input->post('group'));

            if ($this->ion_auth->register($username, $password, $email, $additional_data, $group)) {
                $this->session->set_flashdata('success_message','.');
                $email_params = $this->input->post('first_name') . ' ' . $this->input->post('last_name');
                $this->email_model->send_email_welcome($email, $email_params);
                redirect('restricted/auth');
            } else {
                $this->session->set_flashdata('error_message', $this->ion_auth->erros());
                redirect('restricted/auth/create_account');
            }
        } else {
            //Validation error
            $data = array(
                'title' => "Criar conta",
                'groups' => $this->ion_auth->groups()->result(),
                'scripts' => array(
                    'js/mask/jquery.mask.min.js',
                    'js/mask/custom-mask.js',
                ),
            );
            $this->load->view('restricted/auth/create_account', $data);
        }
    }

    public function valid_email($email){
        $user_id = $this->input->post('user_id');
        if (!$user_id) {
            //Add
            if ($this->core_model->get_by_id('users', array('email' => $email))) {
                $this->form_validation->set_message('valid_email', 'Esse email já existe.');
                return false;
            } else {
                return true;
            }
        }
    }

    public function login(){

        $email = $this->input->post('email');
        $password = $this->input->post('password');
        $remember = ($this->input->post('remember' ? TRUE : FALSE));
        // var_dump($this->session->userdata()); die();
        if($this->ion_auth->login($email, $password, $remember)){

            // Get the user id through session
            $id = $this->session->user_id;
            // Verify if the user is in determinate group and redirect to their panel version
            if ($this->ion_auth->is_admin($id)){
                $this->session->set_flashdata('success_message', 'Seja vem vindo(a)!');
                redirect('restricted/admin/home');
            } else if($this->ion_auth->in_group('author')){
                $this->session->set_flashdata('success_message', 'Seja vem vindo(a)!');
                redirect('restricted/author/home');
            } else if($this->ion_auth->in_group('editor')){
                $this->session->set_flashdata('success_message', 'Seja vem vindo(a)!');
                redirect('restricted/editor/home');
            } else if($this->ion_auth->in_group('reviewer')){
                $this->session->set_flashdata('success_message', 'Seja vem vindo(a)!');
                redirect('restricted/reviewer/home');
            }

        } else {
            $this->session->set_flashdata('error_message', 'Verifique suas credenciais e tente novamente.');
            redirect('restricted/auth');
        }
    }

    public function logout(){
        $this->ion_auth->logout();
        redirect('restricted/auth');
    }
}