<?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:#0D7377;">سجل التنبيهات</h2>
    <a href="/alerts" class="btn btn-outline" style="display:inline-flex;align-items:center;gap:6px;">
        <i data-lucide="settings" style="width:16px;height:16px;"></i>
        قواعد التنبيهات
    </a>
</div>

<?php if (empty($logs)): ?>
    <div class="card" style="padding:40px;text-align:center;color:#6B7280;">
        <i data-lucide="inbox" 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>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($logs as $log): ?>
                    <?php
                        $context = json_decode($log['context_json'] ?? '{}', true);
                        $statusColors = [
                            'sent'         => 'background:#FEF3C7;color:#92400E;',
                            'failed'       => 'background:#FEE2E2;color:#991B1B;',
                            'acknowledged' => 'background:#D1FAE5;color:#065F46;',
                        ];
                        $statusLabels = [
                            'sent'         => 'مرسل',
                            'failed'       => 'فشل',
                            'acknowledged' => 'تم الإطلاع',
                        ];
                    ?>
                    <tr>
                        <td style="font-weight:600;"><?= e($log['rule_name'] ?? '—') ?></td>
                        <td>
                            <span style="background:#E0F2FE;color:#0369A1;padding:3px 10px;border-radius:12px;font-size:12px;">
                                <?= e($categories[$log['category'] ?? ''] ?? ($log['category'] ?? '—')) ?>
                            </span>
                        </td>
                        <td style="font-size:13px;"><?= e($log['triggered_at']) ?></td>
                        <td style="font-size:13px;max-width:300px;overflow:hidden;text-overflow:ellipsis;">
                            <?= e($context['message'] ?? '—') ?>
                        </td>
                        <td>
                            <span style="<?= $statusColors[$log['status']] ?? '' ?>padding:3px 10px;border-radius:12px;font-size:12px;">
                                <?= e($statusLabels[$log['status']] ?? $log['status']) ?>
                            </span>
                        </td>
                        <td>
                            <?php if ($log['status'] === 'sent'): ?>
                                <?php if (can('alert.manage')): ?>
                                <form method="POST" action="/alerts/log/<?= (int)$log['id'] ?>/acknowledge" style="display:inline;">
                                    <?= csrf_field() ?>
                                    <button type="submit" class="btn btn-sm btn-outline" style="font-size:12px;">
                                        تأكيد الإطلاع
                                    </button>
                                </form>
                                <?php endif; ?>
                            <?php elseif ($log['status'] === 'acknowledged'): ?>
                                <span style="color:#6B7280;font-size:12px;">✓ تم</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
<?php endif; ?>

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