<?php 
class Login_model extends CI_Model{

	public function __construct(){
		$this->load->database();
		$this->load->helper('url');	
		$this->load->library('session');
		$this->load->helper('cookie');
	}	
	
	public function resSelectUser(){
		
		$username = $this->security->xss_clean($this->input->post('userName'));
        $password = $this->security->xss_clean($this->input->post('password'));
	
		$this->db->select('loginId,privelegeId,username,password');
		$this->db->from('login ');
		$this->db->where(array('username' => $username, 'password' => $password));
		$query = $this->db->get();
		/*$query = $this->db->query('SELECT loginId,privelegeId,username,password FROM login WHERE
		username = '.$this->db->escape($username).' AND password = '.$this->db->escape($password).' ');
		echo 'Total Results: ' . $query->num_rows();
		echo '<br>count is '.$query->num_rows.'dfgdf';*/
		//print_r($this->db->last_query());
		if($query->num_rows() == 1)
        {
			  // If there is a user, then create session data
            $row = $query->row();
            $data = array('loginId' => $row->loginId,'username' => $row->username,
                   		  'password' => $row->password,'privelegeId' => $row->privelegeId,
                   		  'companyGroupMasterId'=>1, 
				  'companyMasterId'=>1 
                   		 );
            $this->session->set_userdata($data);
            $cookie = array(
                    'name'   => 'user_data',
                    'value'  => json_encode($data),
                    'expire' => 86400, // 1 day (seconds)
                    'secure' => FALSE, // TRUE if using HTTPS
                    'httponly' => TRUE
            );
            $this->input->set_cookie($cookie);
            return true;
		}else
			;//echo '<br>inside else sdkcsldkcn';
		 return false;
	}

	public function selectUserByUsername($username)
	{
		$this->db->select('loginId, username');
		$this->db->from('login');
		$this->db->where('username', $username);
		$this->db->where('status', '1');
		$this->db->limit(1);
		$query = $this->db->get();
		return $query->num_rows() == 1 ? $query->row() : null;
	}

	public function updatePasswordByLoginId($loginId, $newPassword)
	{
		$this->db->set('password', $newPassword);
		$this->db->where('loginId', $loginId);
		return $this->db->update('login');
	}
}
?>