@extends('base.layout')

@section('content')
<div class="wrapper">
    <div class="container-fluid">
        <div class="row">
            <div class="col-12">
                <div class="page-title-box">
                    <h4 class="page-title">Notifikasi Nilai Kritis</h4>
                </div>
            </div>
        </div>

        @if (session('success'))
            <div class="row">
                <div class="col-12">
                    <div class="alert alert-success">{{ session('success') }}</div>
                </div>
            </div>
        @endif
        @if (session('error'))
            <div class="row">
                <div class="col-12">
                    <div class="alert alert-danger">{{ session('error') }}</div>
                </div>
            </div>
        @endif

        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center mb-3">
                            <h5 class="card-title mb-0">Sample Yang Belum Dilaporkan</h5>
                            <a href="{{ route('criticalValueNotificationExport', ['type' => 'pending']) }}" class="btn btn-success btn-sm">
                                Export Excel
                            </a>
                        </div>

                        @if($items->isEmpty())
                            <div class="alert alert-info mb-0">Tidak ada sample nilai kritis yang perlu dilaporkan.</div>
                        @else
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered">
                                    <thead>
                                        <tr>
                                            <th>No</th>
                                            <th>No Foto</th>
                                            <th>No Register</th>
                                            <th>Pasien</th>
                                            <th>Asal Pasien</th>
                                            <th>Spesimen</th>
                                            <th>Waktu Set Nilai Kritis</th>
                                            <th>Waktu Ditindaklanjuti</th>
                                            <th>Aksi</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($items as $index => $item)
                                            <tr @if((string) $highlightId === (string) $item->id) class="table-warning" @endif>
                                                <td>{{ $index + 1 }}</td>
                                                <td>{{ $item->nofoto ?? '-' }}</td>
                                                <td>{{ $item->noregister ?? '-' }}</td>
                                                <td>{{ $item->nmpasien ?? '-' }}</td>
                                                <td>{{ $item->asalpasien ?? '-' }}</td> 
                                                <td>{{ $item->nm_spesimen ?? '-' }}</td>
                                                <td>
                                                    {{ $item->critical_set_at ? \Carbon\Carbon::parse($item->critical_set_at)->format('d-m-Y H:i:s') : '-' }}
                                                    @if(!empty($item->critical_set_by_name))
                                                        <br><small>oleh {{ $item->critical_set_by_name }}</small>
                                                    @endif
                                                </td>
                                                <td>-</td>
                                                <td>
                                                    <form action="{{ route('criticalValueNotificationMarkRead', $item->id) }}" method="POST" class="mb-0">
                                                        @csrf
                                                        <div class="form-group">
                                                            <label for="caratindaklanjut" class="col-form-label">Melalui <font color="red">*</font></label>
                                                            <select class="form-control" id="caratindaklanjut" name="caratindaklanjut">
                                                                <option value="">Belum di Laporkan</option>
                                                                <option value="Telpon">Telpon</option>
                                                                <option value="Whatshap">Whatshap</option>
                                                                <option value="Email">Email</option>
                                                                <option value="Ketemu di Jalan">Ketemu di Jalan</option>
                                                            </select>
                                                        </div>
                                                        <div class="form-group">
                                                            <label for="penerima_laporan" class="col-form-label">Penerima Laporan <font color="red">*</font></label>
                                                            <input type="text" id="penerima_laporan" name="penerima_laporan" class="form-control">
                                                        </div>
                                                        <div class="form-group">
                                                            <label for="alasan_belum_laporkan" class="col-form-label">Alasan belum dilaporkan/Pasien meninggal<font color="red">*</font></label>
                                                            <input type="text" id="alasan_belum_laporkan" name="alasan_belum_laporkan" class="form-control" placeholder="Isi jika belum di laporkan (Meninggal / Pindah Rumah Sakit / dll)">
                                                        </div>
                                                        <button type="submit" class="btn btn-sm btn-warning">Mark as Read</button>
                                                    </form>
                                                </td>
                                            </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center mb-3">
                            <h5 class="card-title mb-0">Riwayat Nilai Kritis Yang Sudah Ditindaklanjuti</h5>
                            <a href="{{ route('criticalValueNotificationExport', ['type' => 'followed', 'start_date' => $startDate, 'end_date' => $endDate]) }}" class="btn btn-success btn-sm">
                                Export Excel
                            </a>
                        </div>
                        <form method="GET" action="{{ route('criticalValueNotifications') }}" class="mb-3">
                            <div class="form-row align-items-end">
                                <div class="form-group col-md-3">
                                    <label for="start_date">Start Date Report</label>
                                    <input type="date" id="start_date" name="start_date" class="form-control" value="{{ $startDate }}">
                                </div>
                                <div class="form-group col-md-3">
                                    <label for="end_date">End Date Report</label>
                                    <input type="date" id="end_date" name="end_date" class="form-control" value="{{ $endDate }}">
                                </div>
                                <div class="form-group col-md-6">
                                    <button type="submit" class="btn btn-primary btn-sm">Tampilkan</button>
                                    <a href="{{ route('criticalValueNotifications') }}" class="btn btn-secondary btn-sm">Reset</a>
                                </div>
                            </div>
                        </form>
                        @if($followedItems->isEmpty())
                            <div class="alert alert-secondary mb-0">Belum ada riwayat tindak lanjut.</div>
                        @else
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered">
                                    <thead>
                                        <tr>
                                            <th>No</th>
                                            <th>No Foto</th>
                                            <th>No Register</th>
                                            <th>Pasien</th>
                                            <th>Asal Pasien</th>
                                            <th>Spesimen</th>
                                            <th>Waktu Set Nilai Kritis</th>
                                            <th>Waktu Ditindaklanjuti</th>
                                            <th>Selisih Waktu</th>
                                            <th>Status TAT</th>
                                            <th>Aksi</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($followedItems as $index => $item)
                                            <tr @if((string) $highlightId === (string) $item->id) class="table-warning" @endif>
                                                <td>{{ $index + 1 }}</td>
                                                <td>{{ $item->nofoto ?? '-' }}</td>
                                                <td>{{ $item->noregister ?? '-' }}</td>
                                                <td>{{ $item->nmpasien ?? '-' }}</td>
                                                <td>{{ $item->asalpasien ?? '-' }}</td>
                                                <td>{{ $item->nm_spesimen ?? '-' }}</td>
                                                <td>
                                                    {{ $item->critical_set_at ? \Carbon\Carbon::parse($item->critical_set_at)->format('d-m-Y H:i:s') : '-' }}
                                                    @if(!empty($item->critical_set_by_name))
                                                        <br><small>oleh {{ $item->critical_set_by_name }}</small>
                                                    @endif
                                                </td>
                                                <td>
                                                    {{ $item->followed_up_at ? \Carbon\Carbon::parse($item->followed_up_at)->format('d-m-Y H:i:s') : '-' }}
                                                    @if(!empty($item->followed_up_by_name))
                                                        <br><small>oleh {{ $item->followed_up_by_name }}</small>
                                                    @endif
                                                </td>
                                                <td>{{ $item->tat_duration_label ?? '-' }}</td>
                                                <td>
                                                    @if(($item->tat_status_label ?? '-') === 'Memenuhi TAT')
                                                        <span class="badge badge-success">{{ $item->tat_status_label }}</span>
                                                    @elseif(($item->tat_status_label ?? '-') === 'Tidak Memenuhi TAT')
                                                        <span class="badge badge-danger">{{ $item->tat_status_label }}</span>
                                                    @else
                                                        <span class="badge badge-secondary">-</span>
                                                    @endif
                                                </td>
                                                <td>
                                                    @php
                                                        $status = $item->follow_up_status ?? '';
                                                        $method = $item->follow_up_method ?? '';
                                                        $recipient = $item->follow_up_recipient ?? '';
                                                        $reason = $item->follow_up_reason ?? '';
                                                    @endphp

                                                    @if($status === 'reported')
                                                        <div><strong>Dilaporkan</strong></div>
                                                        <small class="text-muted">
                                                            Melalui: {{ $method !== '' ? $method : '-' }}<br>
                                                            Penerima: {{ $recipient !== '' ? $recipient : '-' }}
                                                        </small>
                                                    @elseif($status === 'not_reported')
                                                        <div><strong>Belum Dilaporkan</strong></div>
                                                        <small class="text-muted">
                                                            Alasan: {{ $reason !== '' ? $reason : '-' }}
                                                        </small>
                                                    @else
                                                        <small class="text-muted">-</small>
                                                    @endif
                                                </td>
                                            </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
