<?php
class User_model extends CI_Model {
	
    function GetUserById($id)
    {
        $db    = $this->load->database('default', TRUE);
        $this->db->select('*');
        $this->db->from('users');
        $this->db->where('username',$id); 
        $query = $this->db->get();
        return $query->result();
    } 

    function GetUserRowById($id)
    {
        $db    = $this->load->database('default', TRUE);
        $this->db->select('*');
        $this->db->from('users');
        $this->db->where('username',$id); 
        $query = $this->db->get();
        return $query->row();
    } 

    function alluser_count()
    {   
        $db = $this->load->database('default', TRUE);
        $query = $db->get('users');
        return $query->num_rows();  
    }

    function alluser($limit,$start,$col,$dir)
    {   
        $db = $this->load->database('default', TRUE);
       
        $this->db->select('*');
        $this->db->from('users a'); 
        $this->db->join('roles b', 'b.role_id=a.role_id', 'inner');
        $this->db->limit($limit,$start);
        $this->db->order_by($col,$dir);
        $query = $this->db->get(); 
        if($query->num_rows()>0)
        {
            return $query->result(); 
        }
        else
        {
            return null;
        }
        
    }

    function user_search($limit,$start,$search,$col,$dir)
    {
        $db = $this->load->database('default', TRUE);

        $sql="SELECT A.username, A.full_name, A.email,B.roleName
                                  FROM users A INNER JOIN roles B 
                                  ON A.role_id = B.role_id WHERE A.username 
        like '%$search%' OR  A.full_name like '%$search%' 
        OR A.email like '%$search%'  order by $col $dir limit " . $start . ", " . $limit;

        $query = $this->db->query($sql);
        if($query->num_rows()>0)
        {
            return $query->result();  
        }
        else
        {
            return null;
        }

        
    }

    function user_search_count($search)
    {
        $db = $this->load->database('default', TRUE);
        $query = $db->like('id',$search)
                ->or_like('customer_name',$search)
                ->get('customers');
    
        return $query->num_rows();
    } 

    public function InsertUser($data)
    {
        $result = $this->db->insert('users', $data);
        return $result;
    }
  
    public function UpdateUser($id, $data)
    {
        $this->db->where('username',$id);
        $result = $this->db->update('users', $data);
        return $result;
    }

    function getRole()     
    { 

        $db = $this->load->database('default', TRUE);
        $query = $db->query("SELECT * FROM roles");
        $result = $query->result();
        return $result;
       
    }

	


}