<?php
$__template->layout('Layout.main');
use App\Modules\Audit\Services\AuditService;

$action = $entry['action'] ?? '';
$color = AuditService::getActionColor($action);
$icon = AuditService::getActionIcon($action);
$actionLabel = AuditService::getActionLabel($action);
$entityLabel = AuditService::getEntityTypeLabel($entry['entity_type'] ?? '');

$changedFields = null;
if (!empty($entry['changed_fields_json'])) {
    $changedFields = json_decode($entry['changed_fields_json'], true);
}
$afterData = null;
if (!empty($entry['after_data_json'])) {
    $afterData = json_decode($entry['after_data_json'], true);
}
$beforeData = null;
if (!empty($entry['before_data_json'])) {
    $beforeData = json_decode($entry['before_data_json'], true);
}
?>
<?php $__template->section('title'); ?>تفاصيل السجل #<?= (int) $entry['id'] ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<div style="display:flex;gap:8px;">
    <?php if ($prevId): ?>
        <a href="/audit/<?= $prevId ?>" class="btn btn-sm btn-outline">→ السابق</a>
    <?php endif; ?>
    <?php if ($nextId): ?>
        <a href="/audit/<?= $nextId ?>" class="btn btn-sm btn-outline">التالي ←</a>
    <?php endif; ?>
    <?php if ($entry['entity_type'] && $entry['entity_id']): ?>
        <a href="/audit/entity/<?= urlencode($entry['entity_type']) ?>/<?= (int) $entry['entity_id'] ?>" class="btn btn-sm btn-outline">سجل الكيان</a>
    <?php endif; ?>
    <a href="/audit" class="btn btn-sm btn-outline">العودة للسجل</a>
</div>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<div class="card" style="border-right:5px solid <?= $color ?>;margin-bottom:20px;">
    <div style="padding:20px;">
        <div style="display:flex;align-items:center;gap:12px;margin-bottom:16px;">
            <span style="font-size:28px;"><?= $icon ?></span>
            <div>
                <div style="font-size:20px;font-weight:700;color:<?= $color ?>;"><?= e($actionLabel) ?></div>
                <div style="font-size:14px;color:#6B7280;"><?= e($entityLabel) ?> <?php if ($entry['entity_label']): ?>— <?= e($entry['entity_label']) ?><?php endif; ?></div>
            </div>
        </div>

        <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(220px,1fr));gap:12px;font-size:13px;">
            <div style="background:#F9FAFB;border-radius:8px;padding:10px 14px;">
                <div style="color:#9CA3AF;font-size:11px;margin-bottom:2px;">الموظف</div>
                <div style="color:#1A1A2E;font-weight:600;"><?= e($entry['employee_name'] ?? 'النظام') ?></div>
            </div>
            <div style="background:#F9FAFB;border-radius:8px;padding:10px 14px;">
                <div style="color:#9CA3AF;font-size:11px;margin-bottom:2px;">التاريخ والوقت</div>
                <div style="color:#1A1A2E;font-weight:600;"><?= e($entry['created_at']) ?></div>
            </div>
            <?php if ($entry['ip_address']): ?>
            <div style="background:#F9FAFB;border-radius:8px;padding:10px 14px;">
                <div style="color:#9CA3AF;font-size:11px;margin-bottom:2px;">عنوان IP</div>
                <div style="color:#1A1A2E;font-weight:600;direction:ltr;text-align:right;"><?= e($entry['ip_address']) ?></div>
            </div>
            <?php endif; ?>
            <?php if ($entry['route']): ?>
            <div style="background:#F9FAFB;border-radius:8px;padding:10px 14px;">
                <div style="color:#9CA3AF;font-size:11px;margin-bottom:2px;">المسار</div>
                <div style="color:#1A1A2E;font-weight:500;direction:ltr;text-align:right;font-size:12px;word-break:break-all;"><?= e($entry['route']) ?></div>
            </div>
            <?php endif; ?>
            <?php if ($entry['entity_id']): ?>
            <div style="background:#F9FAFB;border-radius:8px;padding:10px 14px;">
                <div style="color:#9CA3AF;font-size:11px;margin-bottom:2px;">رقم الكيان</div>
                <div style="color:#1A1A2E;font-weight:600;">#<?= (int) $entry['entity_id'] ?></div>
            </div>
            <?php endif; ?>
        </div>

        <?php if ($entry['notes']): ?>
        <div style="margin-top:14px;padding:10px 14px;background:#FFF7ED;border:1px solid #FDE68A;border-radius:8px;font-size:13px;color:#92400E;">
            <?= e($entry['notes']) ?>
        </div>
        <?php endif; ?>
    </div>
</div>

<?php if ($action === 'create' && is_array($afterData) && !empty($afterData)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;color:#059669;font-size:15px;">البيانات المنشأة</h3>
    </div>
    <div style="padding:16px 20px;">
        <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(300px,1fr));gap:8px;">
        <?php foreach ($afterData as $fk => $fv):
            if ($fv === null || $fv === '') continue;
        ?>
            <div style="display:flex;align-items:baseline;gap:8px;padding:8px 12px;background:#F0FDF4;border:1px solid #BBF7D0;border-radius:6px;">
                <span style="color:#374151;font-weight:600;font-size:12px;min-width:120px;flex-shrink:0;"><?= e(AuditService::getFieldLabel($fk)) ?></span>
                <span style="color:#065F46;font-size:13px;word-break:break-word;"><?= e(AuditService::formatFieldValue($fk, $fv)) ?></span>
            </div>
        <?php endforeach; ?>
        </div>
    </div>
