<?php
// Paksa role jadi admin biar dropdown nggak hilang-hilang
session(['user_role' => 'admin']);
?>

<?php $__env->startSection('konten'); ?>

<style>
    table th {
        background-color: #2a28a7 !important;
        /* Warna Hijau SJU */
        color: white;
        text-transform: uppercase;
        font-size: 13px;
    }

    table tr:hover {
        background-color: #f9f9f9;
        /* Efek highlight kalau kursor lewat */
    }
</style>

<style>
    /* Paksa nav pagination biar lurus ke samping dan nggak ada titiknya */
    .pagination-wrapper ul {
        display: flex !important;
        list-style: none !important;
        padding-left: 0 !important;
        justify-content: flex-end !important;
        /* Biar nempel ke kanan */
    }

    .pagination-wrapper li {
        margin: 0 2px !important;
    }

    .pagination-wrapper li a,
    .pagination-wrapper li span {
        padding: 8px 12px !important;
        border: 1px solid #dee2e6 !important;
        color: #007bff !important;
        text-decoration: none !important;
        background-color: #fff !important;
    }

    .pagination-wrapper li.active span {
        background-color: #007bff !important;
        color: white !important;
        border-color: #007bff !important;
    }

    /* Sembunyikan tulisan "Showing X to Y of Z results" yang bikin penuh layar */
    .pagination-wrapper .flex.items-center.justify-between {
        display: none !important;
    }
</style>

<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 5px;">
    <h3>Daftar Laporan Kerusakan SJU</h3>
    <?php if(request()->get('role') != 'admin'): ?>
    <a href="<?php echo e(url('service/tambah')); ?>" style="background: #2a28a7; color: white; padding: 10px; text-decoration: none; border-radius: 5px;">+ Buat Laporan</a>
    <?php endif; ?>
</div>

<div class="row mb-1" style="justify-content: flex-end !important; display: flex !important;">
    <div class="col-md-4" style="display: flex !important; justify-content: flex-end !important;">
        <form action="<?php echo e(url()->current()); ?>" method="GET" style="margin: 0 !important;">
            <?php if(request('role')): ?>
            <input type="hidden" name="role" value="<?php echo e(request('role')); ?>">
            <?php endif; ?>

            <div style="display: flex !important; width: 100% !important; align-items: stretch !important;">
                <!-- Input Cari: Kita paksa ambil sisa ruang (flex-grow) -->
                <input type="text" name="cari" class="form-control form-control-sm"
                    placeholder="Cari data..."
                    value="<?php echo e(request('cari')); ?>"
                    style="flex-grow: 1 !important; border-top-right-radius: 0 !important; border-bottom-right-radius: 0 !important; width: auto !important;">

                <!-- Tombol Cari -->
                <button class="btn btn-sm btn-primary" type="submit"
                    style="border-radius: 0 !important; white-space: nowrap !important; padding: 0 15px !important;">
                    Cari
                </button>

                <!-- Tombol Reset -->
                <a href="<?php echo e(url('service')); ?><?php echo e(request('role') ? '?role='.request('role') : ''); ?>"
                    class="btn btn-sm btn-danger d-flex align-items-center justify-content-center text-white"
                    style="border-top-left-radius: 0 !important; border-bottom-left-radius: 0 !important; text-decoration: none; white-space: nowrap !important; padding: 0 15px !important;">
                    Reset
                </a>
            </div>
        </form>
    </div>
</div>

<?php
$pendingCount = $laporan->where('status', 'Pending')->count();
?>

<?php if($pendingCount > 0): ?>
<div class="alert alert-info d-inline-block py-1 px-3 mb-2" style="font-size: 0.85rem;">
    <i class="fas fa-exclamation-circle me-1"></i>
    Ada <strong><?php echo e($pendingCount); ?></strong> laporan baru yang perlu diproses!
</div>
<?php endif; ?>

<style>
    th:last-child,
    td:last-child {
        white-space: nowrap;
        /* Biar tombolnya nggak turun ke bawah, tetep satu baris */
        width: 1%;
        /* Biar kolomnya pas selebar tombol saja */
    }
</style>

<?php if(session('success')): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
    <strong>Mantap!</strong> <?php echo e(session('success')); ?>

    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>

<table border="1" cellpadding="10" style="width: 100%; border-collapse: collapse; margin-top: 20px;">
    <thead>
        <tr style="background-color: #f2f2f2; text-align: center;">
            <th>Pelapor</th> 
            <?php if(request('role') == 'admin'): ?>
            <th>Unit</th>
            <?php endif; ?>
            <th>Lokasi</th> 
            <th>Perangkat</th> 
            <th>No. Inventaris</th> 
            <th>Keluhan</th> 
            <?php if(request('role') == 'admin'): ?>
            <th>Urgensi</th> 
            
            <th style="text-align: center;">FOTO</th>
            <?php endif; ?> 
            <th>Status</th> 

            <?php if(request()->query('role') == 'admin'): ?>
            <th>Aksi</th> 
            <?php endif; ?>
        </tr>
    </thead>
    <tbody class="text-secondary">
        <?php $__currentLoopData = $laporan; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <tr class="<?php echo e($item->status == 'Pending' ? 'table-warning' : ''); ?>">
            
            <td><?php echo e($item->pelapor); ?></td>

            <?php if(request('role') == 'admin'): ?>
            <td class="
            <?php if(str_contains(strtolower($item->lokasi), 'retail')): ?>
                bg-warning text-dark
            <?php elseif(str_contains(strtolower($item->lokasi), 'ruko')): ?>
                bg-primary text-white
            <?php else: ?>
                bg-success text-white
            <?php endif; ?>
