<?php

class User extends CI_Controller {

public function __construct(){

        parent::__construct();
  			$this->load->helper('url');
  	 		$this->load->model('user_model');
        $this->load->library('session');

}

public function index()
{
$this->load->view("register.php");
}

public function register_user(){

      $user=array(
      'nome_usuario'=>$this->input->post('user_name'),
      'email_usuario'=>$this->input->post('user_email'),
      'senha_usuario'=>md5($this->input->post('user_password')),
      //'user_age'=>$this->input->post('user_age'),
      'nivel_usuario'=> '1',
      'id_permissoes'=> '1',


      'celular_usuario'=>$this->input->post('user_mobile')
        );
        print_r($user);

$email_check=$this->user_model->email_check($user['email_usuario']);

if($email_check){
  $this->user_model->register_user($user);
  $this->session->set_flashdata('success_msg', 'Registered successfully.Now login to your account.');
  redirect('user/login_view');

}
else{

  $this->session->set_flashdata('error_msg', 'Error occured,Try again.');
  redirect('user');


}

}

public function login_view(){

  $this->load->view("login.php");

}

function login_user(){
  $user_login=array(
    'email_usuario'=>$this->input->post('user_email'),
    'senha_usuario'=>md5($this->input->post('user_password'))
  );
  $data=$this->user_model->login_user($user_login['email_usuario'],$user_login['senha_usuario']);

//var_dump($data);
//echo $user_login['email_usuario'];
//die();
  
  //if($data)
  //    {
        $this->session->set_userdata('user_id',$data['user_id']);
        $this->session->set_userdata('user_email',$data['user_email']);
        $this->session->set_userdata('user_name',$data['user_name']);
        $this->session->set_userdata('user_age',$data['user_age']);
        $this->session->set_userdata('user_mobile',$data['user_mobile']);

        $this->load->view('user_profile.php');

/*      }
      else{
        $this->session->set_flashdata('error_msg', 'Error occured,Try again.');
        $this->load->view("login.php");

      }
*/

}

function user_profile(){

$this->load->view('user_profile.php');

}
public function user_logout(){

  $this->session->sess_destroy();
  redirect('user/login_view', 'refresh');
}

}

?>
