<?php 

// Include main file
include_once('../libs/main.php');


// Object Creation 
$adminObj = new admin();

$successMsg = '';

if(isset($_REQUEST['successMsg'])){
	$successMsg = $_REQUEST['successMsg'];
}

if(isset($_REQUEST['failedMsg'])){
	$failedMsg = $_REQUEST['failedMsg'];
}


if(isset($_POST['send']) && $_POST['forgot_email']!=''){
	
		$result = $adminObj->getDetailFromEmail($_POST['forgot_email']);
		if($result){
			
			$From_Display = SITE_NAME;
			$From_Email   = ADMIN_EMAIL;
			
			$Subject="Your password Reset link to ".SITE_NAME;
			$Message = "<font face=\"verdana\" size=\"2\">Dear Admin<br><br>";
			$Message .= "You recently asked to have your username and password. Please reset your password on below links :<br><br>";
			$Message .=  "<a href='".SITE_URL.ADMIN_DIR."resetPassword.php?user=".base64_encode($result->admin_email_address)."' > ".$SITE_URL."resetPassword.php?user=".base64_encode($result->admin_email_address)." </a>  <br><br>";
			$Message .= "Thank you.<br>".SITE_NAME." Team.</font>";
			
			SendMailCommon($From_Display,$From_Email,"",$result->admin_email_address, "","", $Subject,$Message, 1);
		    header ("Location:admin-login.php?successMsg=Password reset link send to your email address");
		    exit;
		}
		else{
			header("Location:admin-login.php?failedMsg=No such email address exist.");
			exit;
		}
}


// Logical section of page
if(isset($_POST['submit'])){
	$userName = $_POST['email_address'];
	$password = $_POST['password'];
	
	if($userName!='' && $password!=''){
			
		$result = $adminObj->checkLogin($userName,md5($password));
		
		if($result){
			
			$_SESSION['admin_id'] = $result->admin_id;
			$_SESSION['admin_fname'] = $result->admin_fname;
			$_SESSION['admin_mname'] = $result->admin_mname;
			$_SESSION['admin_lname'] = $result->admin_lname;
			$_SESSION['admin_user'] = $result->admin_email_address;
			$_SESSION['admin_access'] = $result->admin_access;
			$_SESSION['admin_cat_access'] = $result->admin_cat_access;
			
			$adminObj->updateLastLoginDate();	
			
			header('location:index.php');
			exit;
		}else{
			$failedMsg = "Invalid Username or Password";
		}
			
	}else{
		$failedMsg = "Username and Password are required";
	}
}


if(isset($_REQUEST['act']) && $_REQUEST['act']=='logout'){
	
	unset($_SESSION['admin_id']);
	unset($_SESSION['admin_fname']);
	unset($_SESSION['admin_mname']);
	unset($_SESSION['admin_lname']);
	unset($_SESSION['admin_user']);
	unset($_SESSION['admin_access']);
	
	$successMsg =  "You are successfully logout";
}

// Include Template 
include_once('../libs/routs.php')

?>