<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>محرك القواعد<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/rules" style="display:flex;gap:10px;align-items:end;">
        <div>
            <label class="form-label" style="font-size:12px;">التصنيف</label>
            <select name="category" class="form-select" style="min-width:150px;">
                <option value="">الكل</option>
                <?php foreach ($categories as $c): ?>
                    <option value="<?= e($c) ?>" <?= $category === $c ? 'selected' : '' ?>><?= e($c) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="btn btn-outline">تصفية</button>
        <a href="/rules" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>
<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>
                    <th>الإجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rules as $r): ?>
                <tr>
                    <td><code style="font-size:11px;"><?= e($r['rule_code']) ?></code></td>
                    <td><?= e($r['name_ar']) ?></td>
                    <td><span style="background:#F3F4F6;padding:2px 8px;border-radius:4px;font-size:12px;"><?= e($r['category']) ?></span></td>
                    <td style="font-size:12px;"><?= e($r['data_type']) ?></td>
                    <td style="font-size:12px;max-width:200px;overflow:hidden;text-overflow:ellipsis;">
                        <?php $val = json_decode($r['current_value_json'], true); ?>
                        <?php if (is_array($val)): ?>
                            <?php foreach ($val as $k => $v): ?>
                                <span style="color:#6B7280;"><?= e($k) ?>:</span> <strong><?= e((string)$v) ?></strong><?= $k !== array_key_last($val) ? ', ' : '' ?>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <?= e($r['current_value_json']) ?>
                        <?php endif; ?>
                    </td>
                    <td style="text-align:center;"><?= (int) $r['version'] ?></td>
                    <td style="font-size:12px;"><?= $r['branch_id'] ? '#' . (int) $r['branch_id'] : 'عام' ?></td>
                    <td>
                        <div style="display:flex;gap:5px;">
                            <a href="/rules/<?= (int) $r['id'] ?>/edit" class="btn btn-sm btn-outline">تعديل</a>
                            <a href="/rules/<?= (int) $r['id'] ?>/history" class="btn btn-sm btn-outline">السجل</a>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php if (empty($rules)): ?>
                <tr><td colspan="8" style="text-align:center;padding:40px;color:#6B7280;">لا توجد قواعد</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $__template->endSection(); ?>