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

class City extends CI_Controller {

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

	}

	function index()	{	
		$this->manage_city();
	}
	
	

	function list_city() {
		admin_auth();
			
		//$this->_page_data['page_title']="List of citys";
		$this->page_data['bredcrum']=array(			                           
										'City'=>'#',
										'List of city'=>'#',
										);


		$condition = array();
                            
                            $condition[] = array('join',TABLE_STATE,TABLE_STATE.'.state_id ='.TABLE_CITY.'.state_id');
                           // $condition[] = array('where','users.is_active');


                            $selected_array=array(
                                'table_name'=>TABLE_CITY,
                                'table_fields'=>TABLE_CITY.'.is_active,city_id,city_name,state_name,'.TABLE_STATE.'.state_id',
                                'condition'=>$condition,
                                'limit'=>'',
                                'offset'=>''
                            );
 

		$city['city'] = $this->master_model->select_data($selected_array);  

		 if(!empty($city['city'])&& is_array($city['city'])){
							
				 $this->page_data['city'] =   $city['city'];
		 	

  	    }    
  	        $this->load->view('gc/city_list',$this->page_data);
	}	
	
	function view_city($city_id) {
		admin_auth();
		$id=$this->encrypt->decode($city_id);
			//error_reporting(E_ALL);
     
		//$this->_page_data['page_title']="View city";
		$this->_page_data['bredcrum']=array(
			 							
			 							'City'=>'#',
										'List of city'=>'#',
										'View city'=>'#',
										);
		$condition = array();
                            //$condition[] = array('where',TABLE_COMPANY.'.company_id',$company_id);
                            $condition[] = array('join',TABLE_STATE,TABLE_STATE.'.state_id ='. TABLE_CITY.'.state_id');
                          $condition[] = array('where','city_id',$id);

                            $selected_array=array(
                                'table_name'=>TABLE_CITY,
                                'table_fields'=>TABLE_CITY.'.is_active,city_id,city_name,state_name,'.TABLE_STATE.'.state_id',
                                'condition'=>$condition,
                                'limit'=>'',
                                'offset'=>''
                            );

		$city['city'] = $this->master_model->select_data($selected_array);
		
		if(!empty($city['city'])&& is_array($city['city']))
			{
				 $this->_page_data['city'] = $city['city'];		 
  	        }
		$this->load->view('gc/city_view.php',$this->_page_data);
	}
	
	function manage_city($city_id="") {
		admin_auth();
		$id=$this->encrypt->decode($city_id);
			//error_reporting(E_ALL);
		
		//if($purpose=='')
			//$purpose='Add';
		$this->_page_data['state'] =$this->fetch_state();	
		//$this->_page_data['purpose']=$purpose;
		//$this->_page_data['page_title']=$purpose." city";
		$this->_page_data['bredcrum']=array(
										
										'City'=>'#',
										//'List User'=>site_url(USER_PATH.'list_user'),
										'Add city'=>'#',
										);

         							
		if (!$id) {
            
	       $this->load->view('gc/city_manage.php',$this->_page_data);

		}else{
                     
			 $condition = array();
                            //$condition[] = array('where',TABLE_COMPANY.'.company_id',$company_id);
                            $condition[] = array('join',TABLE_STATE,TABLE_CITY.'.state_id ='. TABLE_STATE.'.state_id');
                           // $condition[] = array('where','users.is_active');
                             $condition[] = array('where','city_id',$id);

                            $selected_array=array(
                                'table_name'=>TABLE_CITY,
                                'table_fields'=>TABLE_CITY.'.is_active,city_id,city_name,state_name,'.TABLE_STATE.'.state_id',
                                'condition'=>$condition,
                                'limit'=>'',
                                'offset'=>''
                            );
		$city['city'] = $this->master_model->select_data($selected_array);
		
		if(!empty($city['city'] )&& is_array($city['city']))
			{
				 $this->_page_data['city'] = $city['city'] ;
				 
  	  			 
  	        }
		  $this->load->view('gc/city_manage.php',$this->_page_data); 
		
		}

	}
	

    function add_city($city_id){
    	admin_auth();
    	$id=$this->encrypt->decode($city_id);
    		//error_reporting(E_ALL);
    	   	    
    	$city_name = check_injections_filter($this->input->post('city_name'));
    	$state_name =check_injections_filter($this->input->post('state_name'));
    	$user_id = $this->session->userdata('user_id');    
    	
    	

       $condition[] = array('where','city_name',$city_name);
       if ($id) {

    	    $condition[] = array('where_not_in','country_id',$id);
        }

    	$selected_array=array(
								'table_name' =>TABLE_CITY,								
								'table_fields'=>'city_name',
								'limit'=>'',
								'offset'=>'',
								'condition'=>$condition							
					        );
    			$city['city']  = $this->master_model->select_data($selected_array);
               if(!empty($city['city'] ))
				{
					 $this->session->set_flashdata('message','City already present');
				     redirect('http://jade.nityam.in/index.php/gc/city/manage_city');
  	  			 
  	        	}else{
    		       if (!$city_id) {     
				         $this->session->set_flashdata('message','City inserted successfully');
				         $city_data=array(
								'city_name' => $city_name,
								'state_id' =>$state_name,
								'created_on' => date("Y-m-d H:i:s"),
								'created_by' =>$user_id,
								'modified_by' => '',	
								'modified_on' => date("Y-m-d H:i:s")
								
					    );
				         $this->master_model->insert_record(TABLE_CITY,$city_data);
    	                 redirect('http://jade.nityam.in/index.php/gc/city/list_city');
    	            }else{
			
				 		$this->session->set_flashdata('message','City updated  successfully');
				 		$city_data=array(
								'city_name' => $city_name,
								'state_id' =>$state_name,
								'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")
								
					    );
				 		$condition=array('city_id'=>$id);
				 		$this->master_model->update_record(TABLE_CITY,$city_data,$condition);
			     		redirect('http://jade.nityam.in/index.php/gc/city/list_city');
			        }
               }
    }

    function fetch_state(){
    	admin_auth();
    	//$condition[]=array('where','state_id',$state_id);
    	$selected_array = array( 
			                   'table_name' =>TABLE_STATE,
			                   'table_fields' => 'state_id,state_name',
			                   'order_by_field'=>'Asc',
			                   //'condition'=>$condition
		);
		
		
			return $this->master_model->get_table_data($selected_array);	
	

			         
    }
    function disable_city($city_id){
    	admin_auth();
    	$id=$this->encrypt->decode($city_id);
     $city_data=array(
								'is_active' =>'0',								
								'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")								
					    );
     	$condition=array('city_id'=>$id);
     	$this->session->set_flashdata('message',' data is disable');
		$this->master_model->update_record(TABLE_CITY,$city_data,$condition);
		redirect('http://jade.nityam.in/index.php/gc/city/list_city');
    }

    function enable_city($city_id){
    	admin_auth();
    	$id=$this->encrypt->decode($city_id);
      $city_data=array(
								'is_active' =>'1',								
								'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")								
					    );
     	$condition=array('city_id'=>$id);
     	$this->session->set_flashdata('message',' data is enable');
		$this->master_model->update_record(TABLE_CITY,$city_data,$condition);
		redirect('http://jade.nityam.in/index.php/gc/city/list_city');
    }
   

}