<?php
session_start();
error_reporting(0);
if (isset($_POST['AdminEmail']) && isset($_POST['AdminPassword'])) {
	require "connect.php";
	$AdminEmail = trim(mysqli_real_escape_string($connect, htmlentities($_POST['AdminEmail'])));
	$AdminPassword = trim(mysqli_real_escape_string($connect, htmlentities($_POST['AdminPassword'])));
	
	if($connect){
	   
	}else {
	    echo "Database connection is wrong 😑 ";
	}

	if ($AdminPassword == '') {
		echo "emptyPassword";

	}else if (filter_var($AdminEmail, FILTER_VALIDATE_EMAIL) == false) {
		echo "invalidEmail";
	}
	else {
		    //=================== DATA =======================
    $QUERY = "SELECT * FROM admin WHERE Email = '$AdminEmail' AND AccountType = 1";
    //==============================QUERY DATA ================================
    $result = mysqli_query($connect, $QUERY);
    //======================== GET NUM ROWS ==============================
    $num_rows = mysqli_num_rows($result);
    //============================LOGIN USER OR DIE USER ===============================
            if ($num_rows == 1) {
                           //===================== FETCH DATA ==================================
                $fetch_data = mysqli_fetch_assoc($result);
                $ID = $fetch_data['ID'];
                $email = $fetch_data['Email'];
                $DB_password = $fetch_data['Password'];               

                if ($DB_password == $AdminPassword) {
                    echo "<div class='wow successful_login fadeIn animated' style='text-align: center; color: green;'><span> Login successful..</span></div>";
                    echo "<script>window.location.href='/home/admin/'</script>";
                    $_SESSION['AdminLogin'] = $fetch_data['ID'];
                    $_SESSION['username'] = $fetch_data['Fullname'];
                    $_SESSION['Email'] = $fetch_data['Email'];
                    die();
                } else {
                    echo '<div class="wow error_display fadeIn animated" style="text-align: center; color: red;">Invalid login details, Please try again.</div>';
                }
            }
            else {
                echo '<div class="wow error_display fadeIn animated" style="text-align: center; color: red;"> Invalid login details, Please try again.</div>';
            }
	}







}else {
	header("Location: /en/");
	die();
}