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

class Geography extends CI_Controller {

	function __construct()	{
		parent::__construct();
		//$this->load->model('master_model');
		error_reporting(E_ALL);
	}

	function index()	{	
		$this->list_geography();
	}
	
	function list_branch_geography() {
		$this->page_data['page_title']="List of Branch Saleable Geography";
		$this->page_data['bredcrum']=array(			                           
										'Branch Saleable Geography'=>'#',
										'List of Branch Saleable Geography'=>'#',
										);		   

        $condition[] = array('join',TABLE_COUNTRY.' as co','co.country_id=a.country_ids','left',);
        $selected_array=array(
                              'table_name'=>TABLE_BRANCH_SALABLE_GEOGRAPHY.' as a',
                              'table_fields'=>'a.branch_salable_geography_id,co.country_name,a.is_active,a.state_ids,a.city_ids',
                               'condition'=>$condition,
                               'limit'=>'',
                               //'order_by_field'=>'a.branch_salable_geography_id',
                               // 'group_by_field'=>'b.state_name,c.city_name',
                               'offset'=>''
                              );
           $geography['geography'] = $this->master_model->select_data($selected_array);
           print_r($geography);

        


		// echo "<pre>"; 
		// print_r($state);
		// // print_r($city);
		// // print_r($country);
		// exit;	
  	    


		$this->load->view('gc/list_geography.php',$this->page_data);
	}	
	
	function view_branch_geography($geography_id) {
     
		$this->data['page_title']="View geography";
		$this->data['bredcrum']=array(
			 							
			 							'Branch Saleable Geography'=>'#',
										'List of Branch Saleable Geography'=>'#',
										'View Geography'=>'#',
										);
		$this->load->view('gc/view_geography.php',$this->data);
	}
	
	function manage_branch_geography($user_id=NULL) {
		
		error_reporting(E_ALL);
		
		//echo $country_id;
		
	

		$this->_page_data['country']=$this->fetch_country();
	
		$this->_page_data['bredcrum']=array(
										
										'Branch Saleable Geography'=>'#',
										//'List User'=>site_url(USER_PATH.'list_user'),
										'Add  Branch Saleable Geography'=>'#',
										);

		
		$this->load->view('gc/add_geography.php',$this->_page_data);
	 }

	 function add_geography(){

	 $Saleable_geography_name = check_injections_filter($this->input->post('saleable_name'));
	 $country_name = $this->input->post('country_name');
	 $state = $this->input->post('state');
	 $states=  implode(",",$state);
	 $city = $this->input->post('cities');
	 $cities=  implode(",",$city);
	 $user_id=$this->session->userdata('user_id');

	 $geography_data=array(
								'saleble_geography_name' =>$Saleable_geography_name,
								'country_ids' =>$country_name,
								'state_ids' =>$states,
								'city_ids' => $cities,
								'created_on' => date("Y-m-d H:i:s"),
								'created_by' =>$user_id,
								'modified_by' =>$user_id,	
								'modified_on' => date("Y-m-d H:i:s")
								
					    );

	 					$this->session->set_flashdata('message','data inserted successfully');
				 		$this->master_model->insert_record(TABLE_BRANCH_SALABLE_GEOGRAPHY,$geography_data);
    	         		redirect('http://jade.nityam.in/index.php/gc/geography/manage_branch_geography');
	 }
	
	 function fetch_country(){
    	$selected_array = array( 
			                   'table_name' =>TABLE_COUNTRY,
			                   'table_fields' => 'country_id,country_name',
			                   // 'order_by_field'=>'Asc'
		);

		return $this->master_model->get_table_data($selected_array);		         
    }
     
    function fetch_state(){

        $country_id=$this->input->post('country_id');


	    $state_cond = array();
		
		$state_cond[] = array('where','country_id',$country_id);
		$state_cond[] = array('where','is_active','1');
		$select_array = array(
								'table_name'=>TABLE_STATE,
								'table_fields'=>'state_name,state_id,country_id,is_active',
								'condition'=>$state_cond,
								'sort_by'=>'state_id',
								'order_by_field'=>'state_name',
								'order'=>'ASC',
								'limit'=>'',
								'offset'=>''
								);
		$state_data = $this->master_model->select_data($select_array);
		$output='';
		 foreach($state_data as $row)
                     {
                  		 // $output .= '<option value="'.$row->state_id.'">'.$row->state_name.'</option>';
                  		  $output .= '<option value='.$row['state_id'].'>'.$row['state_name'].'</option>';

                     }

          echo $output;
          // /echo $state_data;


    }
    function fetch_city(){



    	$state_id=$this->input->post('state_id');
        foreach ($state_id as  $value) {
        //$state=  implode(" ",$state_id);
	    $city_cond = array();
		$city_cond[] = array('where','state_id',$value);
		$city_cond[] = array('where','is_active','1');
		$select_array = array(
								'table_name'=>TABLE_CITY,
								'table_fields'=>'city_name,city_id,state_id,is_active',
								'condition'=>$city_cond,
								'sort_by'=>'city_id',
								'order_by_field'=>'city_name',
								'order'=>'ASC',
								'limit'=>'',
								'offset'=>''
								);
		$city_data = $this->master_model->select_data($select_array);
		$output='';
		 foreach($city_data as $row)
                     {
                  		
                  		  $output .= '<option value='.$row['city_id'].'>'.$row['city_name'].'</option>';

                     }

          echo $output;	
        
          }	
                   
    }

}