<?php $pageTitle = t('plugins'); ?>
<div class="page-header">
    <div class="page-header-left">
        <h1 class="page-title"><?= t('plugins') ?></h1>
    </div>
    <div class="page-header-actions">
        <button type="button" class="btn btn-primary btn-sm" data-modal-open="uploadPluginModal">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="14" height="14"><path d="M21 15v4a2 2 0 01-2 2H5a2 2 0 01-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></svg>
            <?= t('plugin_upload_title') ?>
        </button>
    </div>
</div>

<?php if (!empty($success)): ?>
<div class="alert alert-success"><?= Helper::e($success) ?></div>
<?php endif; ?>
<?php if (!empty($errors)): ?>
<div class="alert alert-error"><?php foreach ($errors as $e): ?><div><?= Helper::e($e) ?></div><?php endforeach; ?></div>
<?php endif; ?>

<div class="card">
    <div class="card-header"><h2 class="card-title"><?= t('installed_plugins') ?></h2></div>
    <?php if (empty($plugins)): ?>
    <div class="card-body">
        <p class="text-muted"><?= t('no_plugins') ?></p>
    </div>
    <?php else: ?>
    <div class="card-body p-0">
        <table class="data-table">
            <thead>
                <tr>
                    <th><?= t('name') ?></th>
                    <th><?= t('plugin_version') ?></th>
                    <th><?= t('description') ?></th>
                    <th><?= t('status_label') ?></th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($plugins as $plugin): ?>
            <?php
                $canDisable = PluginManager::filter('plugin_can_disable', true, ['id' => $plugin['id']]);
                $canDelete  = PluginManager::filter('plugin_can_delete',  true, ['id' => $plugin['id'], 'dir' => $plugin['dir_name']]);
                $lockHint   = t('plugin_lock_active_hint');
            ?>
            <tr>
                <td><strong><?= Helper::e($plugin['name'] ?? $plugin['id']) ?></strong></td>
                <td><span class="text-muted"><?= Helper::e($plugin['version'] ?? '–') ?></span></td>
                <td class="text-muted" style="font-size:.85rem"><?= !empty($plugin['description_key']) ? t($plugin['description_key']) : Helper::e($plugin['description'] ?? '') ?></td>
                <td>
                    <?php if ($plugin['enabled']): ?>
                    <span class="badge badge-success"><?= t('plugin_active') ?></span>
                    <?php else: ?>
                    <span class="badge badge-neutral"><?= t('plugin_disabled') ?></span>
                    <?php endif; ?>
                </td>
                <td>
                    <div class="action-btns">
                        <?php if ($plugin['enabled'] && !empty($plugin['settings_url'])): ?>
                        <a href="<?= Helper::e($plugin['settings_url']) ?>" class="btn btn-ghost btn-xs">
                            <?= t('plugin_settings') ?>
                        </a>
                        <?php endif; ?>
                        <?php if ($plugin['enabled']): ?>
                        <form method="POST">
                            <?= Helper::csrf() ?>
                            <input type="hidden" name="action" value="disable">
                            <input type="hidden" name="plugin_id" value="<?= Helper::e($plugin['id']) ?>">
                            <button type="submit" class="btn btn-ghost btn-xs"
                                <?= !$canDisable ? 'disabled title="' . $lockHint . '"' : '' ?>>
                                <?= t('plugin_deactivate') ?>
                            </button>
                        </form>
                        <?php else: ?>
                        <form method="POST">
                            <?= Helper::csrf() ?>
                            <input type="hidden" name="action" value="enable">
                            <input type="hidden" name="plugin_id" value="<?= Helper::e($plugin['id']) ?>">
                            <button type="submit" class="btn btn-ghost btn-xs"><?= t('plugin_activate') ?></button>
                        </form>
                        <?php endif; ?>
                        <form method="POST" data-confirm="<?= Helper::e(t('plugin_delete_confirm')) ?>">
                            <?= Helper::csrf() ?>
                            <input type="hidden" name="action" value="delete">
                            <input type="hidden" name="plugin_dir" value="<?= Helper::e($plugin['dir_name']) ?>">
                            <button type="submit" class="btn btn-ghost btn-xs danger"
                                <?= !$canDelete ? 'disabled title="' . $lockHint . '"' : '' ?>>
                                <?= t('delete') ?>
                            </button>
                        </form>
                    </div>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>

<!-- Upload modal — opened from the page-header button. Replaces the previous
     side-column upload card and the now-removed plugin-structure block. -->
<div class="modal" id="uploadPluginModal">
    <div class="modal-backdrop" data-modal-close="uploadPluginModal"></div>
    <div class="modal-box">
        <div class="modal-header">
            <h2 class="modal-title"><?= t('plugin_upload_title') ?></h2>
            <button class="modal-close" data-modal-close="uploadPluginModal">×</button>
        </div>
        <form method="POST" enctype="multipart/form-data">
            <?= Helper::csrf() ?>
            <input type="hidden" name="action" value="upload">
            <div class="modal-body">
                <p class="text-muted" style="margin-bottom:1rem;font-size:.9rem">
                    <?= tr('plugin_upload_hint') ?>
                </p>
                <div class="form-group">
                    <label class="form-label"><?= t('plugin_upload_label') ?></label>
                    <input type="file" name="plugin_zip" class="form-input" accept=".zip" required>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-ghost" data-modal-close="uploadPluginModal"><?= t('cancel') ?></button>
                <button type="submit" class="btn btn-primary"><?= t('plugin_upload_btn') ?></button>
            </div>
        </form>
    </div>
</div>
