<?php ob_start(); ?>

<?php
admin_content_header(__('admin.marketplace.licenses'), 'key-round', [
    ['url' => admin_url('marketplace'), 'label' => __('admin.marketplace.back_to_marketplace'), 'class' => 'btn-secondary'],
]);
?>

<?php if (empty($installed)): ?>
<?php admin_empty_state(__('admin.marketplace.no_licenses'), 'key-round', admin_url('marketplace'), __('admin.marketplace.browse')); ?>

<?php else: ?>
<div class="table-responsive">
    <table class="data-table">
        <thead>
            <tr>
                <th>Name</th>
                <th>Typ</th>
                <th><?= __('admin.marketplace.version') ?></th>
                <th><?= __('admin.marketplace.license_key') ?></th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($installed as $pkg): ?>
            <tr>
                <td>
                    <strong><?= e($pkg['name'] ?? $pkg['slug']) ?></strong>
                    <br><small class="text-muted"><?= e($pkg['slug']) ?></small>
                </td>
                <td>
                    <span class="mp-badge mp-badge--<?= e($pkg['type'] ?? 'plugin') ?>">
                        <?= ($pkg['type'] ?? 'plugin') === 'theme' ? __('admin.marketplace.type_theme') : __('admin.marketplace.type_plugin') ?>
                    </span>
                </td>
                <td>v<?= e($pkg['version'] ?? '') ?></td>
                <td colspan="2">
                    <form method="POST" action="<?= admin_url('marketplace/licenses') ?>" style="display:flex; gap:.5rem; align-items:center;">
                        <?= $csrf->field() ?>
                        <input type="hidden" name="slug" value="<?= e($pkg['slug']) ?>">
                        <input type="text" name="license_key" class="form-control" style="max-width:340px; font-size:.85rem;"
                               value="<?= e($pkg['license_key'] ?? '') ?>"
                               placeholder="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">
                        <button type="submit" class="btn btn-secondary btn-sm"><?= icon('save', 16) ?></button>
                    </form>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>
<?php endif; ?>

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