<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>منشئ النماذج<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<div style="display:grid;grid-template-columns:250px 1fr;gap:20px;">
    <div class="card" style="padding:15px;">
        <h4 style="margin-bottom:15px;color:#0D7377;">النماذج المتاحة</h4>
        <?php foreach ($schemas as $s): ?>
        <a href="/forms/builder/<?= (int) $s['id'] ?>" class="btn btn-sm <?= ($schema && (int) $schema->id === (int) $s['id']) ? 'btn-primary' : 'btn-outline' ?>" style="display:block;margin-bottom:8px;text-align:right;">
            <?= e($s['name_ar']) ?> <small style="color:#9CA3AF;">(v<?= (int) $s['version'] ?>)</small>
        </a>
        <?php endforeach; ?>
    </div>

    <div>
        <?php if ($schema): ?>
        <div class="card" style="margin-bottom:20px;padding:15px;">
            <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:15px;">
                <h3 style="margin:0;color:#0D7377;"><?= e($schema->name_ar) ?> — الإصدار <?= (int) $schema->version ?></h3>
                <span style="font-size:12px;color:#6B7280;"><?= e($schema->form_code) ?></span>
            </div>
            <form method="POST" action="/forms/builder/<?= (int) $schema->id ?>">
                <?= csrf_field() ?>
                <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:15px;">
                    <div class="form-group">
                        <label class="form-label">الاسم بالعربي</label>
                        <input type="text" name="name_ar" value="<?= e($schema->name_ar) ?>" class="form-input">
                    </div>
                    <div class="form-group">
                        <label class="form-label">رسوم النموذج (ج.م)</label>
                        <input type="number" name="form_fee" value="<?= e($schema->form_fee) ?>" class="form-input" step="0.01">
                    </div>
                </div>
                <div class="form-group">
                    <label class="form-label">هيكل النموذج (JSON)</label>
                    <textarea name="schema_json" class="form-textarea" rows="25" style="font-family:monospace;direction:ltr;text-align:left;font-size:12px;"><?= e($schema->schema_json) ?></textarea>
                </div>
                <div style="margin-top:15px;display:flex;gap:10px;">
                    <button type="submit" class="btn btn-primary">حفظ التعديلات</button>
                    <a href="/forms/render/<?= e($schema->form_code) ?>" class="btn btn-outline" target="_blank">معاينة</a>
                </div>
            </form>
        </div>
        <?php else: ?>
        <div class="card" style="padding:40px;text-align:center;color:#6B7280;">
            <p>اختر نموذجاً من القائمة لتعديله</p>
        </div>
        <?php endif; ?>
    </div>
</div>
<?php $__template->endSection(); ?>