<?php

$inputRules = [
    "email" => ["required" => true, "validate" => ["filter" => FILTER_VALIDATE_EMAIL]],
    "password" => ["required" => true, "validate" => []],
];

if (__RB_validateInput($inputRules, 'login')) {
    $json = "";

    if (__RB_api_cloud(__RB_API_ACTION_CLOUD_LOGIN, $_POST, $json)) {
        $_SESSION['id'] = $json['data']['id'];
        $_SESSION['status'] = $json['data']['status'];
        $_SESSION['user_test'] = $json['data']['test'];
        //$_SESSION['lng'] = $json['data']['lng'];
        $_SESSION['address'] = $json['data']['address'];
        $_SESSION['transactions'] = $json['data']['transactions'];
        $_SESSION['product'] = $json['data']['product'];
        $_SESSION['lotteries'] = $json['data']['lotteries'];
        $_SESSION['signed_in'] = true;

        //$__RB_success['login'] = true;
        header('location: /account/my-account.html');

        exit;

    } else {
        if ($json && is_array($json) && !empty($json['code'])) {
            switch ($json['code']) {
                case 1001:
                case 1004:
                    $errorMsg = $__RB_LNG['error']['form']['login']['incorrect'];

                    break;

                case 1002:
                    $errorMsg = $__RB_LNG['error']['form']['login']['disabled_user'];

                    break;
                
                default: $errorMsg = "Process error";
            }

            __RB_errorHandler('login', $json['code'] <= 1 ? 'process' : 'login', $json['code'] <= 1 ? 'Process error' : $errorMsg);

        } else {
            __RB_errorHandler('login', 'process', 'Process error');
        }
    }
}
