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

class User_model extends CI_Model{
	
	public function add_user($data) { 
		$userins = $data['basic']; 
		$userins['organization_id'] = $data['organization_id'];
		$userins['u_password'] = md5($data['basic']['u_password']);
		$this->db->insert('login',$userins);
		$uid = $this->db->insert_id(); 
		$role = $data['permissions'];
		$role['lr_u_id'] = $uid;
		//return  $this->db->insert('login_roles',$role);
		return array(
			'uid' => $uid,
			'count' => $this->db->insert('login_roles',$role),
		);
	} 
	public function getall_user() { 
		$organization_id=$this->session->userdata['session_data']['organization_id'];
		if($organization_id == "0"){
			$this->db->select("*");
			$this->db->from('login');
			$this->db->join('organization', 'organization.org_id=login.organization_id','LEFT');
			$this->db->order_by('u_id','desc');
			$query = $this->db->get();
			return $query->result_array();
		}else{
			return $this->db->select('*')->from('login')->where('organization_id',$organization_id)->order_by('u_id','desc')->get()->result_array();
		}

	}  


	public function get_userdetails($u_id) { 
	   $this->db->select("*");
	   $this->db->from('login');
	   $this->db->join('login_roles', 'login.u_id=login_roles.lr_u_id');
	   $this->db->where('login.u_id',$u_id);
	   $query = $this->db->get();
	   return $query->result_array();
	} 
	/* public function update_user($data) { 
		$userup = $data['basic'];
		if(isset($data['basic']['u_password'])) {
			$userup['u_password'] = md5($data['basic']['u_password']);
		} 
		$this->db->where('u_id',$data['basic']['u_id']);
		$this->db->update('login',$userup);
		$role = $data['permissions'];

		$this->db->where('lr_u_id',$data['basic']['u_id']);
		return $this->db->update('login_roles',$role);
	} */

	public function update_user($data) { 
		$userup = $data['basic'];
		$userup['organization_id'] = $data['organization_id'];
		if(isset($data['basic']['u_password'])) {
			$userup['u_password'] = md5($data['basic']['u_password']);
		} 
		$this->db->where('u_id',$data['basic']['u_id']);
		$this->db->update('login',$userup);
		$organization_id=$this->session->userdata['session_data']['organization_id'];
		if($organization_id  == "0"){
			$this->db->where('organization_id',$data['organization_id']);
			if($data['basic']['u_isactive']==0){
				$arrayName = array('u_isactive' =>0);
			}
			else{
				$this->db->where('u_id',$data['basic']['u_id']);
				$arrayName = array('u_isactive' =>$data['basic']['u_isactive']);
			}
			$this->db->update('login',$arrayName);
		}else{
			$this->db->where('u_id',$data['basic']['u_id']);
			$arrayName = array('u_isactive' =>$data['basic']['u_isactive']);
			$this->db->update('login',$arrayName);

		}
		$role = $data['permissions'];

		$this->db->where('lr_u_id',$data['basic']['u_id']);
		return $this->db->update('login_roles',$role);
		
	}

	public function getall_user_by_org(){
		$u_id=$this->session->userdata['session_data']['u_id'];
		
		$this->db->select("*");
	   $this->db->from('login');
	   $this->db->join('organization', 'organization.org_id=login.organization_id');
	   $this->db->join('country', 'country.country_id=organization.org_country');
	   $this->db->join('state', 'state.state_id=organization.org_state');
	   $this->db->join('city', 'city.city_id=organization.org_city');
	   $this->db->where('login.u_id',$u_id);
	   $query = $this->db->get();
	   return $query->row_array();
		
	}
} 