<?php ob_start(); ?>

<?php
$pluginActions = [];
if ($zipOk) {
    $pluginActions[] = ['url' => '/admin/plugins/upload', 'label' => __('admin.plugins.install'), 'class' => 'btn-primary'];
} else {
    $pluginActions[] = ['url' => '', 'label' => __('admin.plugins.install'), 'class' => 'btn-secondary btn-disabled', 'attrs' => 'title="' . e(__('admin.plugins.zip_not_available')) . '"'];
}
admin_content_header(__('admin.nav.plugins'), 'puzzle', $pluginActions, __('admin.plugins.subtitle'));
?>

<?php if (empty($plugins)): ?>
<div class="form-card" style="max-width:640px;">
    <?php admin_empty_state(
        __('admin.plugins.no_plugins'),
        'puzzle',
        $zipOk ? '/admin/plugins/upload' : '',
        $zipOk ? __('admin.plugins.install_first') : ''
    ); ?>
</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 class="text-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 class="text-secondary"><?= e($plugin['description']) ?></small>
                    <?php endif; ?>
                    <br><small class="text-muted"><?= __('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 class="actions text-right">
                    <?php if ($isActive): ?>
                    <form method="POST" action="/admin/plugins/deactivate" class="inline-form">
                        <?= $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" class="inline-form">
                        <?= $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" class="inline-form"
                          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 class="admin-note">
    <?php
    $noteLabel = __('admin.common.note');
    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'; ?>
