<?php

use QUI\MasterLogin\Manager;
use QUI\Utils\Grid;
use QUI\Utils\Security\Orthos;

QUI::getAjax()->registerFunction(
    'package_quiqqer_masterlogin_ajax_frontend_searchUsers',
    /**
     * Search slave users
     *
     * @throws QUI\Exception
     */
    function ($searchParams) {
        Manager::checkUsePermission();

        try {
            $searchParams = Orthos::clearArray(json_decode($searchParams, true));
            $users = Manager::searchLoginUsers($searchParams);

            if (!is_array($users)) {
                $users = [];
            }

            $searchParams['count'] = true;
            $count = Manager::searchLoginUsers($searchParams);

            if (!is_int($count)) {
                $count = 0;
            }

            $Grid = new Grid($searchParams);

            return $Grid->parseResult($users, $count);
        } catch (Exception $Exception) {
            QUI\System\Log::writeException($Exception);
            return false;
        }
    },
    ['searchParams'],
    'Permission::checkUser'
);
