<div class="table-responsive">
    <table class="table table-bordered table-striped table-hover mb-0">
        <thead class="thead-light">
            <tr>
                <th class="text-center" width="50">#</th>
                <!-- <th>Tanggal / Hari</th> -->
                <th>Barang</th>
                <th class="text-right">Jumlah</th>
                <th class="text-right">Total</th>
                @if(isset($showBranch) && $showBranch)
                <th class="text-center">Cabang</th>
                @endif
                @if(!isset($readonly) || !$readonly)
                <th class="text-center" width="120">Aksi</th>
                @endif
            </tr>
        </thead>
        <tbody>
        <tbody>
            @php 
                $currentDate = ''; 
                $dailyTotal = 0;
            @endphp
            @forelse($purchases as $index => $p)
                @php
                    // Set locale to Indonesian for day and month names
                    \Carbon\Carbon::setLocale('id');
                    $date = \Carbon\Carbon::parse($p->date);
                    $dateString = $date->translatedFormat('l, d F Y');
                    $dailyTotal += $p->total_price;
                @endphp
                @if($dateString !== $currentDate)
                    @php $currentDate = $dateString; @endphp
                    @php 
                        $colspan = 5;
                        if(isset($showBranch) && $showBranch) $colspan++;
                        if(!isset($readonly) || !$readonly) $colspan++;
                    @endphp
                    <tr class="bg-light font-weight-bold">
                        <td colspan="{{ $colspan }}">{{ $dateString }}</td>
                    </tr>
                @endif
                <tr>
                    <td class="text-center">{{ $index + 1 }}</td>
                    <!-- <td></td> -->
                    <td><strong>{{ $p->item }}</strong></td>
                    <td class="text-right">{{ $p->quantity }}</td>
                    <td class="text-right">Rp{{ number_format($p->total_price, 0, ',', '.') }}</td>
                    @if(isset($showBranch) && $showBranch)
                    <td class="text-center"><span class="badge badge-info">{{ $p->branch }}</span></td>
                    @endif
                    @if(!isset($readonly) || !$readonly)
                    <td class="text-center">
                        <form action="/laporan/pembelian/{{ $p->id }}" method="POST" class="d-inline" onsubmit="return confirm('Yakin ingin menghapus?')">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="btn btn-danger" title="Hapus">
                                <i class="fas fa-trash"></i>
                            </button>
                        </form>
                    </td>
                    @endif
                </tr>
                @php
                    // Determine if the next item has a different date, or if this is the last item
                    $nextDateString = '';
                    if(isset($purchases[$index + 1])) {
                        $nextDate = \Carbon\Carbon::parse($purchases[$index + 1]->date);
                        $nextDateString = $nextDate->translatedFormat('l, d F Y');
                    }
                @endphp
                @if($loop->last || $nextDateString !== $currentDate)
                    @php
                        $subtotalColspan = 3;
                        if(isset($showBranch) && $showBranch) $subtotalColspan++;
                    @endphp
                    <tr class="bg-light font-weight-boldtext-muted">
                        <td colspan="{{ $subtotalColspan }}" class="text-right text-muted" style="font-size: 0.9rem;">Total Pengeluaran {{ $currentDate }}:</td>
                        <td class="text-right text-dark" style="font-size: 0.95rem; font-weight: bold;">Rp{{ number_format($dailyTotal, 0, ',', '.') }}</td>
                        @if(!isset($readonly) || !$readonly)
                        <td></td>
                        @endif
                    </tr>
                    @php $dailyTotal = 0; @endphp <!-- Reset for the next day -->
                @endif
            @empty
                <tr>
                    <td colspan="6" class="text-center text-muted py-4">Belum ada data transaksi.</td>
                </tr>
            @endforelse
        </tbody>
        @if(!$purchases->isEmpty())
        <tfoot class="bg-light">
            @php
                $totalKeseluruhan = $purchases->sum('total_price');
                $footerColspan = 3;
                if(isset($showBranch) && $showBranch) $footerColspan++;
            @endphp
            <tr class="font-weight-bold">
                <td colspan="{{ $footerColspan }}" class="text-right">TOTAL KESELURUHAN:</td>
                <td class="text-right text-orange" style="font-size: 1.1rem;">Rp{{ number_format($totalKeseluruhan, 0, ',', '.') }}</td>
                @if(!isset($readonly) || !$readonly)
                <td></td>
                @endif
            </tr>
        </tfoot>
        @endif
    </table>
</div>
