<?php
ob_start();

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

class Adminhome extends CI_Controller
{
    
		function __construct() {

        parent::__construct();

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


    }
 

	
		
	public function displaystudentupload()
	{ 
		 $this->load->view("template/adminheaderpage");
		$this->load->view("template/adminnave");
				$this->load->view('admindata/campusupload');
		$this->load->view("template/footerpage");
	}

	public function user()
	{ 
		 $this->load->view("template/adminheaderpage");
		$this->load->view("template/adminnave");

			 $data['admin'] = $this->adminusermodel->populateusers();
				$this->load->view('admindata/user',$data);
		$this->load->view("template/footerpage");
	}



	public function visitor()
	{ 
		 $this->load->view("template/adminheaderpage");
		$this->load->view("template/adminnave");

			 $data['visitor'] = $this->adminusermodel->populatevisitor();
				$this->load->view('admindata/visitor',$data);
		$this->load->view("template/footerpage");
	}

		
	 public function home() {


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

     $data['username'] = $session_data['username'];
      $data['name'] = $session_data['name'];
       $data['email'] = $session_data['email'];
         $data['status'] = $session_data['status'];
 	 $data['adminid'] = $session_data['adminid'];

	$id= $data['adminid'] ;
	 $data['admin'] = $this->adminusermodel->populateadmindata($id);
	 $this->load->view("template/adminheaderpage");
   $data['userid'] = "yes";
		$this->load->view("template/adminnave");
		 $this->load->view('admindata/home',$data);
		$this->load->view("template/footerpage");
    
   
		
	
  }
  
	
	
	 public function welcome() {
   if (!isset($_GET['randomitemidentifier'])){

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

     $data['username'] = $session_data['username'];
      $data['name'] = $session_data['name'];
       $data['email'] = $session_data['email'];
         $data['status'] = $session_data['status'];
 	 $data['adminid'] = $session_data['adminid'];

	$id= $data['adminid'] ;
	 $data['admin'] = $this->adminusermodel->populateadmindata($id);
	 $this->load->view("template/adminheaderpage");
   $data['userid'] = "yes";
		$this->load->view("template/adminnave");
		 $this->load->view('admindata/adminpage',$data);
		$this->load->view("template/footerpage");
    }
    else {
     $itemid = $_GET['randomitemidentifier'];
      $data['userid'] = "no";
	 $data['admin'] = $this->adminusermodel->populateadmindata($itemid);
	 $this->load->view("template/adminheaderpage");

		$this->load->view("template/adminnave");
		 $this->load->view('admindata/adminpage',$data);
		$this->load->view("template/footerpage");
		
	}
  }
	
	
	
	
	
	
	
	
	
	
	
	
 
 public function displayregisteruser()
	{
	  $this->load->view("template/adminheaderpage");
		$this->load->view("template/adminnave");
		$this->load->view('registeruser');
		$this->load->view("template/footerpage");
	}
	
 
   public function adminbio() {
	$session_data = $this->session->userdata('logged_in');

     $data['email'] = $session_data['email'];
 	$data['id'] = $session_data['id'];
	
	 	$this->load->view("template/headerpage");
	
		$this->load->view("template/studentnave");
		 $this->load->view('userdata/biodata', $data);
		$this->load->view("template/footerpage");
     
    }
 
    public function adminfamily() {
	$session_data = $this->session->userdata('logged_in');

     $data['email'] = $session_data['email'];
 	$data['id'] = $session_data['id'];
	
	 	$this->load->view("template/headerpage");
		
		$this->load->view("template/studentnave");
		$this->load->view('userdata/familydata', $data);
		$this->load->view("template/footerpage");
     
    }
 
   public function adminschool() {
	$session_data = $this->session->userdata('logged_in');

     $data['email'] = $session_data['email'];
 	$data['id'] = $session_data['id'];
	
	 	$this->load->view("template/headerpage");
	
		$this->load->view("template/studentnave");
		$this->load->view('userdata/schoolbackground', $data);
		$this->load->view("template/footerpage");
     
    }
 
 
    public function adminaddress() {
	$session_data = $this->session->userdata('logged_in');

     $data['email'] = $session_data['email'];
 	$data['id'] = $session_data['id'];
	
	 	$this->load->view("template/headerpage");
		
		$this->load->view("template/studentnave");
		 $this->load->view('userdata/addressdata', $data);
		$this->load->view("template/footerpage");
     
    }
 
 
 	
