<table cellspacing="0" cellpadding="0" width="900" border="1">
    <tr>
        <td colspan="15" align="center">
            <strong>LAPORAN JUMLAH PASIEN {{ $bulan }} {{ $tahun }}</strong>
        </td>
    </tr>
    <tr>
        <td rowspan="2" align="center"><strong>TGL</strong></td>
        <td colspan="10" align="center"><strong>ASAL PENDERITA</strong></td>
        <td colspan="2" align="center"><strong>ASAL</strong></td>
        <td colspan="2" align="center"><strong>J.KEL</strong></td>
    </tr>
    <tr>
        <td align="center"><strong>Billing</strong></td>
        <td align="center"><strong>LUAR</strong></td>
        <td align="center"><strong>UMUM</strong></td>
        <td align="center"><strong>JKN</strong></td>
        <td align="center"><strong>J.MAS</strong></td>
        <td align="center"><strong>J.DA</strong></td>
        <td align="center"><strong>TAG</strong></td>
        <td align="center"><strong>HR</strong></td>
        <td align="center"><strong>PG</strong></td>
        <td align="center"><strong>GCU</strong></td>
        <td align="center"><strong>III</strong></td>
        <td align="center"><strong>PAV</strong></td>
        <td align="center"><strong>L</strong></td>
        <td align="center"><strong>P</strong></td>
    </tr>
@php 
use App\Periksa;

@endphp
    @foreach ($orderbydate as $result)
        @php
            $tanggal = $result->day;
            // Initialize counters
            $counts = [
                'm' => 0, 'f' => 0, 'jkn' => 0, 'umum' => 0, 'tag' => 0,
                'gcu' => 0, 'bill' => 0, 'swasta' => 0, 'electiv' => 0, 'cito' => 0,
                'pav' => 0, 'biasa' => 0, 'luar' => 0
            ];

            // Ambil data pasien berdasarkan tanggal
            $getalldata = Periksa::where('daftar', 'LIKE', $tanggal.'%')->get();

            foreach ($getalldata as $rdata) {
                $jk = $rdata->jkpasien;
                $asalpasien = $rdata->asalpasien;
                $asuransi = $rdata->asuransi;

                // Hitung berdasarkan kategori
                if ($jk == 'L') $counts['m']++;
                else $counts['f']++;

                if ($asalpasien == 'Paviliun') $counts['pav']++;
                elseif ($asalpasien == 'Rujukan' || $asalpasien == 'APS') $counts['luar']++;

                // Hitung asuransi
                $asuransiMap = [
                    'JKN' => 'jkn', 'Umum' => 'umum', 'TAG' => 'tag', 'GCU' => 'gcu', 'Billing' => 'bill'
                ];

                if (array_key_exists($asuransi, $asuransiMap)) {
                    $counts[$asuransiMap[$asuransi]]++;
                } else {
                    $counts['biasa']++;
                }
            }
        @endphp
        <tr>
            <td align="left">{{ $tanggal }}</td>
            <td align="center">{{ $counts['bill'] }}</td>
            <td align="center">{{ $counts['luar'] }}</td>
            <td align="center">{{ $counts['umum'] }}</td>
            <td align="center">{{ $counts['jkn'] }}</td>
            <td align="center"></td>
            <td align="center"></td>
            <td align="center">{{ $counts['tag'] }}</td>
            <td align="center"></td>
            <td align="center"></td>
            <td align="center">{{ $counts['gcu'] }}</td>
            <td align="center">{{ $counts['biasa'] }}</td>
            <td align="center">{{ $counts['pav'] }}</td>
            <td align="center">{{ $counts['m'] }}</td>
            <td align="center">{{ $counts['f'] }}</td>
        </tr>
    @endforeach

    <tr>
        <td align="left" colspan="15">&nbsp;</td>
    </tr>
</table>
