<?php

/**
 * Return the info popup entries of a project as grid data.
 *
 * @param string $project - encoded project data
 * @return array{data: array<int, array<string, mixed>>, total: int}
 */

use QUI\InfoPopup\Handler;

QUI::getAjax()->registerFunction(
    'package_quiqqer_info-popup_ajax_list',
    function ($project) {
        $Project = QUI::getProjectManager()->decode($project);
        $lang = QUI::getLocale()->getCurrent();

        $data = [];

        foreach (Handler::getList($Project) as $row) {
            $data[] = [
                'id' => (int)$row['id'],
                'active' => empty($row['active']) ? 0 : 1,
                'title' => Handler::decodeMultiLang($row['title'] ?? '', $lang),
                'validFrom' => (string)($row['validFrom'] ?? ''),
                'validTo' => (string)($row['validTo'] ?? ''),
                'visibility' => (string)($row['visibility'] ?? 'always'),
                'priority' => (int)($row['priority'] ?? 0)
            ];
        }

        return [
            'data' => $data,
            'total' => count($data)
        ];
    },
    ['project'],
    'quiqqer.infoPopup.edit'
);
