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

class LoginControllers extends CI_Controller {


	public function __construct(){
	parent::__construct();
	$this->load->library("form_validation");
	$this->load->model("LoginModel");
	$this->load->library('session');

	}


	public function index()
	{
		if($this->session->userdata("isUserLoggedIn") == 'true'){
       redirect("LoginControllers/getHome");
     }

 $this->load->view('admin/login');

}


	public function getHome()
	{
$this->load->view('admin/header');
$this->load->view('admin/home');

			//$this->getSessionInfoById();


	}


	function getSessionInfoById() {
$id = $this->session->userdata('UserId');
 $data1['single_data_my'] = $this->LoginModel->getInfoById($id);

	//print_r($data1); die;
$this->load->view('admin/header',$data1);


 }

	public function getUser()
	{

	//	print_r($this->session->userdata("UserId")); die;
		$this->load->view('admin/header');
		$this->load->view('admin/add_admin');
	}

	public function gettrack()
	{
 //print_r($this->session->userdata('UserId')); die;

		if($this->session->userdata('UserId')==0){
			$this->session->sess_destroy();
			redirect('LoginControllers');
		     // do something when exist
		}

	}

	public function checkLogin(){
		$this->load->library("form_validation");

		$this->form_validation->set_rules('User','Email','required');
		$this->form_validation->set_rules('Password','Password','required');

		if($this->form_validation->run() == true){

			$user_name = $this->input->post('User');
			$pass = $this->input->post('Password');

			$result = $this->LoginModel->checkLogin($user_name, $pass);



			if($result){

			//		print_r($result); die;
				$sess_data = array(

			"isUserLoggedIn"	=> TRUE,
					"UserId"=> $result['creator_id'],
						"slider"=> $result['slider'],
							"news"=> $result['news'],
								"event"=> $result['event'],
									"staff"=> $result['staff'],
										"gallery"=> $result['gallery'],
										"subgallery"=> $result['subgallery'],
											"career"=> $result['career'],
											"inquiry"=> $result['inquiry'],
											"user"=> $result['user']
				);

				$this->session->set_userdata($sess_data);

				$login_status = $this->LoginModel->logged_status("admin_table", array('login_status' => 1), array('creator_id' => $result['creator_id']));

				if($login_status){
								$this->session->set_userdata($sess_data);
							redirect('LoginControllers/getHome');
							}else{
								redirect('LoginControllers');
							}

			}else{
			//	echo "Enter your valid ID & Password.";
				$this->session->set_flashdata('msg', 'Enter your valid ID & Password..');
			}
		}
		$this->load->view('admin/login');
	}

	public function logout(){



	$login_status = $this->LoginModel->logged_status("admin_table", array('login_status' => 0), array('creator_id' => $this->session->userdata("UserId")));

	if($login_status){
			// print_r($sess_data); die;
	//	$this->session->unset_userdata($sess_data);
		$this->session->sess_destroy();
		redirect('LoginControllers');
	}else{
		redirect('LoginControllers/getHome');
	}
}


public function insertInfo(){
$data = array(

	'user_name' => $this->input->post('user_name'),
	'PASSWORD' => $this->input->post('PASSWORD'),
	'full_name' => $this->input->post('full_name'),
	'mobile' => $this->input->post('mobile')

	//'creator_id_fk' => $this->input->post('creator_id')

);

	$id = $this->LoginModel->insertInfo('admin_table', $data);
	if($id){
$this->session->set_flashdata('msg', 'data inserted.');
redirect("LoginControllers/getUser");
}else{
$this->session->set_flashdata('msg', 'error...');
redirect("LoginControllers/getUser");
}

}

//...............End insert insertGalleryInfo............


//...............State Manage Subgallerty............


public function manageInfo()
	{
						$this->load->view('admin/header');

		$result['all_info'] = $this->LoginModel->selectInfo('admin_table', array());

			$this->load->view('admin/manage_admin', $result);

}




//...............End Manage Subgallerty............

	//...............State Get ID Wise Subgallery Info ............

	 function getInfoById() {
 $id = $this->uri->segment(3);
	$data['single_data'] = $this->LoginModel->getInfoById($id);

$this->load->view('admin/header');
	 $this->load->view('admin/edit_admin', $data);
	}


			//...............End Get ID Wise Subgallery Info ............

			//...............State Update Subgallery By ID ............


	 function updateInfoById() {

$id= $this->input->post('creator_id');

$data = array(
	'user_name' => $this->input->post('user_name'),
	'PASSWORD' => $this->input->post('PASSWORD'),
	'full_name' => $this->input->post('full_name'),
	'mobile' => $this->input->post('mobile'),
'STATUS' => $this->input->post('STATUS'),
'slider' => $this->input->post('slider'),
'news' => $this->input->post('news'),
'event' => $this->input->post('event'),
'staff' => $this->input->post('staff'),
'gallery' => $this->input->post('gallery'),
'subgallery' => $this->input->post('subgallery'),
'career' => $this->input->post('career'),
'inquiry' => $this->input->post('inquiry'),
'user' => $this->input->post('user'),

);
$this->LoginModel->updateInfoById($id,$data);

if($id){
$this->session->set_flashdata('msg', 'data inserted.');

redirect("LoginControllers/manageInfo");
}else{
$this->session->set_flashdata('msg', 'error...');
redirect("LoginControllers/manageInfo");
}

}
//...............End Update Subgallery By ID ............

public function deleteInfo()
	{
 $id = $this->uri->segment(3);
 //	print_r($id); die;


	$this->LoginModel->deleteInfo($id);

redirect("LoginControllers/manageInfo");
	}

}
