<!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>
    <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">Jenis Kelamin</td>
            <td valign="top">:</td>
            <td valign="top">{{ $periksa->jkpasien }}</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">Usia</td>
            <td valign="top">:</td>
            <td valign="top">{{ $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">
                @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 ($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
                    @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

            </td>
        </tr>
        <tr>
            <td width="45">&nbsp;</td>
            <td valign="top" colspan="7" width="755">
                @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;</td>
            <td valign="top" colspan="3" align="center">RSUD dr. Saiful Anwar <br />Dokter PJ Pemeriksaan Lab</td>
        </tr>
        <tr>
            <td width="45">&nbsp;</td>
            <td valign="top" colspan="4" align="center">&nbsp;</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="left">{{ $periksa->nmanalis }}</td>
            <td valign="top" colspan="3" align="center">{{ $periksa->nmdokter }}</td>
        </tr>
    </table>
     <font color="white">DEFAULT</font>
</body>
</html>
