<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <style>
        .title { font-family: sans-serif; font-size: 14pt; font-weight: bold; text-align: center; }
        .subtitle { font-family: sans-serif; font-size: 10pt; text-align: center; color: #555; }
        .table-excel { border-collapse: collapse; width: 100%; margin-top: 20px; font-family: sans-serif; font-size: 10pt; }
        .table-excel th { background-color: #448b39; color: white; border: 1px solid #333; padding: 8px; font-weight: bold; }
        .table-excel td { border: 1px solid #ccc; padding: 6px; }
        .text-center { text-align: center; }
        .text-right { text-align: right; }
        .fw-bold { font-weight: bold; background-color: #f2f2f2; }
    </style>
</head>
<body>

    <table>
        <tr>
            <td colspan="{{ $tab === 'barang' ? 6 : 5 }}" class="title">TEH TARIK 99 - LAPORAN PENJUALAN KASIR</td>
        </tr>
        <tr>
            <td colspan="{{ $tab === 'barang' ? 6 : 5 }}" class="subtitle">Periode Filter: {{ $tgl_mulai ?: 'Semua' }} s/d {{ $tgl_selesai ?: 'Semua' }}</td>
        </tr>
        <tr>
            <td colspan="{{ $tab === 'barang' ? 6 : 5 }}" class="subtitle">Tipe Laporan: Per {{ ucfirst($tab) }} | Dicetak oleh: {{ $user }}</td>
        </tr>
        @if($search)
        <tr>
            <td colspan="{{ $tab === 'barang' ? 6 : 5 }}" class="subtitle">Pencarian: "{{ $search }}"</td>
        </tr>
        @endif
        <tr><td></td></tr>
    </table>

    <table class="table-excel">
        <thead>
            <tr>
                <th style="background-color: #448b39; color: #ffffff;">NO</th>
                <th style="background-color: #448b39; color: #ffffff;">TANGGAL & WAKTU</th>
                @if($tab === 'barang')
                    <th style="background-color: #448b39; color: #ffffff;">NAMA PRODUK</th>
                    <th style="background-color: #448b39; color: #ffffff;">JUMLAH ITEM</th>
                    <th style="background-color: #448b39; color: #ffffff;">HARGA SATUAN</th>
                @else
                    <th style="background-color: #448b39; color: #ffffff;">NO. TRANSAKSI</th>
                    <th style="background-color: #448b39; color: #ffffff;">JUMLAH ITEM</th>
                    <th style="background-color: #448b39; color: #ffffff;">METODE PEMBAYARAN</th>
                @endif
                <th style="background-color: #448b39; color: #ffffff;">{{ $tab === 'barang' ? 'TOTAL PENDAPATAN' : 'TOTAL BAYAR' }}</th>
            </tr>
        </thead>
        <tbody>
            @php $totalRow = count($data); @endphp
            @foreach($data as $index => $item)
            <tr>
                <td class="text-center">{{ $index + 1 }}</td>
                <td class="text-center">{{ ($item->transaksi ? $item->transaksi->created_at : $item->created_at)->format('d M Y, H:i') }} WIB</td>
                @if($tab === 'barang')
                    <td>{{ $item->nama_produk }}</td>
                    <td class="text-center">{{ $item->qty }}</td>
                    <td class="text-right">{{ $item->harga_satuan }}</td>
                @else
                    <td>TRX-{{ str_pad($item->transaksi_id, 5, '0', STR_PAD_LEFT) }}</td>
                    <td class="text-center">{{ $item->qty }}</td>
                    <td>{{ $item->transaksi->metode_pembayaran ?? 'Tunai' }}</td>
                @endif
                <td class="text-right fw-bold">{{ $item->qty * $item->harga_satuan }}</td>
            </tr>
            @endforeach

            @if($totalRow > 0)
                @php 
                    $startRow = 7; // Row where data starts in Excel
                    if ($search) $startRow += 1;
                    $endRow = $startRow + $totalRow - 1; 
                    $colTotal = $tab === 'barang' ? 'F' : 'E';
                @endphp
                <tr>
                    <td colspan="3" class="text-center fw-bold">TOTAL AKUMULASI</td>
                    <td class="text-center fw-bold">=SUM(D{{ $startRow }}:D{{ $endRow }})</td>
                    @if($tab === 'barang')
                        <td class="fw-bold"></td>
                    @endif
                    <td class="text-right fw-bold">=SUM({{ $colTotal }}{{ $startRow }}:{{ $colTotal }}{{ $endRow }})</td>
                </tr>
            @else
                <tr>
                    <td colspan="{{ $tab === 'barang' ? 6 : 5 }}" class="text-center">Data transaksi tidak ditemukan.</td>
                </tr>
            @endif
        </tbody>
    </table>

</body>
</html>