<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تاريخ الكيان: <?= e($entityType) ?> #<?= $entityId ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<a href="/audit" class="btn btn-outline">← العودة لسجل المراجعة</a>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div class="card">
    <?php if (empty($history)): ?>
        <div style="padding:40px;text-align:center;color:#6B7280;">لا يوجد سجل لهذا الكيان</div>
    <?php else: ?>
        <div style="padding:20px;">
            <?php foreach ($history as $i => $entry): ?>
            <div style="display:flex;gap:15px;padding:15px 0;<?= $i < count($history) - 1 ? 'border-bottom:1px solid #F3F4F6;' : '' ?>">
                <div style="flex-shrink:0;width:10px;position:relative;">
                    <div style="width:10px;height:10px;border-radius:50%;background:<?= $entry['action'] === 'create' ? '#059669' : ($entry['action'] === 'delete' || $entry['action'] === 'archive' ? '#DC2626' : '#0284C7') ?>;margin-top:5px;"></div>
                    <?php if ($i < count($history) - 1): ?>
                        <div style="position:absolute;top:15px;right:4px;width:2px;height:calc(100% + 15px);background:#E5E7EB;"></div>
                    <?php endif; ?>
                </div>
                <div style="flex:1;">
                    <div style="display:flex;justify-content:space-between;margin-bottom:5px;">
                        <strong style="color:#1A1A2E;"><?= e($entry['action']) ?></strong>
                        <span style="color:#9CA3AF;font-size:12px;"><?= e($entry['created_at']) ?></span>
                    </div>
                    <div style="font-size:13px;color:#6B7280;margin-bottom:5px;">
                        بواسطة: <?= e($entry['employee_name'] ?? 'النظام') ?>
                        <?php if ($entry['ip_address']): ?>
                            <span style="direction:ltr;display:inline-block;">(<?= e($entry['ip_address']) ?>)</span>
                        <?php endif; ?>
                    </div>
                    <?php if ($entry['notes']): ?>
                        <div style="font-size:13px;color:#4B5563;margin-bottom:5px;"><?= e($entry['notes']) ?></div>
                    <?php endif; ?>
                    <?php if ($entry['changed_fields_json']): ?>
                        <?php $fields = json_decode($entry['changed_fields_json'], true); ?>
                        <div style="font-size:12px;color:#9CA3AF;">الحقول المُغيَّرة: <?= e(implode(', ', $fields ?? [])) ?></div>
                    <?php endif; ?>
                    <?php if ($entry['before_data_json'] && $entry['after_data_json']): ?>
                        <?php
                        $before = json_decode($entry['before_data_json'], true) ?? [];
                        $after = json_decode($entry['after_data_json'], true) ?? [];
                        $changedKeys = json_decode($entry['changed_fields_json'] ?? '[]', true) ?? [];
                        ?>
                        <?php if (!empty($changedKeys)): ?>
                        <details style="margin-top:8px;">
                            <summary style="cursor:pointer;color:#0D7377;font-size:13px;">عرض التفاصيل</summary>
                            <table style="width:100%;margin-top:8px;font-size:12px;border:1px solid #E5E7EB;border-radius:4px;">
                                <thead><tr style="background:#F9FAFB;"><th style="padding:6px 10px;text-align:right;">الحقل</th><th style="padding:6px 10px;text-align:right;">قبل</th><th style="padding:6px 10px;text-align:right;">بعد</th></tr></thead>
                                <tbody>
                                <?php foreach ($changedKeys as $ck): ?>
                                    <tr>
                                        <td style="padding:4px 10px;font-weight:600;"><?= e($ck) ?></td>
                                        <td style="padding:4px 10px;color:#DC2626;background:#FEF2F2;"><?= e((string) ($before[$ck] ?? '—')) ?></td>
                                        <td style="padding:4px 10px;color:#059669;background:#F0FDF4;"><?= e((string) ($after[$ck] ?? '—')) ?></td>
                                    </tr>
                                <?php endforeach; ?>
                                </tbody>
                            </table>
                        </details>
                        <?php endif; ?>
                    <?php endif; ?>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>
<?php $__template->endSection(); ?>