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

class Geography extends CI_Controller {

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

	function index()	{	
		$this->list_geography();
	}
	
	function list_branch_geography() {
		admin_auth();
		$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,a.saleble_geography_name',
                               '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);
           if(!empty($geography['geography'])&& is_array($geography['geography'])){
							
				 $this->page_data['geography'] =   $geography['geography'];
		 	

  	    } 
                       

		$this->load->view('gc/list_geography.php',$this->page_data);
	}	
	
	function view_branch_geography($branch_salable_geography_id) {
		admin_auth();
     	$id=$this->encrypt->decode($branch_salable_geography_id);
		$this->data['page_title']="View geography";
		$this->data['bredcrum']=array(
			 							
			 							'Branch Saleable Geography'=>'#',
										'List of Branch Saleable Geography'=>'#',
										'View Geography'=>'#',
										);

		$condition[] = array('join',TABLE_COUNTRY.' as co','co.country_id=a.country_ids','left',);
		 $condition[] = array('where','branch_salable_geography_id',$id);
        $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,a.saleble_geography_name',
                               '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);
           if(!empty($geography['geography'])&& is_array($geography['geography'])){
							
				 $this->page_data['geography'] =   $geography['geography'];
		 	

  	    } 

		$this->load->view('gc/view_geography.php',$this->page_data);
	}
	
	function manage_branch_geography($branch_salable_geography_id="") {
		admin_auth();
		
		$id=$this->encrypt->decode($branch_salable_geography_id);
		$this->_page_data['country']=$this->fetch_country();
		$this->_page_data['bredcrum']=array(
										
										'Branch Saleable Geography'=>'#',
										'List Branch Saleable Geography'=>site_url(GEOGRAPHY.'list_branch_geography'),
										'Add  Branch Saleable Geography'=>'#',
										);

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

		}else{
	
		$condition[] = array('join',TABLE_COUNTRY.' as co','co.country_id=a.country_ids','left');
		$condition[] = array('where','branch_salable_geography_id',$id);
        $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,a.saleble_geography_name,co.country_id,a.country_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);
		
		if(!empty($geography['geography'] )&& is_array($geography['geography']))
			{
				 $this->_page_data['geography'] = $geography['geography'] ;
				 	 
  	        }

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

	 function add_geography(){
	 	admin_auth();

     $id = check_injections_filter($this->input->post('hidden'));
	 $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');


	 $this->form_validation->set_rules('saleable_name', 'Branch Salable Name','required'); 
     if ($this->form_validation->run() == FALSE)
	 {
	 		$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);
	 }else{
	 $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' =>'',	
								'modified_on' => date("Y-m-d H:i:s")
								
					    );
	 $condition[] = array('where','saleble_geography_name',$Saleable_geography_name);
	 if ($id) {

                 			$condition[] = array('where_not_in','branch_salable_geography_id',$id);
    		    }
     $selected_array=array(
								'table_name' =>TABLE_BRANCH_SALABLE_GEOGRAPHY,								
								'table_fields'=>'saleble_geography_name',
								'limit'=>'',
								'offset'=>'',
								'condition'=>$condition							
					        );
        $geography['geography'] = $this->master_model->select_data($selected_array);
        if(!empty($geography['geography']))
				{
					 	$this->session->set_flashdata('message','data already present');
				     	redirect('https://shagoon.nityamhygiene.in/index.php/gc/geography/manage_branch_geography');
  	  			 
  	        	}else{
    		         if (!$id) {
						$this->session->set_flashdata('message','data inserted successfully');
				 		$this->master_model->insert_record(TABLE_BRANCH_SALABLE_GEOGRAPHY,$geography_data);
    	         		redirect('https://shagoon.nityamhygiene.in/index.php/gc/geography/list_branch_geography');
    	             }else{			

    	             	$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 updated  successfully');
						$condition=array('branch_salable_geography_id'=>$id);
						$this->master_model->update_record(TABLE_BRANCH_SALABLE_GEOGRAPHY,$geography_data,$condition);
			     		redirect('https://shagoon.nityamhygiene.in/index.php/gc/geography/list_branch_geography');
					  }
				} 
		}

     

	 }
	
	 function fetch_country(){
	 	admin_auth();
    	$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(){
    	admin_auth();

        $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(){

		admin_auth();

    	$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;	
        
          }	
                   
    }
    function disable_geography($branch_salable_geography_id){
    	admin_auth();
    	$id=$this->encrypt->decode($branch_salable_geography_id);
    	$user_id=$this->session->userdata('user_id');
     $geography_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('branch_salable_geography_id'=>$id);
     	$this->session->set_flashdata('message',' data is deactive');
		$this->master_model->update_record(TABLE_BRANCH_SALABLE_GEOGRAPHY,$geography_data,$condition);
		redirect('https://shagoon.nityamhygiene.in/index.php/gc/geography/list_branch_geography/');
    }
    function enable_geography($branch_salable_geography_id){
    	admin_auth();
    	$id=$this->encrypt->decode($branch_salable_geography_id);
    	$user_id=$this->session->userdata('user_id');
      $geography_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('branch_salable_geography_id'=>$id);
     	$this->session->set_flashdata('message',' data is active');
		$this->master_model->update_record(TABLE_BRANCH_SALABLE_GEOGRAPHY,$geography_data,$condition);
		redirect('https://shagoon.nityamhygiene.in/index.php/gc/geography/list_branch_geography/');
    }
   
}