<?php
// require_once('../../vendor/autoload.php');
require_once('../init.php');

function error_res($status, $response_code, $message, $uri = '')
{
    $response = new stdClass;
    $response->status = $status;
    $response->uri = $uri;
    $response->response_code = $response_code;
    $response->message = $message;
    return json_encode($response);
}

foreach ($_POST as $key => $value) {
    $_POST[$key] = htmlspecialchars(trim($value), ENT_QUOTES, 'UTF-8');
}
extract($_POST);

if (isset($password, $email) && !empty($password) && !empty($email)) {

    $user = User::find_by_query("SELECT * FROM user WHERE email='" . $email . "' LIMIT 1");

    $user = array_shift($user);

    if ($user && $user->password == $password) {
        if ($user->status == 1) {

            if ($user->role == 1) {
                $uri = "admin";
                // $_SESSION['admin_user_id'] = $user->id;
            } else {
                $uri = "user";
            }
            $_SESSION['user_id'] = $user->id;
            echo error_res(false, "00", "Login Successful", $uri);
            exit;
        } else {
            include ENVIRONMENT == 'prod' ? '../../email/mail_verify_prod.php' : '../../email/mail_verify_dev.php';

            // echo error_res(false, "02", "Verify Your Account");
            echo error_res(false, "02", "Verify Your Account");
            exit;
        }
    } else {
        echo error_res(false, "03", "Invalid Login Credentials!");
        exit;
    }
} else {

    echo error_res(false, "03", "Login Error Contact Administrator!");
    exit;
}