<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/userguide3/general/urls.html
	 */
	public function index()
	{

		if($this->session->userdata('id') !='') {
			redirect('dashboard/dashboard');
		}
		$this->load->view('login');
	}
	
	public function secureLogin()
    {   
		$user_email = $_POST['user_email'];
		echo "input --".$pass = md5($_POST['user_password']);
		$sql="select * from users where email_id='$user_email'";    
		$query = $this->db->query($sql);
		$q = $query->result_array();
		
		if(empty($q)) {;
			$this->session->set_flashdata('login_msg','User not found in system.');
			redirect('login');
			exit;
		} else {
			echo 'db --'.$q[0]['password'];
			if ($pass != $q[0]['password']) {
				$this->session->set_flashdata('login_msg', 'Incorrect password.');
				redirect('login');
				exit;
			} 
			else {
				// LOGIN SUCCESS
				$this->session->set_userdata('id',$q[0]['id']);
				$this->session->set_userdata('email_id',$q[0]['email_id']);
				$this->session->set_userdata('full_name',$q[0]['first_name'].' '.$q[0]['last_name']);
				$this->session->set_userdata('parent_id',$q[0]['parent_id']);
				$this->session->set_userdata('role_id',$q[0]['role_id']);
			}
		}
		$emp_id = $this->session->userdata('id');
		$date = date('Y-m-d h:i:s');
		
		//$sql1="insert into log_table (description,action, created_time, user_id) values('Login Successful','Login Started','".$date."','".$emp_id."')";
		//$query1 = $this->db->query($sql1);
		redirect('dashboard/dashboard');
		exit;

    }

    // Logout function
	public function logout()
    {
        //$session = session();
        $this->session->set_userdata('id','');
		$this->session->set_userdata('email','');
		$this->session->set_userdata('mobile_number','');
		$this->session->set_userdata('full_name','');
		//$this->session->sess_destroy();
		redirect('login');
        
    }
	public function signUp() {
		$this->load->view('signup');
	}
	public function saveUser() {
		$first_name = $_POST['first_name'];
		$last_name = $_POST['last_name'];
		$email_id = $_POST['email_id'];
		$password = md5($_POST['password']);
		$date = date('Y-m-d');
		$sql="insert into users (first_name, last_name, email_id, status, parent_id, created_at, password, verified, token) values('$first_name','$last_name','$email_id','1','1','$date','$password','0','$password')";    
		$query = $this->db->query($sql);
		
		$select = "select * from users where email_id='$email_id'";
		$select_query = $this->db->query($select);
		$q = $select_query->result_array();
		
		$this->session->set_userdata('id',$q[0]['id']);
		$this->session->set_userdata('email_id',$q[0]['email_id']);
		$this->session->set_userdata('full_name',$q[0]['first_name'].' '.$q[0]['last_name']);
		$this->session->set_userdata('parent_id',$q[0]['parent_id']);
		
		redirect('dashboard');
		
	}

}
