<div>
    <!-- Header & Breadcrumb -->
    <div class="mb-6 flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
        <div>
            <!-- Breadcrumb -->
            <nav class="flex text-xs text-slate-400 mb-1" aria-label="Breadcrumb">
                <ol class="inline-flex items-center space-x-1 md:space-x-2">
                    <li class="inline-flex items-center">
                        <a href="{{ route('dashboard') }}" class="text-slate-400 hover:text-white">Home</a>
                    </li>
                    <li>
                        <div class="flex items-center">
                            <svg class="w-4 h-4 text-slate-600" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd"></path></svg>
                            <span class="ml-1 text-slate-300 font-medium">Monitoring Aplikasi</span>
                        </div>
                    </li>
                </ol>
            </nav>
            <h1 class="text-2xl font-bold text-white tracking-tight">System Health & Machine Socket Monitoring</h1>
        </div>

        <div>
            <button wire:click="refreshHealthChecks" wire:loading.attr="disabled"
                class="px-4 py-2.5 rounded-xl bg-sky-600 hover:bg-sky-500 text-white font-semibold text-xs shadow-lg shadow-sky-600/20 transition-all flex items-center gap-2">
                <svg wire:loading.remove wire:target="refreshHealthChecks" class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"></path></svg>
                <svg wire:loading wire:target="refreshHealthChecks" class="animate-spin h-4 w-4 text-white" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg>
                <span>Perbarui Status Sistem</span>
            </button>
        </div>
    </div>

    <!-- Health Overview Cards -->
    <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
        <!-- Database Health Card -->
        <div class="bg-slate-900 border border-slate-800 rounded-2xl p-5 shadow-sm">
            <div class="flex items-center justify-between mb-3">
                <span class="text-xs font-bold text-slate-400 uppercase tracking-wider">Status Database MySQL</span>
                <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase tracking-wider {{ ($dbHealth['status'] ?? '') == 'healthy' ? 'bg-emerald-500/10 text-emerald-400 border border-emerald-500/20' : 'bg-rose-500/10 text-rose-400 border border-rose-500/20' }}">
                    {{ $dbHealth['label'] ?? 'Unknown' }}
                </span>
            </div>
            <div class="flex items-baseline gap-2">
                <span class="text-2xl font-bold text-white font-mono">{{ $dbHealth['latency_ms'] ?? 0 }}</span>
                <span class="text-xs text-slate-400">ms (waktu respon)</span>
            </div>
            <p class="text-[11px] text-slate-500 font-mono mt-2">Driver: {{ $dbHealth['driver'] ?? 'mysql' }} | DB: {{ $dbHealth['database'] ?? '-' }}</p>
        </div>

        <!-- Cronjob Health Card -->
        <div class="bg-slate-900 border border-slate-800 rounded-2xl p-5 shadow-sm">
            <div class="flex items-center justify-between mb-3">
                <span class="text-xs font-bold text-slate-400 uppercase tracking-wider">Status Cron Job Auto-Sync</span>
                <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase tracking-wider {{ ($cronHealth['status'] ?? '') == 'healthy' ? 'bg-emerald-500/10 text-emerald-400 border border-emerald-500/20' : 'bg-amber-500/10 text-amber-400 border border-amber-500/20' }}">
                    {{ $cronHealth['label'] ?? 'Unknown' }}
                </span>
            </div>
            <div class="flex items-baseline gap-2">
                <span class="text-xs text-slate-300 font-mono">Terakhir Jalan: {{ ($cronHealth['last_run'] ?? null) ? $cronHealth['last_run']->diffForHumans() : 'Belum pernah jalan' }}</span>
            </div>
            <p class="text-[11px] text-slate-500 font-mono mt-2">Log ditarik: {{ $cronHealth['records_fetched'] ?? 0 }} data absensi</p>
        </div>

        <!-- Web Hosting Environment Card -->
        <div class="bg-slate-900 border border-slate-800 rounded-2xl p-5 shadow-sm">
            <div class="flex items-center justify-between mb-3">
                <span class="text-xs font-bold text-slate-400 uppercase tracking-wider">Lingkungan Web Hosting</span>
                <span class="px-2.5 py-1 rounded-full text-[10px] font-bold bg-sky-500/10 text-sky-400 border border-sky-500/20">Kompatibel</span>
            </div>
            <div class="space-y-1 text-xs font-mono text-slate-300">
                <p>PHP Version: <span class="text-white font-bold">{{ PHP_VERSION }}</span></p>
                <p>Laravel Framework: <span class="text-white font-bold">{{ app()->version() }}</span></p>
            </div>
        </div>
    </div>

    <!-- Machine Status Matrix -->
    <div class="bg-slate-900 border border-slate-800 rounded-2xl overflow-hidden shadow-sm mb-6">
        <div class="p-4 border-b border-slate-800">
            <h3 class="text-sm font-bold text-white">Status Integrasi Database Mesin (Terakhir Sinkronisasi)</h3>
            <p class="text-[10px] text-slate-400 mt-0.5">Status koneksi didasarkan pada waktu terakhir mesin mengirimkan log absensi ke dalam Database Fingerspot Middleware.</p>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full text-left text-xs">
                <thead class="bg-slate-950/80 text-slate-400 uppercase tracking-wider font-semibold border-b border-slate-800">
                    <tr>
                        <th class="px-6 py-4">Nama Mesin</th>
                        <th class="px-6 py-4">Cloud ID / SN</th>
                        <th class="px-6 py-4">Status Integrasi</th>
                        <th class="px-6 py-4">Terakhir Sinkronisasi</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-800/60 text-slate-300">
                    @foreach($machines as $m)
                        @php
                            $mCalcStatus = $m->calculatedStatus();
                        @endphp
                        <tr class="hover:bg-slate-800/40 transition-colors">
                            <td class="px-6 py-4 font-bold text-white">
                                {{ $m->name }}
                            </td>
                            <td class="px-6 py-4 font-mono text-sky-400">
                                {{ $m->cloud_id ?: '-' }}
                            </td>
                            <td class="px-6 py-4">
                                @if($mCalcStatus === 'online')
                                    <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase tracking-wider bg-emerald-500/10 text-emerald-400 border border-emerald-500/20">Sinkronisasi Aktif</span>
                                @elseif($mCalcStatus === 'stale')
                                    <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase tracking-wider bg-amber-500/10 text-amber-400 border border-amber-500/20">Stale (Delay)</span>
                                @elseif($mCalcStatus === 'never_connected')
                                    <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase tracking-wider bg-slate-700/60 text-slate-400 border border-slate-600/40">Belum Ada Data</span>
                                @else
                                    <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase tracking-wider bg-rose-500/10 text-rose-400 border border-rose-500/20">Offline</span>
                                @endif
                            </td>
                            <td class="px-6 py-4 text-slate-400 font-mono">
                                {{ $m->last_sync ? $m->last_sync->diffForHumans() : '-' }}
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>
