<?php


class ISTKR_ServerInfoView
{
    public static function istkr_render_view()
    {
        require_once realpath(__DIR__) . '/../components/header.tpl.php';
        $data = ISTKR_ServerInfoController::istkr_parse_phpinfo();
    ?>
        <div class="container server-info">
            <table class="php-info table">
                <thead>
                    <th>Property</th>
                    <th>Value</th>
                </thead>
                <tbody>
                    <?php foreach ($data as $section_title => $section_content) : ?>
                        <tr class="no-shadow">
                            <td>
                                <h4 class="section-title"><?php echo esc_html($section_title); ?></h4>
                            </td>
                            <td></td>
                        </tr>
                        <?php foreach ($section_content as $sub_key => $sub_value) : ?>
                            <?php if ($sub_key === 'Directive') continue; ?>
                            <tr>
                                <td>
                                    <p class="key"><?php echo esc_html($sub_key); ?></p>
                                </td>
                                <td>
                                    <p class="value">
                                        <?php
                                        if (is_string($sub_value)) {
                                            echo esc_html($sub_value);
                                        } elseif (is_array($sub_value)) {
                                            $formatted_values = array_map(function ($k, $v) {
                                                $label = $k === 0 ? __('Local value', 'istkr') : __('Master value', 'istkr');
                                                return $label . ': ' . esc_html($v);
                                            }, array_keys($sub_value), $sub_value);

                                            echo implode('<br>', $formatted_values);
                                        }
                                        ?>
                                    </p>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php
    }
}
