<?php ob_start(); ?>

<?php admin_content_header(__('admin.plugins.upload_title'), 'puzzle', [
    ['url' => '/admin/plugins', 'label' => __('admin.plugins.back')],
], __('admin.plugins.upload_text')); ?>

<div style="display:grid; grid-template-columns:1fr 1fr; gap:1.5rem; align-items:start; max-width:900px;">

    <!-- Upload form -->
    <div>
        <form method="POST" action="/admin/plugins/upload" enctype="multipart/form-data" class="admin-form" id="pluginUploadForm">
            <?= $csrf->field() ?>

            <div class="form-card">
                <h4><?= __('admin.plugins.upload_title') ?></h4>

                <div class="form-group">
                    <label for="plugin_zip"><?= __('admin.plugins.upload_label') ?></label>
                    <input type="file" id="plugin_zip" name="plugin_zip" accept=".zip" required
                           style="display:block; width:100%; padding:0.45rem 0.6rem;
                                  border:1px solid var(--gray-300,#d1d5db); border-radius:5px;
                                  background:#fff; font-size:0.875rem; box-sizing:border-box;">
                    <small class="form-hint"><?= __('admin.plugins.upload_hint') ?></small>
                </div>

                <div style="display:flex; gap:0.625rem; align-items:center; margin-top:1rem;">
                    <button type="submit" class="btn btn-primary" id="uploadBtn">
                        <?= __('admin.plugins.upload_btn') ?>
                    </button>
                    <a href="/admin/plugins" class="btn btn-secondary"><?= __('admin.common.cancel') ?></a>
                </div>
            </div>
        </form>

        <div class="form-card" style="margin-top:1rem; border-left:3px solid var(--warning,#f59e0b); background:var(--warning-bg,#fffbeb);">
            <p style="margin:0; font-size:0.82rem; color:var(--gray-700,#374151);">
                <strong><?= __('admin.common.security') ?? 'Security' ?>:</strong> <?= __('admin.plugins.security_note') ?>
            </p>
        </div>

        <script>
        document.getElementById('pluginUploadForm').addEventListener('submit', function() {
            var btn = document.getElementById('uploadBtn');
            btn.textContent = '<?= __('admin.common.loading') ?>';
            btn.disabled = true;
        });
        </script>
    </div>

    <!-- Package format reference -->
    <div class="form-card">
        <h4><?= __('admin.plugins.format_title') ?></h4>
        <p style="font-size:0.85rem; color:var(--gray-600,#4b5563); margin:0 0 0.75rem;">
            A valid plugin package is a <code>.zip</code> file containing:
        </p>
        <pre style="background:var(--gray-50,#f9fafb); border:1px solid var(--gray-200,#e5e7eb);
                    padding:0.75rem 1rem; border-radius:6px; font-size:0.78rem;
                    overflow:auto; margin:0 0 0.875rem; line-height:1.6;"><?= htmlspecialchars(
'plugin-name.zip
├── plugin.json      ← required
├── plugin.php       ← required (entry point)
└── assets/          ← optional
    ├── css/
    └── js/'
) ?></pre>

        <p style="font-size:0.83rem; font-weight:600; margin:0 0 0.4rem; color:var(--gray-700,#374151);">
            Required <code>plugin.json</code>:
        </p>
        <pre style="background:var(--gray-50,#f9fafb); border:1px solid var(--gray-200,#e5e7eb);
                    padding:0.75rem 1rem; border-radius:6px; font-size:0.78rem;
                    overflow:auto; margin:0 0 0.875rem; line-height:1.6;"><?= htmlspecialchars(
'{
  "slug":        "my-plugin",
  "name":        "My Plugin",
  "version":     "1.0.0",
  "author":      "Your Name",
  "description": "Short description.",
  "min_cms":     "1.0.17"
}'
) ?></pre>

        <ul style="font-size:0.82rem; margin:0; padding-left:1.1rem;
                   color:var(--gray-600,#4b5563); line-height:1.8;">
            <li><strong>slug</strong>: lowercase letters, digits and hyphens only</li>
            <li><strong>plugin.php</strong>: entry point file is required</li>
            <li><strong>min_cms</strong>: optional minimum CMS version check</li>
            <li>The ZIP can be flat or wrapped in one subdirectory</li>
            <li>Assets are automatically mirrored to <code>public/plugins/</code></li>
            <li>Re-uploading the same slug updates the plugin</li>
        </ul>
    </div>

</div>

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