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

class Model_user_address extends CI_Model {

	public function fetchAddress($user_id = null,$latest = false,$profiling_id = null)
	{
		$this->db->select('MAX(a.profiling_id)');
		$this->db->from('user__address a');
		if($user_id) $this->db->where('a.user_id',$user_id);
        $this->db->where('a.is_delete',0);
		$max = $this->db->get_compiled_select();


		$this->db->select('a.*,
							b.state as state_name ,
							c.country as country_name,
							d.action_efective_date,
							e.type as action_type
							');

		$this->db->where('a.is_delete',0);
		if($profiling_id) $this->db->where('a.profiling_id',$profiling_id);
		if($user_id) $this->db->where('a.user_id',$user_id);
		if($latest) $this->db->where("a.profiling_id IN ($max)"); 

		$this->db->order_by('a.profiling_id','DESC');
		$this->db->order_by('a.type','ASC'); //make sure permanent address will be on the top followed by temporary n correspondence 

		$this->db->join('_ref__state b','b.id = a.state_id','left');
		$this->db->join('_ref__country c','c.id = a.country_id','left');
		$this->db->join('user__profiling d','d.id = a.profiling_id','left');
		$this->db->join('_ref__action_type e','e.id = d.action_type_id','left');

		$query = $this->db->get('user__address a');
		
		return ($query->num_rows() > 0)?$query->result():null;
	}

	public function fetchlatest($user_id)
	{
		$this->db->select('a.id');
		$this->db->where('a.user_id',$user_id);
		$this->db->where('a.is_delete',0);
		$this->db->order_by('a.action_efective_date,id','desc');
		$profiling_ids = $this->db->get('user__profiling a')->result();

		if(!empty($profiling_ids))
		{	
			foreach ($profiling_ids as $key => $id) 
			{
				$this->db->select('a.*,
							b.state as state_name ,
							c.country as country_name,
							d.action_efective_date,
							e.type as action_type
							');
				$this->db->where('a.is_delete',0);
				$this->db->where('a.profiling_id',$id->id);
				if($user_id) $this->db->where('a.user_id',$user_id);
				$this->db->order_by('a.profiling_id','DESC');
				$this->db->order_by('a.type','ASC');
				$this->db->join('_ref__state b','b.id = a.state_id','left');
				$this->db->join('_ref__country c','c.id = a.country_id','left');
				$this->db->join('user__profiling d','d.id = a.profiling_id','left');
				$this->db->join('_ref__action_type e','e.id = d.action_type_id','left');
				$query = $this->db->get('user__address a');
				if($query->num_rows() > 0)
				{
					return $query->result();
				}

				// return ($query->num_rows() > 0)?$query->result():null;
			}
		}else{
			return null;
		}
	}

	public function createAddress($input = array())
	{
		$data = array
		(
			'profiling_id'	=> $input['profiling_id'],
			'user_id' => $input['user_id'],
			'type'	=> $input['type'],
			'address' => $input['address'],
			'postcode' => $input['postcode'],
			'city' => $input['city'],
			'state_id' => $input['state_id'],
			'country_id' => $input['country_id'],
			'contact_no' => $input['contact_no'],
			'is_delete' => 0 
		);

		$this->db->insert('user__address',$data);

		return $this->db->insert_id();
	}

	public function editAddress($input = array(), $address_id = null)
	{
		$this->db->set($input);
		$this->db->where('id',$address_id);

		$this->db->update('user__address');

		return true;
	}

	public function getAddress($address_id = null)
	{
		$this->db->where('id',$address_id);
		// $this->db->where('profiling_id',$profiling_id);

		$query = $this->db->get('user__address');

		return ($query->num_rows() > 0) ? $query->result_array() : null;
	}


}
?>