<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ $periksa->nmpasien }} ({{ $periksa->noregister }} - {{ $periksa->nofoto }})</title>

</head>
<body>
    @php 
        $data               = [];
        $keterangan         = $periksa->keterangan;
        $statuspemeriksaan  = $periksa->status;
        $data['keterangan'] = $periksa->keterangan;
        if (!empty($komponens)) {
            foreach ($komponens as $rows) {
                $key = trim($rows->komponen);
                if (!empty($key)) {
                    if ($key == 'keterangan') {

                    } else {
                        $data[$key] = $rows->isidata;
                    }
                }
            }
        }
    @endphp
    <table cellpadding="0" cellspacing="0" width="800" border="0">
        @if ($kopsurat == '')
            <tr>
                <td width="45" height="200">&nbsp;</td>
                <td width="173" valign="top">&nbsp;</td>
                <td width="12" valign="top">&nbsp;</td>
                <td width="165" valign="top">&nbsp;</td>
                <td width="115" valign="top">&nbsp;</td>
                <td width="12" valign="top">&nbsp;</td>
                <td width="128" valign="top">&nbsp;</td>
                <td width="254" valign="top">&nbsp;</td>
            </tr>
        @else 
            <tr>
                <td colspan="8"><img src="{!! $kopsurat !!}" width="100%" /></td>
            </tr>
        @endif
        <tr>
            <td colspan="8" align="center"><p><strong><u>HASIL PEMERIKSAAN MIKROBIOLOGI KLINIK</u></strong></p></td>
        </tr>
        <tr>
            <td valign="top" width="45">&nbsp;</td>
            <td valign="top" width="173">No. Laboratorium</td>
            <td valign="top" width="12">:</td>
            <td valign="top" width="165">{{ $periksa->nofoto }}</td>
            <td valign="top" width="115">Tanggal MRS</td>
            <td valign="top" width="12">:</td>
            <td valign="top" colspan="2">{!! $periksa->mulai !!}</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td valign="top">Nama Pasien</td>
            <td valign="top">:</td>
            <td valign="top">{{ $periksa->nmpasien }}</td>
            <td valign="top">Ruang Pengirim</td>
            <td valign="top">:</td>
            <td valign="top" colspan="2">{!! $periksa->asalpasien !!}</td>
        </tr>
        <tr>
            <td valign="top">&nbsp;</td>
            <td valign="top">No.Rekam Medik</td>
            <td valign="top">:</td>
            <td valign="top">{{ $periksa->noregister }}</td>
            <td valign="top">Diagnosa</td>
            <td valign="top">:</td>
            <td valign="top" colspan="2">{!! $periksa->klinis !!}</td>
        </tr>

        <tr>
            <td valign="top">&nbsp;</td>
            <td valign="top">Jenis Kelamin / Usia</td>
            <td valign="top">:</td>
            <td valign="top">{{ $periksa->jkpasien }} / {{ $periksa->usia }}</td>
            <td valign="top">Jenis Pemeriksaan</td>
            <td valign="top">:</td>
            <td valign="top" colspan="2">{!! $periksa->reques !!}</td>
        </tr>

        <tr>
            <td valign="top">&nbsp;</td>
            <td valign="top">Tanggal Kirim Sample</td>
            <td valign="top">:</td>
            <td valign="top">{{ $periksa->daftar }}</td>
            <td valign="top">Spesimen</td>
            <td valign="top">:</td>
            <td valign="top" colspan="2">{!! $periksa->nm_spesimen !!}</td>
        </tr>

        <tr>
            <td valign="top">&nbsp;</td>
            <td valign="top">Tanggal Selesai Hasil</td>
            <td valign="top">:</td>
            <td valign="top">{{ $periksa->verifikasi }}</td>
            <td valign="top">&nbsp;</td>
            <td valign="top">&nbsp;</td>
            <td valign="top" colspan="2">&nbsp;</td>
        </tr>
        <tr><td colspan="8">&nbsp;</td></tr>
        <tr>
            <td width="45">&nbsp;</td>
            <td valign="top" colspan="7" width="755">
                @php 
                $nomor = 1;
                @endphp
                <table style="width:100%">
                    @if (isset($data['id_selradang']) OR
                    isset($data['id_selepitel']) OR
                    isset($data['id_mikroorganisme']) OR
                    isset($data['id_mikroorganismeoptional']) OR
                    isset($data['id_jumlahlactobacillus']) OR
                    isset($data['id_jumlahmobiluncus']) OR
                    isset($data['id_lactobacillus']) OR
                    isset($data['id_gardnerella']) OR
                    isset($data['id_mobiluncus']) OR
                    isset($data['id_jumlahgardnerella']))
                        <tr><td valign="top" colspan="3">{{$nomor}}. Pewarnaan Gram :</td></tr>
                        @if (isset($data['id_selepitel'])) 
                            <tr>
                                <td valign="top" style="width:20%">Sel epithel</td>
                                <td valign="top" style="width:5%">:</td>
                                <td valign="top" style="width:75%">{!! $data['id_selepitel'] !!}</td>
                            </tr>
                        @endif
                        @if (isset($data['id_selradang'])) 
                            <tr>
                                <td valign="top" style="width:20%">Sel radang</td>
                                <td valign="top" style="width:5%">:</td>
                                <td valign="top" style="width:75%">{!! $data['id_selradang'] !!}</td>
                            </tr>
                        @endif
                        @if (isset($data['id_mikroorganisme']) OR isset($data['id_mikroorganismeoptional'])) 
                            <tr>
                                <td valign="top">Mikroorganisme</td>
                                <td valign="top">:</td>
                                <td valign="top">
                                    @if (isset($data['id_mikroorganisme']))
                                        {!! $data['id_mikroorganisme'] !!} 
                                    @endif
                                    @if (isset($data['id_mikroorganismeoptional']))
                                        <br />{!! $data['id_mikroorganismeoptional'] !!} 
                                    @endif
                                </td>
                            </tr>
                        @endif
                        @if (isset($data['id_jumlahlactobacillus']) OR isset($data['id_jumlahgardnerella']) OR isset($data['id_jumlahmobiluncus']) OR isset($data['id_lactobacillus']) OR isset($data['id_gardnerella']) OR isset($data['id_mobiluncus']))
                            <tr>
                                <td valign="top">&nbsp;</td>
                                <td valign="top" colspan="2">
                                    <table cellpadding="0" cellspacing="0" width="100%" border="1">
                                        <tr>
                                            <td width="30%" align="center"><strong>NUGGENT SCORE</strong></td>
                                            <td width="40%" align="center"><strong>Jumlah per lapang pandang besar</strong></td>
                                            <td width="30%" align="center"><strong>skor</strong></td>
                                        </tr>
                                        <tr>
                                            <td>Lactobacillus</td>
                                            <td align="center">
                                                @if (isset($data['id_jumlahlactobacillus']))
                                                    {!! $data['id_jumlahlactobacillus'] !!} 
                                                @endif
                                            </td>
                                            <td align="center">
                                                @if (isset($data['id_lactobacillus']))
                                                    {!! $data['id_lactobacillus'] !!} 
                                                    @php $id_lactobacillus = $data['id_lactobacillus']; @endphp
                                                @else 
                                                    @php $id_lactobacillus = 0; @endphp
                                                @endif
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>Gardnerella</td>
                                            <td align="center">
                                                @if (isset($data['id_jumlahgardnerella']))
                                                    {!! $data['id_jumlahgardnerella'] !!} 
                                                @endif
                                            </td>
                                            <td align="center">
                                                @if (isset($data['id_gardnerella']))
                                                    {!! $data['id_gardnerella'] !!} 
                                                    @php $id_gardnerella = $data['id_gardnerella']; @endphp
                                                @else 
                                                    @php $id_gardnerella = 0; @endphp
                                                @endif
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>Mobiluncus</td>
                                            <td align="center">
                                                @if (isset($data['id_jumlahmobiluncus']))
                                                    {!! $data['id_jumlahmobiluncus'] !!} 
                                                @endif
                                            </td>
                                            <td align="center">
                                                @if (isset($data['id_mobiluncus']))
                                                    {!! $data['id_mobiluncus'] !!} 
                                                    @php $id_mobiluncus = $data['id_mobiluncus']; @endphp
                                                @else 
                                                    @php $id_mobiluncus = 0; @endphp
                                                @endif
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>Total Score</td>
                                            <td>&nbsp;</td>
                                            <td align="center">
                                                @php 
                                                    $jumlah = (int)$id_lactobacillus + (int)$id_gardnerella + (int)$id_mobiluncus;
                                                    echo $jumlah;
                                                    if ($jumlah >= 0 && $jumlah <= 3) {
                                                        $keterangan = "Normal";
                                                    } elseif ($jumlah >= 4 && $jumlah <= 6) {
                                                        $keterangan = "Intermediate";
                                                    } elseif ($jumlah >= 7 && $jumlah <= 10) {
                                                        $keterangan = "Bacterial vaginosis";
                                                    } else {
                                                        $keterangan = "Nilai tidak valid";
                                                    }
                                                @endphp
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="3">KET : {{$keterangan}}</td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        @endif
                        @php
                            $nomor++;
                        @endphp
                    @endif
                    <tr><td colspan="3">&nbsp;</td></tr>
                    @if (isset($data['id_pewarnaanziehlnielsen']))
                        <tr>
                            <td valign="top">{{$nomor}}. Pewarnaan Ziehl Neelsen Pagi</td>
                            <td valign="top">:</td>
                            <td valign="top">
                                {!! $data['id_pewarnaanziehlnielsen'] !!}
                            </td>
                        </tr>
                        @php 
                            $nomor++;
                        @endphp
                        
                    @endif
                    @if (isset($data['id_pewarnaanziehlnielsensewaktu']))
                        <tr>
                            <td valign="top">{{$nomor}}. Pewarnaan Ziehl Neelsen Sewaktu</td>
                            <td valign="top">:</td>
                            <td valign="top">
                                {!! $data['id_pewarnaanziehlnielsensewaktu'] !!}
                            </td>
                        </tr>
                        @php 
                            $nomor++;
                        @endphp
                    @endif
                    @if (isset($data['id_pewarnaankoh']))
                        <tr><td colspan="3">&nbsp;</td></tr>
                        <tr>
                            <td valign="top">{{$nomor}}. Pewarnaan KOH</td>
                            <td valign="top">:</td>
                            <td valign="top">
                                @if (isset($data['id_pewarnaankoh']) AND $data['id_pewarnaankoh'] != 'lainnya') 
                                    {!! $data['id_pewarnaankoh'] !!}
                                @else
                                    {!! $data['id_pewarnaankohoptional'] ?? '' !!}
                                @endif
                            </td>
                        </tr>
                        @php 
                            $nomor++;
                        @endphp
                    @endif
                    @if (isset($data['id_pewarnaanneisser']))
                        <tr><td colspan="3">&nbsp;</td></tr>
                        <tr>
                            <td valign="top">{{$nomor}}. Pewarnaan Neelsen</td>
                            <td valign="top">:</td>
                            <td valign="top">{!! $data['id_pewarnaanneisser'] !!}</td>
                        </tr>
                        @php 
                            $nomor++;
                        @endphp
                    @endif
                    @if (isset($data['id_pewarnaannegatif']))
                        <tr><td colspan="3">&nbsp;</td></tr>
                        <tr>
                            <td valign="top">{{$nomor}}. Pewarnaan Negatif</td>
                            <td valign="top">:</td>
                            <td valign="top">{!! $data['id_pewarnaannegatif'] !!}</td>
                        </tr>
                        @php 
                            $nomor++;
                        @endphp
                    @endif
                    @if (isset($data['id_pewarnaanspora']))
                        <tr><td colspan="3">&nbsp;</td></tr>
                        <tr>
                            <td valign="top">{{$nomor}}. Pewarnaan Spora</td>
                            <td valign="top">:</td>
                            <td valign="top">{!! $data['id_pewarnaanspora'] !!}</td>
                        </tr>
                        @php 
                            $nomor++;
                        @endphp
                    @endif
                    @if (isset($data['id_pewarnaangiesma']) OR isset($data['id_pewarnaangiesmaoptional']) )
                        <tr><td colspan="3">&nbsp;</td></tr>
                        <tr>
                            <td valign="top">{{$nomor}}. Pewarnaan Giemsa</td>
                            <td valign="top">:</td>
                            <td valign="top">
                                @if (isset($data['id_pewarnaangiesma']) AND $data['id_pewarnaangiesma'] != 'lainnya') 
                                    {!! $data['id_pewarnaangiesma'] !!}
                                @else
                                    {!! $data['id_pewarnaangiesmaoptional'] ?? '' !!} 
                                @endif
                            </td>
                        </tr>
                        @php 
                            $nomor++;
                        @endphp
                    @endif
                    <tr><td colspan="3">&nbsp;</td></tr>
                    @if (isset($data['id_biakankultur']))
                        <tr>
                            <td valign="top">{{$nomor}}. Biakan Kultur (Aerob / Anaerob)</td>
                            <td valign="top">:</td>
                            <td valign="top">
                                {!! $data['id_biakankultur'] !!}
                            </td>
                        </tr>
                        @php
                            $nomor++;
                        @endphp
                    @endif
                </table>
                @php
                    $cekketerangan = explode('/table', $keterangan);
                    $cetakjudul = false;
                    $sudahcetak1= 0;
                    $sudahcetak2= 0;
                    $sudahcetak3= 0;
					$namakuman 	= '';
                    $namakuman1 = '';
                    $namakuman2 = '';
                @endphp
                @if (isset($antibiotiks) && $antibiotiks->isNotEmpty())
                    @php
                        $grouped = $antibiotiks->filter(function ($row) {
                            return $row->printrow; // hanya yang perlu dicetak
                        })->groupBy('kuman');
                        if (empty($grouped) OR $grouped->isEmpty()) {
                            if (isset($data['bakteri'])) {
                                if (isset($data['id_baktericetak']) AND $data['id_baktericetak'] == 'TIDAK') {
                                } else {
                                    echo "<p>Bakteri Yang ditemukan : <strong><i>" . $data['bakteri'] . "</i></strong> " . ($data['bakterisir'] ?? '') . "</p>";
                                    $namakuman 	= $data['bakteri'];
                                }
                            }
                            if (isset($data['id_bakteri01']) AND $namakuman != $data['id_bakteri01']) {
                                echo "<p>Bakteri Yang ditemukan : <strong><i>" . $data['id_bakteri01'] . "</i></strong> " . ($data['id_bakterisir01'] ?? '') . "</p>";
                                $namakuman1 	= $data['id_bakteri01'];
                            }
                            if (isset($data['id_bakteri02']) AND $namakuman != $data['id_bakteri02']) {
                                echo "<p>Bakteri Yang ditemukan : <strong><i>" . $data['id_bakteri02'] . "</i></strong> " . ($data['id_bakterisir02'] ?? '') . "</p>";
                                $namakuman2 	= $data['id_bakteri02'];
                            }
                        }
                    @endphp
                    @foreach ($grouped as $kuman => $items)
                        @php
                        $cetakjudul = false;
                        @endphp
                        @if (isset($data['bakteri']) AND $data['bakteri'] == $kuman)
                            @if (isset($data['id_baktericetak']) AND $data['id_baktericetak'] == 'TIDAK')
                            @else
                                @if ($sudahcetak1 == 0)
                                    <p>Bakteri Yang ditemukan : <strong><i>{{ $data['bakteri'] }}</i></strong> {{ $data['bakterisir'] ?? '' }}</p>
									@if (isset($data['id_bakterihitungkolteks']))
										<p>Jumlah Koloni : <strong><i>{{ $data['id_bakterihitungkolteks'] }}</i></strong></p>
									@elseif (isset($data['id_bakterihitungkol']))
										<p>Jumlah Koloni : <strong><i>{{ $data['id_bakterihitungkol'] }}</i></strong></p>
									@endif
									@php
                                        $sudahcetak1= 1;
										$cetakjudul = true; 
										$namakuman 	= $data['bakteri'];
									@endphp
                                @endif
                            @endif
                        @endif
                        @if (isset($data['id_bakteri01']) AND $data['id_bakteri01'] == $kuman)
                            @if (isset($data['id_bakteri01cetak']) AND $data['id_bakteri01cetak'] == 'TIDAK')
                            @else
                                @if ($sudahcetak2 == 0)
                                    <p>Bakteri Yang ditemukan : <strong><i>{{ $data['id_bakteri01'] }}</i></strong> {{ $data['id_bakterisir01'] ?? '' }}</p>
                                    @if (isset($data['id_bakterihitungkolteks01']))
                                        <p>Jumlah Koloni : <strong><i>{{ $data['id_bakterihitungkolteks01'] }}</i></strong></p>
                                    @elseif (isset($data['id_bakterihitungkol01']))
                                        <p>Jumlah Koloni : <strong><i>{{ $data['id_bakterihitungkol01'] }}</i></strong></p>
                                    @endif
                                    @php
                                        $sudahcetak2= 1;
                                        $cetakjudul = true;
                                        $namakuman1 = $data['id_bakteri01'];
                                    @endphp
                                @endif
                            @endif
                        @endif
                        @if (isset($data['id_bakteri02']) AND $data['id_bakteri02'] == $kuman)
                            @if (isset($data['id_bakteri02cetak']) AND $data['id_bakteri02cetak'] == 'TIDAK')
                            @else
                                @if ($sudahcetak3 == 0)
                                    <p>Bakteri Yang ditemukan : <strong><i>{{ $data['id_bakteri02'] }}</i></strong> {{ $data['id_bakterisir02'] ?? '' }}</p>
                                    @if (isset($data['id_bakterihitungkolteks02']))
                                        <p>Jumlah Koloni : <strong><i>{{ $data['id_bakterihitungkolteks02'] }}</i></strong></p>
                                    @elseif (isset($data['id_bakterihitungkol02']))
                                        <p>Jumlah Koloni : <strong><i>{{ $data['id_bakterihitungkol02'] }}</i></strong></p>
                                    @endif
                                    @php
                                        $sudahcetak3 = 1;
                                        $cetakjudul = true;
                                        $namakuman2 = $data['id_bakteri02'];
                                    @endphp
                                @endif
                            @endif
                        @endif
                        @php
                            $total  = $items->count();
                            $half   = ceil($total / 2);
                            $left   = $items->slice(0, $half)->values();
                            $right  = $items->slice($half)->values();
                        @endphp
                        @if ($statuspemeriksaan == 'Preliminary Results')
                            <p><em><strong>Uji Kepekaan antibiotik on going</strong></em></p>
                        @else
                            @if ($cetakjudul)
                                <table style="width:100%; border-collapse: collapse;" border="1" cellpadding="3" cellspacing="0">
                                    <thead>
                                        <tr style="background-color: #f2f2f2;">
                                            <th>No</th>
                                            <th>Antibiotik</th>
                                            <th>Value</th>
                                            <th>Interpretation</th>
                                            <th>&nbsp;</th>
                                            <th>No</th>
                                            <th>Antibiotik</th>
                                            <th>Value</th>
                                            <th>Interpretation</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @for ($i = 0; $i < $half; $i++)
                                            <tr>
                                                {{-- Kolom kiri --}}
                                                @if (isset($left[$i]) AND $left[$i]->printrow)
                                                    <td>{{ $i + 1 }}</td>
                                                    <td>{{ $left[$i]->antibiotic }}</td>
                                                    <td>{{ $left[$i]->printcol == 1 ? $left[$i]->value : '' }}</td>
                                                    <td align="center">{{ $left[$i]->interpretation }}</td>
                                                @else
                                                    <td colspan="4">&nbsp;</td>
                                                @endif

                                                <td>&nbsp;</td> {{-- Pemisah tengah --}}

                                                {{-- Kolom kanan --}}
                                                @if (isset($right[$i]) AND $right[$i]->printrow)
                                                    <td>{{ $i + 1 + $half }}</td>
                                                    <td>{{ $right[$i]->antibiotic }}</td>
                                                    <td>{{ $right[$i]->printcol == 1 ? $right[$i]->value : '' }}</td>
                                                    <td align="center">{{ $right[$i]->interpretation }}</td>
                                                @else
                                                    <td colspan="4">&nbsp;</td>
                                                @endif
                                            </tr>
                                        @endfor
                                    </tbody>
                                </table>
                            @endif
                        @endif
                    @endforeach
                @endif
                @php 
                    if (isset($data['bakteri']) AND $namakuman == '') {
                        if (isset($data['id_baktericetak']) AND $data['id_baktericetak'] == 'TIDAK') {
                        } else {
                            echo "<p>Bakteri Yang ditemukan : <strong><i>" . $data['bakteri'] . "</i></strong> " . ($data['bakterisir'] ?? '') . "</p>";
                            if (isset($data['id_bakterihitungkolteks'])){
			                    echo "<p>Jumlah Koloni : <strong><i>".$data['id_bakterihitungkolteks']."</i></strong></p>";
                            } elseif (isset($data['id_bakterihitungkol'])){
							    echo "<p>Jumlah Koloni : <strong><i>".$data['id_bakterihitungkol']."</i></strong></p>";
                            }
                        }
                    }
                    if (isset($data['id_bakteri01']) AND $namakuman1 == '') {
                        if (isset($data['id_bakteri01cetak']) AND $data['id_bakteri01cetak'] == 'TIDAK'){

                        } else {
                            echo "<p>Bakteri Yang ditemukan : <strong><i>" . $data['id_bakteri01'] . "</i></strong> " . ($data['id_bakterisir01'] ?? '') . "</p>";
                            if (isset($data['id_bakterihitungkolteks01'])){
                                echo "<p>Jumlah Koloni : <strong><i>".$data['id_bakterihitungkolteks01']."</i></strong></p>";
                            } elseif (isset($data['id_bakterihitungkol01'])){
                                echo "<p>Jumlah Koloni : <strong><i>".$data['id_bakterihitungkol01']."</i></strong></p>";
                            }
                        }
                    }
                    if (isset($data['id_bakteri02']) AND $namakuman2 == ''){
                        if (isset($data['id_bakteri02cetak']) AND $data['id_bakteri02cetak'] == 'TIDAK'){

                        } else{
                            echo "<p>Bakteri Yang ditemukan : <strong><i>" . $data['id_bakteri02'] . "</i></strong> " . ($data['id_bakterisir02'] ?? '') . "</p>";
                            if (isset($data['id_bakterihitungkolteks02'])){
                                echo "<p>Jumlah Koloni : <strong><i>".$data['id_bakterihitungkolteks02']."</i></strong></p>";
                            } elseif (isset($data['id_bakterihitungkol02'])){
                                echo "<p>Jumlah Koloni : <strong><i>".$data['id_bakterihitungkol02']."</i></strong></p>";
                            }
                        }
                    }
                @endphp
                @if (isset($periksa->keterangan))
                    <br />Komentar:<br />
                    {!! $periksa->keterangan !!}
                @endif
            </td>
        </tr>
        <tr><td colspan="8">&nbsp;</td></tr>
        <tr><td colspan="8">&nbsp;</td></tr>
        <tr>
            <td width="45">&nbsp;</td>
            <td valign="top" colspan="4" align="center">&nbsp;<br />Petugas</td>
            <td valign="top" colspan="3" align="center">Malang {!! $tanggal !!}<br />DPJP</td>
        </tr>
        <tr>
            <td width="45">&nbsp;</td>
            <td valign="top" colspan="4" align="center">{!! $tandatangananalis !!}</td>
            <td colspan="3" align="center" valign="top">{!! $tandatangan !!}</td>
        </tr>
        <tr><td colspan="8">&nbsp;</td></tr>
        <tr>
            <td width="45">&nbsp;</td>
            <td valign="top" colspan="4" align="center">{{ $periksa->nmanalis }}</td>
            <td valign="top" colspan="3" align="center">{{ $periksa->nmdokter }}</td>
        </tr>
    </table>
     <font color="white">KULTUR</font>
</body>
</html>