 public function addnewvisitor()
    {
         
	  	 $type = $this->input->post("type");
		  	 $surname = $this->input->post("surname");
			 $idno = $this->input->post("idno");
		  	  $firstname = $this->input->post("firstname");
		  	  	  $email = $this->input->post("email");
		  	  	  	  $username = $this->input->post("username");
		  	  	  	   
		  	  	  	  	  $phone = $this->input->post("phone");
		  	  	  	  	  	  $campus = $this->input->post("campus");
		  	  	  	  	  	  	  $college = $this->input->post("college");
								    $college = $this->input->post("country");
		  	  	  	  	  	  	  	  
 	


		
		  		  			$this->form_validation->set_rules('username', 'username', 'trim|required|min_length[2]|max_length[25]|xss_clean');
		  			$this->form_validation->set_rules('firstname', 'first name', 'trim|required|min_length[2]|max_length[25]|xss_clean');

		  			$this->form_validation->set_rules('surname', 'Surname', 'trim|required|min_length[2]|max_length[25]|xss_clean');
		$testingusernameemail = $this->adminusermodel->verfyvisitorusernameemail($username,$email);
	$testingusername = $this->adminusermodel->verfyvisitorusername($username);
	$testingemail = $this->adminusermodel->verfyvisitoremail($email);
	 if($testingusernameemail==TRUE){
			 
		
			  $this->session->set_flashdata('msg', '<div class="alert alert-danger fade in"><a href="#" class="close" data-dismiss="alert">&times;</a>
						  <strong>Error!</strong> The user with PF Number(' .$username.') and email(' .$email.' already registerd...Try again</div>');
							 redirect("admin/visitor");
						return false;		
		 }else
		 if($testingusername==TRUE){
			 
		
			  $this->session->set_flashdata('msg', '<div class="alert alert-danger fade in"><a href="#" class="close" data-dismiss="alert">&times;</a>
						  <strong>Error!</strong> The user with PF Number(' .$username.')already registerd...Try again</div>');
							 redirect("admin/visitor");
						return false;		
		 } else  if($testingemail==TRUE){
			 
			 	  $this->session->set_flashdata('msg', '<div class="alert alert-danger fade in"><a href="#" class="close" data-dismiss="alert">&times;</a>
						  <strong>Error!</strong> The user with email(' .$email.')already registerd...Try again</div>');
			 
			  redirect("admin/visitor");
			  return false;
			 
		 }  
			
			$data['errors'] = validation_errors();
          if ($this->form_validation->run() == FALSE)
        {
      	$this->load->view("template/adminheaderpage");
		$this->load->view("template/adminnave");
		 $this->load->view('admindata/user',$data);
		$this->load->view("template/footerpage");
		}
		 else
		{
					   //validation succeeds
					   if ($this->input->post('btn_addvisitor') == "visitor")
					   {
								
								$data=array(		
			'surname'=>$surname,
		  	'firstname'=>$firstname,
				'idno'=>$idno,
		  	'email'=>$email,
		  	'regno'=>$username,
		  	'phone'=>$phone,
		  	'campusname'=>$campus,
		  	'collage'=>$college,
		  
		
		  	
					'type'=>$type,
			
								);
							$usr_result = $this->adminusermodel->signvisitor($data);
							
							//check if username and password is correct
							if ($usr_result ==TRUE) //active user record is present
							{
								 
								 
						 $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 Added new User!</div>');
								
								 redirect('admin/visitor');
							}
							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.. That pf number exist! Just login</div>');
								
								 redirect('admin/visitor');
							}
					   }
					   else
					   {
							redirect('admin');
					   }
	    }
    }		
	
	 public function addnewuser()
    {
         
	  	
		  	 $surname = $this->input->post("surname");
			 $idno = $this->input->post("idno");
		  	  $firstname = $this->input->post("firstname");
		  	  	  $email = $this->input->post("email");
		  	  	  	  $username = $this->input->post("username");
		  	  	  	    $password = $this->input->post("password");
		  	  	  	  	  $phone = $this->input->post("phone");
		  	  	  	   	  $status = $this->input->post("status");


		
		  		  			$this->form_validation->set_rules('username', 'username', 'trim|required|min_length[2]|max_length[25]|xss_clean');
		  			$this->form_validation->set_rules('firstname', 'first name', 'trim|required|min_length[2]|max_length[25]|xss_clean');

		  			$this->form_validation->set_rules('surname', 'Surname', 'trim|required|min_length[2]|max_length[25]|xss_clean');
		$testingusernameemail = $this->adminusermodel->verfyuserusernameemail($username,$email);
	$testingusername = $this->adminusermodel->verfyuserusername($username);
	$testingemail = $this->adminusermodel->verfyuseremail($email);
	 if($testingusernameemail==TRUE){
			 
		
			  $this->session->set_flashdata('msg', '<div class="alert alert-danger fade in"><a href="#" class="close" data-dismiss="alert">&times;</a>
						  <strong>Error!</strong> The user with PF Number(' .$username.') and email(' .$email.' already registerd...Try again</div>');
							 redirect("admin/user");
						return false;		
		 }else
		 if($testingusername==TRUE){
			 
		
			  $this->session->set_flashdata('msg', '<div class="alert alert-danger fade in"><a href="#" class="close" data-dismiss="alert">&times;</a>
						  <strong>Error!</strong> The user with PF Number(' .$username.')already registerd...Try again</div>');
							 redirect("admin/user");
						return false;		
		 } else  if($testingemail==TRUE){
			 
			 	  $this->session->set_flashdata('msg', '<div class="alert alert-danger fade in"><a href="#" class="close" data-dismiss="alert">&times;</a>
						  <strong>Error!</strong> The user with email(' .$email.')already registerd...Try again</div>');
			 
			  redirect("admin/user");
			  return false;
			 
		 }  
			
			$data['errors'] = validation_errors();
          if ($this->form_validation->run() == FALSE)
        {
      	$this->load->view("template/adminheaderpage");
		$this->load->view("template/adminnave");
		 $this->load->view('admindata/user',$data);
		$this->load->view("template/footerpage");
		}
		 else
		{
					   //validation succeeds
					   if ($this->input->post('btn_adduser') == "user")
					   {
								
								$data=array(		
			'surname'=>$surname,
		  	'firstname'=>$firstname,
				'idno'=>$idno,
		  	'email'=>$email,
		  	'username'=>$username,
		  	'phone'=>$phone,
		  
			'password'=>$password,
		  		'status'=>$status,
			
								);
							$usr_result = $this->adminusermodel->sigadmin($data);
							
							//check if username and password is correct
							if ($usr_result ==TRUE) //active user record is present
							{
								 
								 
						 $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 Added new User!</div>');
								
								 redirect('admin/user');
							}
							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.. That pf number exist! Just login</div>');
								
								 redirect('admin/user');
							}
					   }
					   else
					   {
							redirect('admin');
					   }
	    }
    }		
			 


			 public function updateuser()
    {
		 $userid = $this->input->post("userid");
         	  	 $id = $this->input->post("id");
		  	 $surname = $this->input->post("surname");
			 $idno = $this->input->post("idno");
		  	  $firstname = $this->input->post("firstname");
		  	  	  $email = $this->input->post("email");
		  	  	  	  $username = $this->input->post("username");
		  	  	  	    $password = $this->input->post("password");
		  	  	  	  	  $phone = $this->input->post("phone");
		  	  	  	  	  	  $campus = $this->input->post("campus");
		  	  	  	  	  	  	  $college = $this->input->post("college");
		  	  	  	  	  	  	  	  $department = $this->input->post("department");
 	  $status = $this->input->post("status");


		   	

		  		  			$this->form_validation->set_rules('username', 'username', 'trim|required|min_length[2]|max_length[25]|xss_clean');
		  			$this->form_validation->set_rules('firstname', 'first name', 'trim|required|min_length[2]|max_length[25]|xss_clean');

		  			$this->form_validation->set_rules('surname', 'Surname', 'trim|required|min_length[2]|max_length[25]|xss_clean');

		     $data['admin'] = $this->adminusermodel->populateadmindata($id);
			
			$data['errors'] = validation_errors();
          if ($this->form_validation->run() == FALSE)
        {
      	$this->load->view("template/adminheaderpage");
		$this->load->view("template/adminnave");
		 $this->load->view('admindata/adminpage',$data);
		$this->load->view("template/footerpage");
		}
		 else
		{
					   //validation succeeds
					   if ($this->input->post('btn_updateuser') == "user")
					   {
								
								$data=array(		
			'surname'=>$surname,
		  	'firstname'=>$firstname,
				'idno'=>$idno,
		  	'email'=>$email,
		  	'username'=>$username,
		  	'phone'=>$phone,
		  	'campus'=>$campus,
		  	'college'=>$college,
		  	'department'=>$department,
			'password'=>$password,
		  		'status'=>$status,
			
								);
							$usr_result = $this->adminusermodel->updateuser($data, $id);
							
							//check if username and password is correct
							if ($usr_result ==TRUE) //active user record is present
							{
								 
								 
						 $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 Updated the user Details !</div>');
								if( $userid=="yes"){
									
										 redirect('administrator/welcome');
								}else 	if( $userid=="no"){
									
										redirect('admin/user');
								}
								
							
							}
							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>');
								
									if( $userid=="yes"){
									
										 redirect('administrator/welcome');
								}else 	if( $userid=="no"){
									
										 redirect('admin/user');
								}
							}
					   }
					   else
					   {
							redirect('admin');
					   }
	    }
    }
    
	   public function logout() {
        $data = ['id', 'username'];
        $this->session->set_userdata('adminadminlogged_in');
		session_destroy();

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