<?php
	require_once('source/session.php');
	require_once('source/error.php');
	require_once('source/dbconfig.php');
	require_once('source/function.php');
	
	if(isset($_POST['signin']))
	{
		try { 
			// INSERT RECORD
			$email	   = strip_tags($_POST['username']);
			$username	= strip_tags($_POST['username']);
			$pass		= strip_tags($_POST['password']);
			$password 	= md5(md5($salt . md5($pass)) . $salt);
			$status 	  = "Active";
			
			//Get Duplicate Username OR Email
			$STH = $DBH->prepare("SELECT * FROM tbl_admin WHERE email = :email OR username = :username AND status = :status");
			$STH->bindParam(':email', $email);
			$STH->bindParam(':username', $username);
			$STH->bindParam(':status', $status);
			$STH->execute();
			$row = $STH->fetch(PDO::FETCH_ASSOC);
			if($STH->rowCount() == 1)
			{
				if($password == $row['password'])
				{
					switch($row['access_level'])
					#1) Administrator
					{
						###########Case No:1
						#1) Administrator
						case "Administrator":
						
						session_regenerate_id();
						#MD5 Database Details
						$_SESSION['SESS_ID_MD']  					= md5($salt . $row['id']);
						$_SESSION['SESS_FULLNAME_MD']  		      = md5($salt . $row['fullname']);
						$_SESSION['SESS_EMAIL_MD']  	  	     	 = md5($salt . $row['email']);
						$_SESSION['SESS_USERNAME_MD']  		      = md5($salt . $row['username']);
						$_SESSION['SESS_PASSWORD_MD']  			  = md5($salt . $row['password']);
						$_SESSION['SESS_PHOTO_MD']   	 			 = md5($salt . $row['photo']);
						$_SESSION['SESS_REGISTRATION_DATE_MD']   	 = md5($salt . $row['registration_date']);
						$_SESSION['SESS_ACCESS_LEVEL_MD']   	 	  = md5($salt . $row['access_level']);
						$_SESSION['SESS_HASH_MD']   	 	  		  = md5($salt . $row['hash']);
						$_SESSION['SESS_STATUS_MD']   	 			= md5($salt . $row['status']);
						$_SESSION['SESS_TOKEN_MD']   	 		  = md5($salt . $token);
						
						#NORMAL Database Details
						$_SESSION['SESS_ID_ADMIN']  				 = $row['id'];
						$_SESSION['SESS_FULLNAME_ADMIN']  		   = $row['fullname'];
						$_SESSION['SESS_EMAIL_ADMIN']  	  	      = $row['email'];
						$_SESSION['SESS_USERNAME_ADMIN']  		   = $row['username'];
						$_SESSION['SESS_PASSWORD_ADMIN']  		   = $row['password'];
						$_SESSION['SESS_PHOTO_ADMIN']   	 		  = $row['photo'];
						$_SESSION['SESS_REGISTRATION_DATE_ADMIN']  = $row['registration_date'];
						$_SESSION['SESS_ACCESS_LEVEL_ADMIN']   	   = $row['access_level'];
						$_SESSION['SESS_HASH_ADMIN']   	 	  	   = $row['hash'];
						$_SESSION['SESS_STATUS_ADMIN']   	 	   	 = $row['status'];
						$_SESSION['SESS_TOKEN_ADMIN']   	 		  = $token;
						session_write_close();
						
						#Redirect
						redirect('main.php');
						break;

						###########Default Case:
						default:
						#Redirect
						redirect('index.php?msg=failed');
						break;
					}
					//Switch Close
				}
				else
				{
					#Redirect
					redirect('index.php?msg=failed');
					//echo "Not Verify";
				}
			}
			else
			{
					redirect('index.php?msg=failed');
					//echo "No Record Found";
			}
		}
		catch(PDOException $e) { 
			file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND); # Errors Log File
				redirect('index.php?msg=failed');
		}
	}
	else
	{
				redirect('index.php?msg=failed');
	}
?>