</div>

<?php elseif ($action === 'update' && is_array($changedFields) && !empty($changedFields)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;color:#0284C7;font-size:15px;">التغييرات (<?= count($changedFields) ?> حقل)</h3>
    </div>
    <div style="padding:0;">
        <table style="width:100%;font-size:13px;border-collapse:collapse;">
            <thead>
                <tr style="background:#F0F9FF;">
                    <th style="padding:10px 16px;text-align:right;border-bottom:2px solid #BAE6FD;width:25%;">الحقل</th>
                    <th style="padding:10px 16px;text-align:right;border-bottom:2px solid #BAE6FD;width:37%;">القيمة السابقة</th>
                    <th style="padding:10px 16px;text-align:right;border-bottom:2px solid #BAE6FD;width:37%;">القيمة الجديدة</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($changedFields as $fname => $fchange): ?>
                <?php if (!is_array($fchange) || !isset($fchange['from'])) continue; ?>
                <tr>
                    <td style="padding:10px 16px;font-weight:600;color:#374151;border-bottom:1px solid #F3F4F6;"><?= e(AuditService::getFieldLabel($fname)) ?></td>
                    <td style="padding:10px 16px;color:#DC2626;background:#FEF2F2;border-bottom:1px solid #F3F4F6;">
                        <?= e(AuditService::formatFieldValue($fname, $fchange['from'])) ?>
                    </td>
                    <td style="padding:10px 16px;color:#059669;background:#F0FDF4;border-bottom:1px solid #F3F4F6;">
                        <?= e(AuditService::formatFieldValue($fname, $fchange['to'])) ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<?php if (is_array($beforeData) && !empty($beforeData)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;color:#6B7280;font-size:15px;">اللقطة الكاملة (قبل التعديل)</h3>
    </div>
    <div style="padding:16px 20px;">
        <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(300px,1fr));gap:6px;">
        <?php foreach ($beforeData as $fk => $fv):
            if ($fv === null || $fv === '') continue;
            $isChanged = isset($changedFields[$fk]);
        ?>
            <div style="display:flex;align-items:baseline;gap:8px;padding:6px 10px;background:<?= $isChanged ? '#FEF2F2' : '#F9FAFB' ?>;border:1px solid <?= $isChanged ? '#FECACA' : '#E5E7EB' ?>;border-radius:6px;">
                <span style="color:#374151;font-weight:600;font-size:11px;min-width:110px;flex-shrink:0;"><?= e(AuditService::getFieldLabel($fk)) ?></span>
                <span style="color:<?= $isChanged ? '#991B1B' : '#4B5563' ?>;font-size:12px;word-break:break-word;"><?= e(AuditService::formatFieldValue($fk, $fv)) ?></span>
            </div>
        <?php endforeach; ?>
        </div>
    </div>
</div>
<?php endif; ?>

<?php elseif ($action === 'delete' && is_array($beforeData) && !empty($beforeData)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;color:#DC2626;font-size:15px;">البيانات المحذوفة</h3>
    </div>
    <div style="padding:16px 20px;">
        <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(300px,1fr));gap:8px;">
        <?php foreach ($beforeData as $fk => $fv):
            if ($fv === null || $fv === '') continue;
        ?>
            <div style="display:flex;align-items:baseline;gap:8px;padding:8px 12px;background:#FEF2F2;border:1px solid #FECACA;border-radius:6px;">
                <span style="color:#374151;font-weight:600;font-size:12px;min-width:120px;flex-shrink:0;"><?= e(AuditService::getFieldLabel($fk)) ?></span>
                <span style="color:#991B1B;font-size:13px;word-break:break-word;"><?= e(AuditService::formatFieldValue($fk, $fv)) ?></span>
            </div>
        <?php endforeach; ?>
        </div>
    </div>
</div>

<?php elseif ($action === 'archive' && is_array($beforeData) && !empty($beforeData)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;color:#D97706;font-size:15px;">البيانات المؤرشفة</h3>
    </div>
    <div style="padding:16px 20px;">
        <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(300px,1fr));gap:8px;">
        <?php foreach ($beforeData as $fk => $fv):
            if ($fv === null || $fv === '') continue;
        ?>
            <div style="display:flex;align-items:baseline;gap:8px;padding:8px 12px;background:#FFFBEB;border:1px solid #FDE68A;border-radius:6px;">
                <span style="color:#374151;font-weight:600;font-size:12px;min-width:120px;flex-shrink:0;"><?= e(AuditService::getFieldLabel($fk)) ?></span>
                <span style="color:#92400E;font-size:13px;word-break:break-word;"><?= e(AuditService::formatFieldValue($fk, $fv)) ?></span>
            </div>
        <?php endforeach; ?>
        </div>
    </div>
</div>
<?php endif; ?>

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