">
                <?php if(str_contains(strtolower($item->lokasi), 'retail')): ?>
                Retail / Toko
                <?php elseif(str_contains(strtolower($item->lokasi), 'ruko')): ?>
                Kantor (Ruko)
                <?php else: ?>
                Kantor (SJU)
                <?php endif; ?>
            </td>
            <?php endif; ?>

            <td><?php echo e($item->divisi); ?></td>
            <td><?php echo e($item->perangkat); ?></td>
            <td><?php echo e($item->no_inventaris ?? '-'); ?></td>
            <td><?php echo e($item->keluhan); ?></td>
            <?php
            $urgensi = strtolower($item->urgensi);
            $bgcolor = '#28a745'; // Default Abu-abu
            $textColor = 'white';

            if (str_contains($urgensi, 'darurat')) {
            $bgcolor = '#ffee00'; // Hitam (Atau Merah Marun #8b0000) biar beda banget
            $textColor = 'black'; // Tulisan kuning biar kontras
            } elseif (str_contains($urgensi, 'penting') || str_contains($urgensi, 'high')) {
            $bgcolor = '#dc3545'; // Merah standar
            } elseif (str_contains($urgensi, 'menengah') || str_contains($urgensi, 'medium')) {
            $bgcolor = '#ffc107'; // Kuning Bootstrap
            $textColor = 'black';
            } elseif (str_contains($urgensi, 'rendah') || str_contains($urgensi, 'low')) {
            $bgcolor = '#28a745'; // Hijau
            }
            ?>
            <?php if(request('role') == 'admin'): ?>
            <td style="background-color: <?php echo e($bgcolor); ?>; color: <?php echo e($textColor); ?>;">
                <?php echo e($item->urgensi); ?>

            </td>
            <?php endif; ?>

            <?php if(request('role') == 'admin'): ?>
            <td style="text-align: center;">
                <?php if($item->foto): ?>
                <button type="button" class="btn btn-sm btn-info text-white" data-bs-toggle="modal" data-bs-target="#fotoModal<?php echo e($item->id); ?>">
                    Lihat Foto
                </button>
                <?php else: ?>
                -
                <?php endif; ?>
            </td>
            <?php endif; ?>
            <td><?php echo e($item->status); ?></td>

            <?php if(request()->query('role') == 'admin'): ?>
            <td style="vertical-align: middle !important; white-space: nowrap !important; text-align: center;">
                <div style="display: flex !important; flex-direction: row !important; align-items: center !important; justify-content: center !important; gap: 5px !important;">

                    

                    <form action="<?php echo e(url('/service/'.$item->id)); ?>" method="POST" style="margin: 0 !important; padding: 0 !important;">
                        <?php echo csrf_field(); ?> <?php echo method_field('DELETE'); ?>
                        <button type="submit" onclick="return confirm('Yakin Hapus?')"
                            style="background-color: #dc3545 !important; color: white !important; border: none !important; padding: 5px 10px !important; font-size: 11px !important; border-radius: 4px !important; cursor: pointer !important;">
                            Hapus
                        </button>
                    </form>

                    <?php if(session('user_role') == 'admin'): ?>
                    <a href="<?php echo e(url('/service/'.$item->id.'/analisis')); ?>"
                        style="background-color: #0d6efd !important; color: white !important; text-decoration: none !important; padding: 5px 10px !important; font-size: 11px !important; border-radius: 4px !important; display: inline-block !important;">
                        Analisis
                    </a>

                    <!-- KODINGAN BARU (LANGSUNG COPAS DI TEMPAT TOMBOL LAMA) -->
                    <!-- TOMBOL CETAK BARU: UKURAN SERAGAM SAMA RATA -->
                    <!-- KODE MURNI POLOSAN - PASTIKAN KODE SEBELUMNYA DIHAPUS TOTAL YA MAS -->
                    <!-- UKURAN PENYESUAIAN: NAIK DIKIT BIAR PAS -->
                    <a href="javascript:void(0)" onclick="cetakTanpaNewTab('<?php echo e(url('/service/' . $item->id . '/print')); ?>')" class="btn btn-success" style="background-color: #198754 !important; color: white !important; text-decoration: none !important; padding: 4px 10px !important; font-size: 11px !important; border-radius: 4px !important; display: inline-block !important;">
                        <i class="fas fa-print"></i> Cetak
                    </a>

                    <script>
                        function cetakTanpaNewTab(urlPrint) {
                            // 1. Cari dulu apakah ada iframe gaib sisa sebelumnya, kalau ada hapus dulu biar gak numpuk
                            let iframeLama = document.getElementById('iframeCetakGaib');
                            if (iframeLama) {
                                iframeLama.remove();
                            }
                        
                            // 2. Buat elemen iframe gaib baru
                            let iframeGaib = document.createElement('iframe');
                            iframeGaib.id = 'iframeCetakGaib';
                        
                            // 3. Sembunyikan dari layar
                            iframeGaib.style.position = 'fixed';
                            iframeGaib.style.width = '0';
                            iframeGaib.style.height = '0';
                            iframeGaib.style.border = 'none';
                        
                            // 4. Masukkan URL halaman cetak
                            iframeGaib.src = urlPrint;
                        
                            // 5. [PERBAIKAN] Tunggu sampai konten cetak selesai di-load, lalu paksa print!
                            iframeGaib.onload = function() {
                                iframeGaib.contentWindow.focus();
                                iframeGaib.contentWindow.print();
                            };
                        
                            // 6. Masukkan ke dalam body halaman
                            document.body.appendChild(iframeGaib);
                        }
                    </script>
                    <?php endif; ?>
                </div>
            </td>
            <?php endif; ?>
        </tr>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
    </tbody>
