<?php
session_start();
header('Content-Type: application/json');
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(1);
ini_set("log_errors", 1);
ini_set("error_log", "php-error.log");

date_default_timezone_set("Asia/Kolkata");

require_once('API/database.php');

$validationErr = "";
$result = array();

if(!isset($_REQUEST['userID']) || $_REQUEST['userID']  == '') {
	$validationErr = "Please enter the User Name!";
} 

else if(!isset($_REQUEST['password']) || $_REQUEST['password']  == '') {
	$validationErr = "Please enter the password!";
} 

else
{

	$userName		 = $_REQUEST['userID'];
	$password        = md5($_REQUEST['password']);
    $decryptPassword = $_REQUEST['password'];


    try{

			$conn = new Database;
			$db = $conn->getConnection();
			$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

			$stmt = $db->prepare("select * from my_admin_users where username='$userName' and password = '$password'");
			$stmt->execute();
			$resultData = $stmt->fetch(PDO::FETCH_ASSOC);
			$db = null;
			$result = array();

			if($resultData['id'] != NULL)
			{
				$result['success'] = true;
				$result['message'] = 'Login Successful!';
				$_SESSION['admin_username'] = $resultData['username'];
				$_SESSION['admin_phone'] = $resultData['phone'];
				$_SESSION['admin_email_address'] = $resultData['email_address'];
				$_SESSION['admin_name'] = $resultData['firstname'] . " " . $resultData['lastname'];
				$resultData['decryptPassword'] =  $decryptPassword;
			}


			else
			{
				session_unset();
				session_destroy();
				$result['success'] = false;
				$result['message'] = 'Invalid Login Credentials!';
			}

	}

    catch(Exception $err){
	  $result['success'] = false;
	  $result['message'] = 'Login failed!';
    }

	print $return = json_encode($result);
	exit;

}


if($validationErr != '') {

	$result = array();
	$result['success'] = false;
	$result['message'] = $validationErr;
	print $return = json_encode($result);
	exit;

}

?> 