<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Laporan Penjualan - {{ date('d M Y') }}</title>
    <style>
        body { font-family: sans-serif; font-size: 12px; color: #333; }
        .header { text-align: center; margin-bottom: 20px; }
        .header h1 { margin: 0; font-size: 20px; color: #111; }
        .header p { margin: 5px 0 0 0; color: #555; }
        table { width: 100%; border-collapse: collapse; margin-top: 10px; }
        th, td { border: 1px solid #aaa; padding: 8px; text-align: left; }
        th { background-color: #f3f4f6; font-weight: bold; text-transform: uppercase; font-size: 11px; }
        .text-right { text-align: right; }
        .text-center { text-align: center; }
        .total-row { font-weight: bold; background-color: #e5e7eb; }
        .status { padding: 3px 6px; border-radius: 4px; font-size: 10px; font-weight: bold; text-transform: uppercase; }
    </style>
</head>
<body>
    <div class="header">
        <h1>LAPORAN PENJUALAN TOKO MIFTAH</h1>
        <p>Dicetak pada: {{ date('d F Y H:i') }}</p>
    </div>

    <table>
        <thead>
            <tr>
                <th class="text-center" width="5%">No</th>
                <th width="15%">ID Pesanan</th>
                <th width="15%">Tanggal</th>
                <th width="20%">Pelanggan</th>
                <th width="15%">Status Pembayaran</th>
                <th width="15%">Status Pengiriman</th>
                <th class="text-right" width="15%">Total</th>
            </tr>
        </thead>
        <tbody>
            @php $totalKeseluruhan = 0; @endphp
            @forelse($pesanans as $index => $pesanan)
                @php 
                    $totalKeseluruhan += $pesanan->total_harga; 
                    $statusBayar = $pesanan->pembayaran->transaction_status ?? 
                                   (in_array($pesanan->status, ['diproses', 'dikirim', 'selesai']) ? 'berhasil' : 'pending');
                @endphp
                <tr>
                    <td class="text-center">{{ $index + 1 }}</td>
                    <td>#{{ $pesanan->id }}</td>
                    <td>{{ \Carbon\Carbon::parse($pesanan->tanggal_pesanan)->format('d M Y') }}</td>
                    <td>{{ $pesanan->user->nama ?? 'Pelanggan Umum' }}</td>
                    <td>{{ ucfirst($statusBayar) }}</td>
                    <td>{{ ucfirst($pesanan->status) }}</td>
                    <td class="text-right">Rp {{ number_format($pesanan->total_harga, 0, ',', '.') }}</td>
                </tr>
            @empty
                <tr>
                    <td colspan="7" class="text-center">Belum ada data pesanan.</td>
                </tr>
            @endforelse
        </tbody>
        <tfoot>
            <tr class="total-row">
                <td colspan="6" class="text-right">TOTAL PENDAPATAN KESELURUHAN</td>
                <td class="text-right">Rp {{ number_format($totalKeseluruhan, 0, ',', '.') }}</td>
            </tr>
        </tfoot>
    </table>
</body>
</html>
