<?php
/**
 * @var \App\Assets $assets
 * @var array $admin_panels
 */

$this->layout('main', [
    'title' => 'Administration',
    'manual' => true,
    'page_class' => 'page-admin',
]);

$assets
    ->load('luxon')
    ->addInlineJs($this->fetch('admin/index/index.js'), 99);
?>

<h2 class="outside-card-header mb-1"><?=__('Administration')?></h2>

<div class="card-columns">
    <?php foreach ($admin_panels as $category_id => $category): ?>
        <section class="card" role="region">
            <div class="card-header bg-primary-dark">
                <h2 class="card-title"><?=$category['label']?></h2>
            </div>
            <div class="list-group list-group-flush">
                <?php foreach ($category['items'] as $item): ?>
                    <a class="list-group-item list-group-item-action" href="<?=$item['url']?>" title="<?=($item['title'] ?? '')?>">
                        <?=$item['label']?>
                    </a>
                <?php endforeach; ?>
            </div>
        </section>
    <?php endforeach; ?>
</div>

<h2 class="outside-card-header mb-1"><?=__('Server Status')?></h2>

<div class="card-deck">
    <div class="card" role="region">
        <div class="card-header bg-primary-dark">
            <h2 class="card-title"><?=__('CPU Load')?></h2>
        </div>
        <ul class="list-group list-group-flush">
            <li class="list-group-item">
                <p class="card-text mb-0">
                    <strong><?=round($load[0], 2)?></strong>
                    <small><?=__('Current')?></small>
                </p>
            </li>
            <li class="list-group-item">
                <p class="card-text mb-0">
                    <strong><?= round($load[2], 2) ?></strong>
                    <small><?= __('15-Minute Average') ?></small>
                </p>
            </li>
        </ul>
    </div>

    <div class="card" role="region">
        <div class="card-header bg-primary-dark">
            <h2 class="card-title"><?= __('Memory') ?></h2>
        </div>
        <div class="card-body">
            <div class="progress h-20 mb-1">
                <div class="progress-bar" role="progressbar" aria-valuenow="<?= $memory_percent ?>" aria-valuemin="0"
                     aria-valuemax="100" style="width: <?= $memory_percent ?>%;">
                    <?= $memory_percent ?>%
                </div>
            </div>
            <h3><small><?= __('%s of %s Used', $memory_used, $memory_total) ?></small></h3>
        </div>
    </div>

    <div class="card" role="region">
        <div class="card-header bg-primary-dark">
            <h2 class="card-title"><?= __('Disk Space') ?></h2>
        </div>
        <div class="card-body">
            <div class="progress h-20 mb-1">
                <div class="progress-bar" role="progressbar" aria-valuenow="<?= $space_percent ?>" aria-valuemin="0"
                     aria-valuemax="100" style="width: <?= $space_percent ?>%;">
                    <?= $space_percent ?>%
                </div>
            </div>
            <h3><small><?= __('%s of %s Used', $space_used, $space_total) ?></small></h3>
        </div>
    </div>
</div>

<h2 class="outside-card-header mb-1"><?= __('Synchronization Tasks') ?></h2>

<div class="card-deck">
    <?php
    foreach ($sync_times as $sync_key => $sync_info): ?>
        <section class="card" role="region">
            <div class="card-header bg-primary-dark">
                <h2 class="card-title"><?= $sync_info['name'] ?></h2>
                <h3 class="card-subtitle"><?= implode(', ', $sync_info['contents']) ?></h3>
            </div>
            <div class="card-body">
                <p class="card-text"><?= __(
                        'Last run: %s',
                        '<time data-duration="' . $sync_info['latest'] . '"></time>'
                    )?></p>
            </div>
            <div class="card-actions">
                <a class="btn btn-outline-primary" role="button" href="<?=$router->named(
                    'admin:debug:sync',
                    ['type' => $sync_key]
                )?>">
                    <i class="material-icons" aria-hidden="true">send</i>
                    <?=__('Run Task')?>
                </a>
            </div>
        </section>
    <?php endforeach; ?>
</div>
