<?php
$viewModel = $block->getData('view_model');
$overview = $viewModel->getOverview();
$lastUpdated = $viewModel->getLastUpdated();
$tools = $viewModel->getTools();
?>
<div class="admin__page-section">
    <?php if ($overview['error'] !== null): ?>
        <div class="message message-error">
            <?= $escaper->escapeHtml($overview['error']) ?>
        </div>
    <?php else: ?>
        <div class="aget-stats">
            <?php foreach ($overview['stats'] as $stat): ?>
                <div class="aget-stat aget-stat--<?= $escaper->escapeHtmlAttr($stat['tone']) ?>">
                    <span class="aget-stat__value">
                        <?= $escaper->escapeHtml(number_format((float)$stat['value'], 0, ',', '.')) ?>
                    </span>
                    <span class="aget-stat__label"><?= $escaper->escapeHtml($stat['label']) ?></span>
                </div>
            <?php endforeach; ?>
        </div>

        <div class="aget-refresh">
            <form action="<?= $escaper->escapeUrl($viewModel->getRefreshUrl()) ?>" method="post">
                <input name="form_key" type="hidden" value="<?= $escaper->escapeHtmlAttr($viewModel->getFormKey()) ?>"/>
                <button type="submit" class="action-default scalable">
                    <span><?= $escaper->escapeHtml(__('Refresh data')) ?></span>
                </button>
            </form>
            <p class="aget-refresh__note">
                <?php if ($lastUpdated !== null): ?>
                    <?= $escaper->escapeHtml(__('Last updated: %1', $lastUpdated)) ?>
                <?php else: ?>
                    <?= $escaper->escapeHtml(__('The statistics have not been calculated yet.')) ?>
                <?php endif; ?>
            </p>
        </div>
    <?php endif; ?>

    <div class="aget-grid">
        <?php foreach ($tools as $tool): ?>
            <div class="aget-card aget-card--<?= $escaper->escapeHtmlAttr($tool['accent']) ?>">
                <div>
                    <h3 class="aget-card__title"><?= $escaper->escapeHtml($tool['title']) ?></h3>
                    <p class="aget-card__desc"><?= $escaper->escapeHtml($tool['description']) ?></p>
                </div>
                <div class="aget-card__actions">
                    <?php foreach ($tool['actions'] as $action): ?>
                        <?php if ($action['post']): ?>
                            <form action="<?= $escaper->escapeUrl($action['url']) ?>" method="post">
                                <input name="form_key" type="hidden"
                                       value="<?= $escaper->escapeHtmlAttr($viewModel->getFormKey()) ?>"/>
                                <?php foreach ($action['params'] as $name => $value): ?>
                                    <input name="<?= $escaper->escapeHtmlAttr($name) ?>" type="hidden"
                                           value="<?= $escaper->escapeHtmlAttr($value) ?>"/>
                                <?php endforeach; ?>
                                <button type="submit"
                                        class="action-default scalable<?= $action['primary'] ? ' primary' : '' ?>">
                                    <span><?= $escaper->escapeHtml($action['label']) ?></span>
                                </button>
                            </form>
                        <?php else: ?>
                            <a href="<?= $escaper->escapeUrl($action['url']) ?>"
                               class="action-default scalable<?= $action['primary'] ? ' primary' : '' ?>">
                                <span><?= $escaper->escapeHtml($action['label']) ?></span>
                            </a>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>
