<?php ob_start(); ?>

<div class="content-header">
    <div>
        <h2><?= __('admin.nav.plugins') ?></h2>
        <p style="margin:0; font-size:0.875rem; color:var(--gray-500,#6b7280);"><?= __('admin.plugins.subtitle') ?></p>
    </div>
    <div class="header-actions">
        <?php if ($zipOk): ?>
        <a href="/admin/plugins/upload" class="btn btn-primary"><?= __('admin.plugins.install') ?></a>
        <?php else: ?>
        <span class="btn btn-secondary" style="opacity:.55; cursor:not-allowed;"
              title="<?= __('admin.plugins.zip_not_available') ?>"><?= __('admin.plugins.install') ?></span>
        <?php endif; ?>
    </div>
</div>

<?php if (empty($plugins)): ?>
<div class="form-card" style="max-width:640px; text-align:center; padding:2.5rem 2rem;">
    <p style="font-size:1.1rem; color:var(--gray-500,#6b7280); margin:0 0 1rem;"><?= __('admin.plugins.no_plugins') ?></p>
    <p style="font-size:0.875rem; color:var(--gray-400,#9ca3af); margin:0 0 1.5rem;">
        <?= __('admin.plugins.no_plugins_hint') ?>
    </p>
    <?php if ($zipOk): ?>
    <a href="/admin/plugins/upload" class="btn btn-primary"><?= __('admin.plugins.install_first') ?></a>
    <?php endif; ?>
</div>

<?php else: ?>
<div class="table-responsive">
    <table class="data-table">
        <thead>
            <tr>
                <th><?= __('admin.plugins.plugin') ?></th>
                <th><?= __('admin.plugins.version') ?></th>
                <th><?= __('admin.plugins.author') ?></th>
                <th><?= __('admin.common.status') ?></th>
                <th style="text-align:right;"><?= __('admin.common.actions') ?></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($plugins as $slug => $plugin):
                $isActive = in_array($slug, $activeSlugs, true);
            ?>
            <tr>
                <td>
                    <strong><?= e($plugin['name']) ?></strong>
                    <?php if (!empty($plugin['description'])): ?>
                    <br><small style="color:var(--gray-500,#6b7280);"><?= e($plugin['description']) ?></small>
                    <?php endif; ?>
                    <br><small style="color:var(--gray-400,#9ca3af);"><?= __('admin.plugins.slug_label', [':slug' => e($slug)]) ?></small>
                </td>
                <td>v<?= e($plugin['version']) ?></td>
                <td><?= e($plugin['author'] ?: '—') ?></td>
                <td>
                    <?php if ($isActive): ?>
                    <span class="badge badge-success"><?= __('admin.common.active') ?></span>
                    <?php else: ?>
                    <span class="badge badge-draft"><?= __('admin.common.inactive') ?></span>
                    <?php endif; ?>
                </td>
                <td style="text-align:right; white-space:nowrap;">
                    <?php if ($isActive): ?>
                    <form method="POST" action="/admin/plugins/deactivate" style="display:inline;">
                        <?= $csrf->field() ?>
                        <input type="hidden" name="plugin" value="<?= e($slug) ?>">
                        <button type="submit" class="btn btn-secondary btn-xs"><?= __('admin.plugins.deactivate') ?></button>
                    </form>
                    <?php else: ?>
                    <form method="POST" action="/admin/plugins/activate" style="display:inline;">
                        <?= $csrf->field() ?>
                        <input type="hidden" name="plugin" value="<?= e($slug) ?>">
                        <button type="submit" class="btn btn-primary btn-xs"><?= __('admin.plugins.activate') ?></button>
                    </form>
                    <?php endif; ?>
                    <form method="POST" action="/admin/plugins/uninstall" style="display:inline;"
                          onsubmit="return confirm('<?= __('admin.plugins.confirm_uninstall', [':name' => e(addslashes($plugin['name']))]) ?>')">
                        <?= $csrf->field() ?>
                        <input type="hidden" name="plugin" value="<?= e($slug) ?>">
                        <button type="submit" class="btn btn-danger btn-xs"><?= __('admin.common.delete') ?></button>
                    </form>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>
<?php endif; ?>

<div style="margin-top:1.5rem; padding:0.875rem 1rem; background:var(--gray-50,#f9fafb);
            border:1px solid var(--gray-200,#e5e7eb); border-radius:6px;
            font-size:0.82rem; color:var(--gray-600,#4b5563); max-width:700px;">
    <?php
    $noteLabel = __('admin.common.note');
    // Fallback: if translation returns the raw key, use hardcoded label
    if ($noteLabel === 'admin.common.note') {
        $noteLabel = (setting('locale', 'de') === 'de') ? 'Hinweis' : 'Note';
    }
    ?>
    <strong><?= e($noteLabel) ?>:</strong> <?= __('admin.plugins.activation_note') ?>
</div>

<?php $content = ob_get_clean(); include dirname(__DIR__) . '/layouts/admin.php'; ?>
