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

class County extends CI_Controller {


		function __construct() {

        parent::__construct();

        $this->load->database();
		$this->load->model('countymodel','',TRUE);
		$this->load->model("countymodel", "login",TRUE);
     	   if(($this->session->userdata('adminadminlogged_in')=="")) {
            $this->session->set_flashdata('flash_data', 'You don\'t have access!');
            redirect('administrator');
        }

    }
	
	
	
	public function index()


	{ 
		$session_data = $this->session->userdata('adminlogged_in');


		 $data['county'] = $this->countymodel->populatecounty();
	 $this->load->view("template/adminheaderpage");

		$this->load->view("template/adminnave");
		 $this->load->view('admindata/county',$data);
		$this->load->view("template/footerpage");
     
	}
		
	
	

	
			 public function addcounty()
    {
         
		  	 $name = $this->input->post("name");
		   	  $description = $this->input->post("description");
		  $this->form_validation->set_rules("name", "county Name", "trim|required");
		    
			
			$data['errors'] = validation_errors();
          if ($this->form_validation->run() == FALSE)
        {
      	$this->load->view("template/adminheaderpage");
		$this->load->view("template/adminnave");
		 $this->load->view('admindata/county',$data);
		$this->load->view("template/footerpage");
		}
		 else
		{
					   //validation succeeds
					   if ($this->input->post('btn_county') == "county")
					   {
								
								$data=array(		
			'name'=>$name,
		  	'description'=>$description,
			
								);
							$usr_result = $this->countymodel->addnewcounty($data);
							
							//check if username and password is correct
							if ($usr_result ==TRUE) //active user record is present
							{
								 echo "datasaved";
								 
						 $this->session->set_flashdata('msg', '<div class="alert alert-success fade in">
						  <a href="#" class="close" data-dismiss="alert">&times;</a>
						 
						<strong>Success!</strong> You have successfully Adde new county!</div>');
								
								 redirect('admin/county');
							}
							else if ($usr_result ==FALSE) 
							{     

						 $this->session->set_flashdata('msg', '<div class="alert alert-danger fade in">
						 <a href="#" class="close" data-dismiss="alert">&times;</a>
						  <strong>Error!</strong> Data are not saved.. Try again</div>');
								
								 redirect('admin/county');
							}
					   }
					   else
					   {
							redirect('admin');
					   }
	    }
    }


			
	 function __destruct() {

        $this->db->close();

    }
	
	
	
	   public function logout() {
        $data = ['id', 'pfno'];
        $this->session->set_userdata('adminlogged_in');
		session_destroy();

  		 redirect('administrator', 'refresh');
       
    }
	
	
	
	
}
 ?>