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

class Organization_model extends CI_Model{
	
	public function add_organization($data){
		$customerins = $data; 
		/* if(isset($data['c_pwd'])) {
			$customerins['c_pwd'] = md5($data['c_pwd']); 
		} */
		$this->db->insert('organization',$customerins);
		 return $this->db->insert_id();
	}
    public function getall_organization() { 
		$this->db->select("*");
		$this->db->from('organization');
		$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->order_by("org_id", "desc");
		$query = $this->db->get();
		 return $query->result_array();
	} 
	public function get_organization($org_id) { 
	   $this->db->select("*");
	   $this->db->from('organization');
	   $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('org_id',$org_id);
	   $query = $this->db->get();
	   return $query->result_array();
	}  
	public function update_organization(){  
		$this->db->where('org_id',$this->input->post('org_id'));
		return $this->db->update('organization',$this->input->post());
	}
	public function fetch_country(){
	  $this->db->order_by("country_name", "asc");
	  $query = $this->db->get("country");
	  return $query->result();
	}
	public function fetch_states($org_id){ 
	   //get percentage from the database
		$query = $this->db->select('org_country')->from('organization')->where('org_id', $org_id)->get();
		$data =$query->row()->org_country;
		$query1=$this->db->select('*')->from('state')->where('country_id',$data)->get();
		return $query1->result();
	}

	public function fetch_citys($org_id){
	  $query = $this->db->select('org_state')->from('organization')->where('org_id', $org_id)->get();
	  $data =$query->row()->org_state; 
	  $query1=$this->db->select('*')->from('city')->where('state_id',$data)->get();
	  return $query1->result();
	}
	public function fetch_state($country_id)
	{
	  $this->db->where('country_id', $country_id);
	  $this->db->order_by('state_name', 'ASC');
	  $query = $this->db->get('state');
	  $output = '<option value="">Select State</option>';
	  foreach($query->result() as $row)
	  {
	   $output .= '<option value="'.$row->state_id.'">'.$row->state_name.'</option>';
	  }
	  return $output;
	}
	public function fetch_city($state_id){
	  $this->db->where('state_id', $state_id);
	  $this->db->order_by('city_name', 'ASC');
	  $query = $this->db->get('city');
	  $output = '<option value="">Select City</option>';
	  foreach($query->result() as $row)
	  {
	   $output .= '<option value="'.$row->city_id.'">'.$row->city_name.'</option>';
	  }
	  return $output;
	}
} 