<?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="/audit" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div>
            <label class="form-label" style="font-size:12px;">بحث</label>
            <input type="text" name="q" value="<?= e($filters['search']) ?>" placeholder="بحث..." class="form-input" style="min-width:150px;">
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">الإجراء</label>
            <select name="action" class="form-select" style="min-width:120px;">
                <option value="">الكل</option>
                <?php foreach ($actions as $a): ?>
                    <option value="<?= e($a) ?>" <?= $filters['action'] === $a ? 'selected' : '' ?>><?= e($a) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">نوع الكيان</label>
            <select name="entity_type" class="form-select" style="min-width:120px;">
                <option value="">الكل</option>
                <?php foreach ($entityTypes as $et): ?>
                    <option value="<?= e($et) ?>" <?= $filters['entity_type'] === $et ? 'selected' : '' ?>><?= e($et) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">الموظف</label>
            <select name="employee_id" class="form-select" style="min-width:150px;">
                <option value="">الكل</option>
                <?php foreach ($employees as $emp): ?>
                    <option value="<?= (int) $emp['id'] ?>" <?= $filters['employee_id'] == $emp['id'] ? 'selected' : '' ?>><?= e($emp['full_name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">من</label>
            <input type="date" name="date_from" value="<?= e($filters['date_from']) ?>" class="form-input">
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">إلى</label>
            <input type="date" name="date_to" value="<?= e($filters['date_to']) ?>" class="form-input">
        </div>
        <button type="submit" class="btn btn-outline">بحث</button>
        <a href="/audit" 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>IP</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $r): ?>
                <tr>
                    <td style="font-size:12px;white-space:nowrap;"><?= e($r['created_at']) ?></td>
                    <td><?= e($r['employee_name'] ?? '—') ?></td>
                    <td>
                        <?php
                        $actionColors = [
                            'create' => '#059669', 'update' => '#0284C7', 'delete' => '#DC2626',
                            'archive' => '#D97706', 'login' => '#6B7280', 'logout' => '#6B7280',
                            'login_failed' => '#DC2626',
                        ];
                        $color = $actionColors[$r['action']] ?? '#6B7280';
                        ?>
                        <span style="color:<?= $color ?>;font-weight:600;font-size:13px;"><?= e($r['action']) ?></span>
                    </td>
                    <td style="font-size:13px;"><?= e($r['entity_type'] ?? '—') ?></td>
                    <td>
                        <?php if ($r['entity_type'] && $r['entity_id']): ?>
                            <a href="/audit/entity/<?= urlencode($r['entity_type']) ?>/<?= (int) $r['entity_id'] ?>" style="color:#0D7377;">
                                <?= e($r['entity_label'] ?? '#' . $r['entity_id']) ?>
                            </a>
                        <?php else: ?>
                            <?= e($r['entity_label'] ?? '—') ?>
                        <?php endif; ?>
                    </td>
                    <td style="font-size:12px;color:#6B7280;max-width:300px;">
                        <?php if ($r['changed_fields_json']): ?>
                            <?php $fields = json_decode($r['changed_fields_json'], true); ?>
                            <?php if (is_array($fields) && !empty($fields)): ?>
                                <?php
                                $first3 = array_slice($fields, 0, 3, true);
                                foreach ($first3 as $fname => $fchange):
                                    if (is_array($fchange) && isset($fchange['from'])):
                                ?>
                                    <div style="margin-bottom:2px;">
                                        <strong style="color:#374151;"><?= e($fname) ?>:</strong>
                                        <span style="color:#DC2626;text-decoration:line-through;"><?= e(mb_substr((string)($fchange['from'] ?? '—'), 0, 30)) ?></span>
                                        →
                                        <span style="color:#059669;"><?= e(mb_substr((string)($fchange['to'] ?? '—'), 0, 30)) ?></span>
                                    </div>
                                <?php elseif (is_string($fchange)): ?>
                                    <span style="background:#F3F4F6;padding:1px 5px;border-radius:3px;font-size:11px;margin-left:3px;"><?= e($fchange) ?></span>
                                <?php else: ?>
                                    <span style="background:#F3F4F6;padding:1px 5px;border-radius:3px;font-size:11px;margin-left:3px;"><?= e($fname) ?></span>
                                <?php endif; endforeach; ?>
                                <?php if (count($fields) > 3): ?>
                                    <span style="color:#9CA3AF;font-size:11px;">+<?= count($fields) - 3 ?> حقل آخر</span>
                                <?php endif; ?>
                            <?php else: ?>
                                —
                            <?php endif; ?>
                        <?php elseif ($r['notes']): ?>
                            <?= e(mb_substr($r['notes'], 0, 80)) ?>
                        <?php else: ?>
                            —
                        <?php endif; ?>
                    </td>
                    <td style="direction:ltr;text-align:right;font-size:12px;color:#9CA3AF;"><?= e($r['ip_address'] ?? '—') ?></td>
                </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?>
                <tr><td colspan="7" style="text-align:center;padding:40px;color:#6B7280;">لا توجد سجلات</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    <?php if ($pagination['last_page'] > 1): ?>
    <div style="padding:15px;">
        <nav class="pagination-wrapper">
            <ul class="pagination" style="display:flex;gap:5px;list-style:none;padding:0;justify-content:center;">
                <?php if ($pagination['has_prev']): ?>
                    <li><a href="?page=<?= $pagination['prev_page'] ?>&q=<?= urlencode($filters['search']) ?>&action=<?= urlencode($filters['action']) ?>&entity_type=<?= urlencode($filters['entity_type']) ?>&employee_id=<?= urlencode($filters['employee_id']) ?>&date_from=<?= urlencode($filters['date_from']) ?>&date_to=<?= urlencode($filters['date_to']) ?>" class="btn btn-sm btn-outline">السابق</a></li>
                <?php endif; ?>
                <?php foreach ($pagination['pages'] as $p): ?>
                    <?php if ($p === '...'): ?>
                        <li style="padding:5px;">...</li>
                    <?php else: ?>
                        <li><a href="?page=<?= $p ?>&q=<?= urlencode($filters['search']) ?>&action=<?= urlencode($filters['action']) ?>&entity_type=<?= urlencode($filters['entity_type']) ?>&employee_id=<?= urlencode($filters['employee_id']) ?>&date_from=<?= urlencode($filters['date_from']) ?>&date_to=<?= urlencode($filters['date_to']) ?>" class="btn btn-sm <?= $p === $pagination['current_page'] ? 'btn-primary' : 'btn-outline' ?>"><?= $p ?></a></li>
                    <?php endif; ?>
                <?php endforeach; ?>
                <?php if ($pagination['has_next']): ?>
                    <li><a href="?page=<?= $pagination['next_page'] ?>&q=<?= urlencode($filters['search']) ?>&action=<?= urlencode($filters['action']) ?>&entity_type=<?= urlencode($filters['entity_type']) ?>&employee_id=<?= urlencode($filters['employee_id']) ?>&date_from=<?= urlencode($filters['date_from']) ?>&date_to=<?= urlencode($filters['date_to']) ?>" class="btn btn-sm btn-outline">التالي</a></li>
                <?php endif; ?>
            </ul>
        </nav>
    </div>
    <?php endif; ?>
</div>
<?php $__template->endSection(); ?>