

<?php use Illuminate\Support\Str; ?>

<?php $__env->startSection('content'); ?>
<div class="container-xxl flex-grow-1 container-p-y">
    <div class="card">
        <!-- HEADER -->
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="mb-0">Audit Logs</h5>

            <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if(auth()->guard()->check()): ?>
                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if(Auth::user()->is_admin): ?>
                    <form action="<?php echo e(route('audit-logs.clear-old')); ?>" method="POST">
                        <?php echo csrf_field(); ?>
                        <button class="btn btn-warning"
                            onclick="return confirm('Clear logs older than 90 days?')">
                            <i class="fas fa-broom me-1"></i> Clear Old Logs
                        </button>
                    </form>
                <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
            <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
        </div>

        <div class="card-body">
            
            <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if(session('success')): ?>
                <div class="alert alert-success"><?php echo e(session('success')); ?></div>
            <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>

            <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if(session('error')): ?>
                <div class="alert alert-danger"><?php echo e(session('error')); ?></div>
            <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>

             <!-- STATS -->
            <div class="row mt-3 mb-4">
                <div class="col-md-3">
                    <div class="card bg-primary text-white text-center p-3">
                        <h3><?php echo e(\App\Models\AuditLog::count()); ?></h3>
                        <small>Total Logs</small>
                    </div>
                </div>

                <div class="col-md-3">
                    <div class="card bg-success text-white text-center p-3">
                        <h3><?php echo e(\App\Models\AuditLog::where('action','created')->count()); ?></h3>
                        <small>Total Created</small>
                    </div>
                </div>

                <div class="col-md-3">
                    <div class="card bg-warning text-white text-center p-3">
                        <h3><?php echo e(\App\Models\AuditLog::where('action','updated')->count()); ?></h3>
                        <small>Total Updated</small>
                    </div>
                </div>

                <div class="col-md-3">
                    <div class="card bg-danger text-white text-center p-3">
                        <h3><?php echo e(\App\Models\AuditLog::where('action','deleted')->count()); ?></h3>
                        <small>Total Deleted</small>
                    </div>
                </div>
            </div>

            <!-- FILTER PANEL -->
            <div class="card mb-4">
                <div class="card-header bg-light">
                    <strong>Filter Logs</strong>
                </div>

                

                <div class="card-body">
                    <form method="GET" action="<?php echo e(route('audit-logs.index')); ?>" class="row g-3" id="filterForm">
                        <!-- Table -->
                        <div class="col-md-3">
                            <label class="form-label">Table</label>
                            <select class="form-control" name="table_name">
                                <option value="">All Tables</option>
                                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__currentLoopData = $tableNames; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $table): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($table); ?>" <?php echo e(request('table_name') == $table ? 'selected' : ''); ?>>
                                        <?php echo e($table); ?>

                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                            </select>
                        </div>

                        <!-- Action -->
                       <div class="col-md-3">
                            <label class="form-label">Action</label>
                            <select class="form-control" name="action">
                                <option value="">All Actions</option>
                                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__currentLoopData = $actions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $action): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($action); ?>" <?php echo e(request('action') == $action ? 'selected' : ''); ?>>
                                        <?php echo e(ucfirst($action)); ?>

                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                            </select>
                        </div>

                        <!-- User -->
                        <div class="col-md-3">
                            <label class="form-label">User</label>
                            <select class="form-control" name="user_id">
                                <option value="">All Users</option>
                                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__currentLoopData = $users; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($user->id); ?>" <?php echo e(request('user_id') == $user->id ? 'selected' : ''); ?>>
                                        <?php echo e($user->name); ?>

                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                            </select>
                        </div>

                        <!-- Date From -->
                        <div class="col-md-3">
                            <label class="form-label">Date From</label>
                            <input type="date" class="form-control" name="date_from" value="<?php echo e(request('date_from')); ?>">
                        </div>

                        <!-- Date To -->
                        <div class="col-md-3">
                            <label class="form-label">Date To</label>
                            <input type="date" class="form-control" name="date_to" value="<?php echo e(request('date_to')); ?>">
                        </div>

                        <!-- Buttons -->
                        <div class="col-md-12">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-filter me-1"></i> Filter
                            </button>

                            <a href="<?php echo e(route('audit-logs.index')); ?>" class="btn btn-secondary">
                                Clear
                            </a>
                        </div>
                    </form>
                </div>
            </div>

            <!-- LOG TABLE -->
            <div class="table-responsive">
                <table id="bookings-table" class="table table-bordered align-middle text-center display">
                    <thead class="table-light">
                        <tr>
                            <th>ID</th>
                            <th>Table</th>
                            <th>Record</th>
                            <th>Action</th>
                            <th>User</th>
                            <th>Description</th>
                            <th>Date</th>
                            <th>Actions</th>
                        </tr>
                    </thead>

                    <tbody>
                    <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__empty_1 = true; $__currentLoopData = $logs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $log): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <tr>
                            <td><?php echo e($log->id); ?></td>

                            <td>
                                <code><?php echo e($log->table_name); ?></code>
                            </td>

                            <td>
                                <span class="badge bg-secondary">#<?php echo e($log->record_id); ?></span>
                            </td>

                            <td>
                                <?php
                                    $colors = [
                                        'created' => 'success',
                                        'updated' => 'warning',
                                        'deleted' => 'danger'
                                    ];
                                ?>
                                <span class="badge bg-<?php echo e($colors[$log->action] ?? 'info'); ?>">
                                    <?php echo e(ucfirst($log->action)); ?>

                                </span>
                            </td>

                            <td>
                                <?php echo e($log->user->name ?? 'System'); ?>

                            </td>

                            <td>
                                <?php echo e(Str::limit($log->description ?? 'N/A', 60)); ?>

                            </td>

                            <td>
                                <?php echo e($log->created_at->format('d M Y')); ?> <br>
                                <small class="text-muted"><?php echo e($log->created_at->format('h:i A')); ?></small>
                            </td>

                            <td>
                                <div class="btn-group" role="group"> 
                                      <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('view audit logs')): ?>
                                            <a href="<?php echo e(route('audit-logs.show', $log)); ?>" class="btn btn-sm btn-info">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                        <?php endif; ?>

                                        <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('delete audit logs')): ?>
                                            <form action="<?php echo e(route('audit-logs.destroy', $log)); ?>" method="POST" class="d-inline"
                                                onsubmit="return confirm('Delete this log?')">
                                                <?php echo csrf_field(); ?>
                                                <?php echo method_field('DELETE'); ?>
                                                <button type="submit" class="btn btn-sm btn-danger">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </form>
                                        <?php endif; ?>            
                                   </div>
                                </td>


                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                        <tr>
                            <td colspan="8">No audit logs found.</td>
                        </tr>
                    <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                    </tbody>
                </table>
            </div>

           
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>
<?php $__env->startPush('script'); ?>
<script>      
   $(document).ready(function () {
    $('#bookings-table').DataTable({
        responsive: false,
        scrollX: true,
        pageLength: 10,
        lengthMenu: [5, 10, 25, 50, 100],
        order: [[0, 'asc']],
        columnDefs: [
            { orderable: false, targets: -1 } // disable sorting for last column
        ]
    });
});
</script>
<?php $__env->stopPush(); ?>
<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH F:\xampp\htdocs\G_desizn\mfmsqatar\resources\views/audit-logs/index.blade.php ENDPATH**/ ?>