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

class Login_model extends CI_Model {


    public function __construct()
    {   
        parent::__construct();      
        $this->load->helper('encryption');
    }
  
    public function cek_user()
    {
        $username = $this->input->post('txtusername');
        $password = $this->input->post('txtpassword');
        $db = $this->load->database('default', TRUE);
        $query = $db->query("SELECT a.*,b.* FROM users a Left join roles b
                             ON a.role_id = b.role_id WHERE username='$username' AND a.active='Y'");
        $result = $query->result();
        if ($query->num_rows() == 1)
        {
           $data = array();
           foreach($result as $row)
           {
               $passworddecript = $row->password;
               if (decrypt($passworddecript) == $password) 
               {
                    $data = array('username' => $row->username, 
                             'login' => TRUE,
                             'full_name' => $row->full_name,
                             'role_id' => $row->role_id,
                             'aktif' => $row->active,
                    );
                    $this->session->set_userdata($data);
                    return TRUE;
                }

            }
            
           
        }
        else
        {
            return FALSE;
        }
    }

  
    public function logout()
    {
        $this->session->unset_userdata(array('username' => '', 'login' => FALSE));
        $this->session->sess_destroy();
    }
}
