<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>التنبيهات<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:20px;">
    <h2 style="margin:0;color:#1e3a5f;">قواعد التنبيهات</h2>
    <a href="/alerts/log" class="btn btn-outline" style="display:inline-flex;align-items:center;gap:6px;">
        <i data-lucide="list" style="width:16px;height:16px;"></i>
        سجل التنبيهات
    </a>
</div>

<?php if (empty($rules)): ?>
    <div class="card" style="padding:40px;text-align:center;color:#6B7280;">
        <i data-lucide="bell-off" style="width:48px;height:48px;margin-bottom:12px;opacity:0.5;"></i>
        <p>لا توجد قواعد تنبيهات مسجلة بعد.</p>
    </div>
<?php else: ?>
    <div class="card">
        <div class="table-responsive">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>اسم التنبيه</th>
                        <th>التصنيف</th>
                        <th>نوع المحفز</th>
                        <th>قنوات الإشعار</th>
                        <th>الحالة</th>
                        <th>آخر تشغيل</th>
                        <th>إجراءات</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($rules as $rule): ?>
                    <tr>
                        <td style="font-weight:600;"><?= e($rule['name_ar']) ?></td>
                        <td>
                            <span style="background:#E0F2FE;color:#0369A1;padding:3px 10px;border-radius:12px;font-size:12px;">
                                <?= e($categories[$rule['category']] ?? $rule['category']) ?>
                            </span>
                        </td>
                        <td>
                            <span style="background:#F3F4F6;padding:2px 8px;border-radius:4px;font-size:12px;">
                                <?= e($triggerTypes[$rule['trigger_type']] ?? $rule['trigger_type']) ?>
                            </span>
                        </td>
                        <td style="font-size:13px;"><?= e($rule['notification_channels']) ?></td>
                        <td>
                            <?php if ((int)$rule['is_active'] === 1): ?>
                                <span style="background:#D1FAE5;color:#065F46;padding:3px 10px;border-radius:12px;font-size:12px;">مفعّل</span>
                            <?php else: ?>
                                <span style="background:#FEE2E2;color:#991B1B;padding:3px 10px;border-radius:12px;font-size:12px;">متوقف</span>
                            <?php endif; ?>
                        </td>
                        <td style="font-size:13px;color:#6B7280;">
                            <?= $rule['last_triggered_at'] ? e($rule['last_triggered_at']) : '—' ?>
                        </td>
                        <td>
                            <?php if (can('alert.manage')): ?>
                            <form method="POST" action="/alerts/<?= (int)$rule['id'] ?>/toggle" style="display:inline;">
                                <?= csrf_field() ?>
                                <button type="submit" class="btn btn-sm <?= (int)$rule['is_active'] === 1 ? 'btn-outline-danger' : 'btn-outline-success' ?>" style="font-size:12px;">
                                    <?= (int)$rule['is_active'] === 1 ? 'إيقاف' : 'تفعيل' ?>
                                </button>
                            </form>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
<?php endif; ?>

<?php $__template->endSection(); ?>