</table>

<?php if(request()->get('role') == 'admin'): ?>
<div class="alert alert-success mt-2 d-flex justify-content-between align-items-center" role="alert" style="padding: 10px 20px;">
    <div>
        <i class="fa fa-users"></i>
        <strong><?php echo e(count($userOnline)); ?> User Sedang Online</strong>
    </div>

    <div>
        <a href="<?php echo e(url('/service/export')); ?>" class="btn btn-success btn-sm">
            <i class="fa fa-file-excel-o"></i> Download Rekap Excel
        </a>
    </div>
</div>
<?php endif; ?>
</div>

<div class="pagination-wrapper mt-3">
    <?php echo e($laporan->links('pagination::bootstrap-4')); ?>

</div>

<?php if(request()->get('role') == 'admin'): ?>
<div class="card mt-3">
    <div class="card-header bg-primary text-white" style="padding: 8px 12px; font-size: 14px;">
        <i class="fas fa-users"></i> Detil User Online (5 Menit Terakhir)
    </div>
    <ul class="list-group list-group-flush" style="font-size: 12px;">
        <?php $__currentLoopData = $userOnline; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <?php
        // Trik sederhana menerjemahkan OS
        $agent = $user->user_agent;
        $os = "Unknown OS";
        if (preg_match('/win/i', $agent)) { $os = 'Windows'; }
        elseif (preg_match('/android/i', $agent)) { $os = 'Android'; }
        elseif (preg_match('/iphone|ipad/i', $agent)) { $os = 'iOS'; }
        elseif (preg_match('/mac/i', $agent)) { $os = 'MacOS'; }
        elseif (preg_match('/linux/i', $agent)) { $os = 'Linux'; }

        // Trik sederhana menerjemahkan Browser
        $browser = "Unknown Browser";
        if (preg_match('/chrome/i', $agent) && !preg_match('/edge/i', $agent)) { $browser = 'Chrome'; }
        elseif (preg_match('/firefox/i', $agent)) { $browser = 'Firefox'; }
        elseif (preg_match('/safari/i', $agent) && !preg_match('/chrome/i', $agent)) { $browser = 'Safari'; }
        elseif (preg_match('/edge/i', $agent)) { $browser = 'Edge'; }
        ?>

        <li class="list-group-item d-flex justify-content-between align-items-center" style="padding: 8px 12px;">
            <div>
                <!-- Kalau belum login, tampilkan 'Tamu / Public User' -->
                <strong><?php echo e($user->user_name ?? 'Tamu (Public)'); ?></strong>
                <small class="text-muted d-block">IP: <?php echo e($user->ip_address); ?></small>
            </div>
            <span class="badge bg-secondary text-white" style="font-size: 11px; padding: 4px 8px;">
                <i class="fas fa-laptop"></i> <?php echo e($os); ?> - <?php echo e($browser); ?>

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

<style>
    /* Efek kedip-kedip biar kayak lampu indikator online */
    .blink {
        animation: blinker 1.5s linear infinite;
    }

    @keyframes blinker {
        50% {
            opacity: 0;
        }
    }
</style>



<?php if(request()->query('role') == 'admin'): ?>
<script>
    // Refresh otomatis cuma buat Admin tiap 30 detik
    setTimeout(function() {
        location.reload();
    }, 600000);
</script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

<?php $__currentLoopData = $laporan; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<div class="modal fade" id="fotoModal<?php echo e($item->id); ?>" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Foto Kendala - <?php echo e($item->pelapor); ?></h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body text-center">
                <?php if($item->foto): ?>
                <img src="<?php echo e(asset($item->foto)); ?>" class="img-fluid rounded">
                <?php else: ?>
                <p>Tidak ada foto</p>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

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

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/sjumyid/public_html/sju-service/resources/views/service/index.blade.php ENDPATH**/ ?>