<?php
declare(strict_types=1);

function service_management_page_url(string $page, array $params = array()): string
{
    $allowedPages = array(
        'services.php',
        'service_categories.php',
        'service_groups.php',
    );

    if (!in_array($page, $allowedPages, true)) {
        $page = 'services.php';
    }

    $filteredParams = array();
    foreach ($params as $key => $value) {
        if ($value === null || $value === '') {
            continue;
        }

        $filteredParams[$key] = $value;
    }

    if (empty($filteredParams)) {
        return $page;
    }

    return $page . '?' . http_build_query($filteredParams);
}

function render_service_management_tabs(string $activePage): string
{
    $items = array(
        array('services.php', 'services', ui_text('รายการบริการ', 'Service list')),
        array('service_categories.php', 'service_categories', ui_text('รายการหมวดหมู่', 'Category list')),
        array('service_groups.php', 'service_groups', ui_text('รายการกลุ่มบริการ', 'Service group list')),
    );

    $links = array();
    foreach ($items as $item) {
        $href = $item[0];
        $pageKey = $item[1];
        $label = $item[2];
        $class = $activePage === $pageKey ? 'button button-accent' : 'button button-secondary';
        $links[] = '<a class="' . e($class) . '" href="' . e($href) . '">' . e($label) . '</a>';
    }

    return '<section class="service-management-tabs">' . implode('', $links) . '</section>';
}
