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

class Admin extends CI_Controller 
{

	public function __construct()
	{
		parent::__construct();
		$this->load->library('session');
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->library('userauth');
		$this->load->library('password');
		$this->load->model('Admin_model');
		$this->load->helper('cookie');
	}

	public function index()
	{
		$this->load->view('admin/login');
	}

	public function dashboard()
	{
		$this->userauth->logged_in();
		$data['class'] = $this->Admin_model->totalrows('tbl_classname','*');
		$data['creates'] = $this->Admin_model->totalrows('tbl_classform','*');
		$data['users'] = $this->Admin_model->totalrows('tbl_usermaster','*');
		$data['list'] = $this->Admin_model->totalrows('tbl_regmaster','*');
		$this->load->view('admin/dashboard',$data);
	}

	public function login() 
	{

		$data['username'] = get_cookie('swebin_user_ad');
		$data['password'] = $this->password->decrypt_password(get_cookie('swebin_sec'));
		
		$this->form_validation->set_rules('username', 'username', 'trim|required');
		$this->form_validation->set_rules('password', 'password', 'trim|required');
		
		if ($this->form_validation->run() == FALSE) 
		{
			$this->load->view('admin/login',$data);
		}
		else
		{
			$username = $this->input->post('username');
			$password_input = $this->input->post('password');
			$remember_me = $this->input->post('remember_me');
			$password = $this->password->encrypt_password($password_input);
			
			if($remember_me)
			{
				$expire = time()+365*60*60*24;

				set_cookie('abc_user',$username, $expire);
				set_cookie('abc_password',$password, $expire);
			}
			else
			{
				delete_cookie('abc_user');
				delete_cookie('abc_password');
			}


			$data = $this->Admin_model->login_valid($username,$password);
			
			if($data) 
			{
				$this->session->set_userdata('user_id',$data['user_id']);
				$this->session->set_userdata('user_name',$data['user_name']);
				$this->session->set_userdata('user_type',$data['user_type']);

				redirect('admin/dashboard');
			}
			else 
			{
				$this->session->set_flashdata('login_failed', 'Error Occur. Login Failed..!');
				$this->load->view('admin/login',$data);
			}
		}
	}

	public function logout()
	{
		$this->session->sess_destroy();
		redirect('admin', 'refresh');
	}

	
}

?>
