<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>قوالب الرسائل النصية<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div class="card">
    <?php foreach ($templates as $t): ?>
    <div style="padding:20px;border-bottom:1px solid #E5E7EB;">
        <form method="POST" action="/notifications/templates/<?= (int) $t['id'] ?>">
            <?= csrf_field() ?>
            <div style="display:flex;justify-content:space-between;align-items:start;margin-bottom:10px;">
                <div>
                    <strong style="color:#0D7377;"><?= e($t['name_ar']) ?></strong>
                    <code style="font-size:11px;color:#6B7280;margin-right:10px;"><?= e($t['template_code']) ?></code>
                    <?php if ($t['trigger_event']): ?><span style="font-size:11px;background:#EFF6FF;color:#0284C7;padding:2px 6px;border-radius:4px;">حدث: <?= e($t['trigger_event']) ?></span><?php endif; ?>
                </div>
                <div style="display:flex;gap:8px;align-items:center;">
                    <select name="is_active" class="form-select" style="width:auto;font-size:12px;">
                        <option value="1" <?= $t['is_active'] ? 'selected' : '' ?>>مفعّل</option>
                        <option value="0" <?= !$t['is_active'] ? 'selected' : '' ?>>معطّل</option>
                    </select>
                    <button type="submit" class="btn btn-sm btn-primary">حفظ</button>
                </div>
            </div>
            <input type="hidden" name="name_ar" value="<?= e($t['name_ar']) ?>">
            <textarea name="message_template_ar" class="form-textarea" rows="2" style="font-size:13px;"><?= e($t['message_template_ar']) ?></textarea>
            <?php if ($t['variables_json']): ?>
                <?php $vars = json_decode($t['variables_json'], true) ?? []; ?>
                <div style="margin-top:5px;font-size:11px;color:#9CA3AF;">المتغيرات: <?= e(implode(', ', array_map(fn($v) => '{' . $v . '}', $vars))) ?></div>
            <?php endif; ?>
        </form>
    </div>
    <?php endforeach; ?>
    <?php if (empty($templates)): ?>
        <div style="padding:40px;text-align:center;color:#6B7280;">لا توجد قوالب</div>
    <?php endif; ?>
</div>
<?php $__template->endSection(); ?>