<?php


require_once('config.php');





if(($response["error"] == true))
{
    echo(json_encode($response));
}




if((isset($_POST["request"])) && ($_POST["request"] == "signin"))
{
    $logs       =   array();
    $email      =   "";
    $password   =   "";

    if((empty($_POST["email"])))
    {
        $response["error"]      =   true;
        $response["error_msg"]  =   "An Error Occurred. Email is required";
    }
    else
    {
        $email  =   $_POST["email"];
    }

    if(($response["error"] == false))
    {
        if((empty($_POST["password"])))
        {
            $response["error"]      =   true;
            $response["error_msg"]  =   "An Error Occurred. Password is required";
        }
        else
        {
            $password  =   $_POST["password"];
        }
    }

    if(($response["error"] == false))
    {
        $sql = "    SELECT M.id ID, M.firstname FIRSTNAME, M.lastname LASTNAME, M.email_address EMAIL_ADDRESS, M.phone_no PHONE_NO, 
                    M.register_on REGISTER_ON, M.role USER_ROLE 
                    FROM users M
                    WHERE LOWER(M.email_address) = LOWER(?)
                    AND M.password = ?";
        $stmt = $db->prepare($sql); 
        $stmt->bind_param("ss", $email, $password);
        $stmt->execute();
        $result = $stmt->get_result();
        $row = $result->fetch_assoc();
        if(($row))
        {
            $logs["USER"]   =   $row;
        }
        else
        {
            $response["error"]      =   true;
            $response["error_msg"]  =   "An Error Occurred. The Email address or password is not correct. No account found in the system.";
        }
    }


    if(($response["error"] == false))
    {
        $_SESSION["GOLDFAYRE"]["USER_ID"]   =   $logs["USER"]["ID"];
        $_SESSION["GOLDFAYRE"]["NAME"]      =   $logs["USER"]["FIRSTNAME"]." ".$logs["USER"]["LASTNAME"];
        $_SESSION["GOLDFAYRE"]["LOGIN_AT"]  =   date('d-m-Y H:i:s');
        $_SESSION["GOLDFAYRE"]["ROLE"]      =   $logs["USER"]["USER_ROLE"];
        $_SESSION["GOLDFAYRE"]["CART"]      =   array();


    }

    if(($response["error"] == false))
    {
        if(($logs["USER"]))
        {
            if(($logs["USER"]["USER_ROLE"] == "administrator"))
            {
                $response["R_URL"]  =   $base_url."admin/dashboard/";
            }
            else
            {
                $response["R_URL"]  =   $base_url;
            }
        }
    }
    
    echo(json_encode($response));

}







































?>