@extends('layouts.app')

@section('title', __('nx600Lab::lang.nx600_lab') . ' — ' . __('nx600Lab::lang.dashboard'))

@section('content')
<section class="content-header">
    <h1>
        <i class="fas fa-vials"></i> {{ __('nx600Lab::lang.nx600_lab') }}
        @if($syncEnabled)
            <small><span class="label label-success"><i class="fas fa-paw"></i> VetClinic sync ON</span></small>
        @else
            <small><span class="label label-default">Standalone mode</span></small>
        @endif
    </h1>
</section>

<section class="content">

    @if(session('status'))
        <div class="alert alert-{{ session('status')['success'] ? 'success' : 'danger' }}">
            {{ session('status')['msg'] }}
        </div>
    @endif

    <div class="row">
        <div class="col-md-3 col-sm-6">
            <div class="info-box bg-aqua">
                <span class="info-box-icon"><i class="fa fa-list"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">Pending Worklist</span>
                    <span class="info-box-number">{{ $stats['pending_worklist'] }}</span>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6">
            <div class="info-box bg-green">
                <span class="info-box-icon"><i class="fa fa-flask"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">Results Today</span>
                    <span class="info-box-number">{{ $stats['sessions_today'] }}</span>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6">
            <div class="info-box bg-yellow">
                <span class="info-box-icon"><i class="fa fa-exclamation-triangle"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">Abnormal Today</span>
                    <span class="info-box-number">{{ $abnormalToday }}</span>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6">
            <div class="info-box {{ $stats['unack_errors'] > 0 ? 'bg-red' : 'bg-gray' }}">
                <span class="info-box-icon"><i class="fa fa-plug"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">Unack. Device Errors</span>
                    <span class="info-box-number">{{ $stats['unack_errors'] }}</span>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-8">
            @component('components.widget', ['class' => 'box-primary', 'title' => 'Latest Test Sessions'])
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>Sample No</th>
                            <th>Patient</th>
                            <th>Date / Time</th>
                            <th>Parameters</th>
                            <th>Status</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($recentSessions as $s)
                            <tr>
                                <td><strong>{{ $s->sample_no }}</strong></td>
                                <td>{{ $s->patient_name ?: '—' }}</td>
                                <td>{{ $s->test_date?->format('Y-m-d') }} {{ $s->test_time }}</td>
                                <td>{{ $s->testResults->count() }}</td>
                                <td>
                                    @if($s->is_abnormal)
                                        <span class="label label-warning">Abnormal</span>
                                    @else
                                        <span class="label label-success">Normal</span>
                                    @endif
                                </td>
                                <td><a href="{{ route('nx.results.show', $s->id) }}" class="btn btn-xs btn-default">View</a></td>
                            </tr>
                        @empty
                            <tr><td colspan="6" class="text-center text-muted">No test sessions yet.</td></tr>
                        @endforelse
                    </tbody>
                </table>
                <a href="{{ route('nx.results.index') }}" class="btn btn-link">All test results &raquo;</a>
            @endcomponent
        </div>

        <div class="col-md-4">
            @component('components.widget', ['class' => 'box-default', 'title' => 'Device Status'])
                @if($lastConnection)
                    <p>
                        <strong>Last connection:</strong><br>
                        {{ $lastConnection->device_ip }}:{{ $lastConnection->device_port }}<br>
                        <span class="label label-{{ $lastConnection->status === 'connected' ? 'success' : 'default' }}">
                            {{ ucfirst($lastConnection->status) }}
                        </span>
                        — {{ $lastConnection->connected_at?->diffForHumans() }}
                    </p>
                    <p class="text-muted">Comm type: {{ $lastConnection->comm_type }}</p>
                @else
                    <p class="text-muted">No device has connected yet.</p>
                @endif
                <hr>
                <p class="text-muted" style="font-size:12px;">
                    Start the listener with:<br>
                    <code>php artisan nx600lab:tcp-server</code><br>
                    <code>php artisan nx600lab:hl7-server</code>
                </p>
            @endcomponent

            @component('components.widget', ['class' => 'box-default', 'title' => 'Recent Activity'])
                <ul class="list-unstyled" style="max-height:260px;overflow-y:auto;">
                    @forelse($recentLogs as $log)
                        <li style="padding:4px 0;border-bottom:1px solid #f4f4f4;">
                            <span class="label {{ $log->level_badge }}">{{ strtoupper($log->level) }}</span>
                            <small>{{ $log->message }}</small>
                            <br><small class="text-muted">{{ $log->created_at?->diffForHumans() }}</small>
                        </li>
                    @empty
                        <li class="text-muted">No activity logged.</li>
                    @endforelse
                </ul>
            @endcomponent
        </div>
    </div>
</section>
@endsection
