<?php $__env->startSection('page-title'); ?>
    <?php echo e(__('Manage Audit')); ?>

<?php $__env->stopSection(); ?>

<?php $__env->startSection('breadcrumb'); ?>
    <li class="breadcrumb-item"><a href="<?php echo e(route('dashboard')); ?>"><?php echo e(__('Home')); ?></a></li>
    <li class="breadcrumb-item"><?php echo e(__('Audit')); ?></li>
<?php $__env->stopSection(); ?>


<?php $__env->startSection('content'); ?>
    <div class="row">
        <div class="col-xl-12">
            <div class="card">
                <div class="card-header card-body table-border-style">
                    
                    <div class="table-responsive">
                        <table class="table" id="pc-dt-simple">
                            <thead>
                                <tr>
                                    <th>S/N</th>
                                    <th>Date & Time</th>
                                    <th>Action</th>
                                    <th>Module Name</th>
                                    <th>Description</th>
                                    <th>Performed By</th>
                                    <th>Role</th>
                                    <!-- <th>Action</th> -->
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__currentLoopData = $audits; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $audit): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <tr>
                                        <td><?php echo e($index + 1); ?></td>
                                        <td><?php echo e(\Carbon\Carbon::parse($audit->created_at)->format('Y-m-d, H:i')); ?></td>
                                        <td><?php echo e(ucfirst($audit->event)); ?></td>
                                        <td><?php echo e($audit->log_name); ?></td>
                                        <td><?php echo e($audit->description); ?></td>
                                        <td>
                                            <?php if($audit->causer): ?>
                                                <?php echo e($audit->causer->email); ?>

                                            <?php else: ?>
                                                <em class="text-muted">System</em>
                                            <?php endif; ?>
                                        </td>
                                        <td><?php echo e($audit->causer->role ?? 'N/A'); ?></td>
                                        <!-- <td>
                                                                                                                                                                    <i class="fa-solid fa-ellipsis cursor-pointer" onclick="openAuditModal(<?php echo e($audit->id); ?>)"></i>

                                                                                                                                                                    <div class="modal fade" id="auditModal<?php echo e($audit->id); ?>" tabindex="-1" aria-labelledby="auditModalLabel<?php echo e($audit->id); ?>"
                                                                                                                                                                        aria-hidden="true">
                                                                                                                                                                        <div class="modal-dialog modal-dialog-scrollable">
                                                                                                                                                                            <div class="m-4 modal-content">
                                                                                                                                                                                <div class="modal-header">
                                                                                                                                                                                    <h5 class="modal-title text-primary" id="auditModalLabel<?php echo e($audit->id); ?>">Audit Attributes</h5>
                                                                                                                                                                                    <button type="button" class="btn btn-close" onclick="closeAuditModal(<?php echo e($audit->id); ?>)" aria-label="Close">x</button>
                                                                                                                                                                                    </div>
                                                                                                                                                                                    <div class="modal-body">
                                                                                                                                                                                        <?php
                                                                                                                                                                                        $props = $audit->properties->toArray();
                                                                                                                                                                                        ?>

                                                                                                                                                                                    <?php if(!empty($props['attributes'])): ?>
                                                                                                                                                                                    <ul class="px-2 list-group mb-3">
                                                                                                                                                                                        <?php $__currentLoopData = $props['attributes']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                                                                                                                                                        <li class="list-group-item d-flex justify-content-between align-items-center">
                                                                                                                                                                                            <strong><?php echo e($key); ?></strong>
                                                                                                                                                                                                    
                                                                        <span>
                                                                            <?php echo e(is_array($value) 
                                                                                ? implode(', ', array_map('strval', \Illuminate\Support\Arr::flatten($value))) 
                                                                                : $value ?? ''); ?>

                                                                        </span>



                                                                                                                                                                                                </li>
                                                                                                                                                                                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                                                                                                                                                                </ul>
                                                                                                                                                                                                <?php else: ?>
                                                                                                                                                                                                <p class="text-muted fst-italic">No attributes found.</p>
                                                                                                                                                                                                <?php endif; ?>

                                                                                                                                                                                    <?php if($audit->event === 'updated' && !empty($props['old'])): ?>
                                                                                                                                                                                    <h6 class="px-2 mt-3 text-primary">Changes</h6>
                                                                                                                                                                                    <ul class="px-2 pb-3 list-group">
                                                                                                                                                                                        <?php $__currentLoopData = $props['attributes']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $newVal): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                                                                                                                                                        <?php if(isset($props['old'][$key]) && $props['old'][$key] !== $newVal): ?>
                                                                                                                                                                                        <li class="list-group-item d-flex justify-content-between align-items-center">
                                                                                                                                                                                            <strong><?php echo e($key); ?></strong>
                                                                                                                                                                                            <span>
                                                                                                                                                                                                <del><?php echo e($props['old'][$key]); ?></del>
                                                                                                                                                                                                &rarr;
                                                                                                                                                                                                <?php echo e($newVal); ?>

                                                                                                                                                                                            </span>
                                                                                                                                                                                        </li>
                                                                                                                                                                                        <?php endif; ?>
                                                                                                                                                                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                                                                                                                                                    </ul>
                                                                                                                                                                                    <?php endif; ?>
                                                                                                                                                                                    </div>
                                                                                                                                                                                    </div>
                                                                                                                                                                                    </div>
                                                                                                                                                                                    </div>
                                                                                                                                                                                    </td> -->
                                    </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </tbody>
                        </table>

                        <?php if(count($audits) === 0): ?>
                            <div class="text-center py-4">
                                <p>No audit logs found.</p>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>

<script>

    // Store modal instances
    const auditModals = {};

    // Initialize all modals on page load
    document.addEventListener('DOMContentLoaded', () => {
        <?php $__currentLoopData = $audits; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $audit): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            auditModals[<?php echo e($audit['id']); ?>] = new bootstrap.Modal(document.getElementById('auditModal<?php echo e($audit['id']); ?>'), {
                keyboard: false
            });
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
    });

    // Function to open modal manually
    function openAuditModal(id) {
        auditModals[id].show();
    }

    // Function to close modal manually
    function closeAuditModal(id) {
        auditModals[id].hide();
    }

</script>
<?php echo $__env->make('layouts.admin', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /var/www/payraty/hris/resources/views/audit_trail/index.blade.php ENDPATH**/ ?>