<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Laporan Rincian Pendapatan</title>
    <style>
        @media print {
            @page {
                size: A4 portrait;
                margin: 15mm;
            }
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            font-size: 10px;
            line-height: 1.4;
            background: white;
            padding: 20px;
        }

        .header {
            text-align: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #333;
        }

        .header h1 {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 5px;
        }

        .header h2 {
            font-size: 14px;
            font-weight: normal;
            color: #666;
        }

        .header .period {
            font-size: 12px;
            margin-top: 10px;
        }

        .summary-section {
            display: flex;
            justify-content: space-around;
            margin-bottom: 20px;
            padding: 15px;
            background: #f9f9f9;
            border-radius: 5px;
            gap: 10px;
        }

        .summary-box {
            text-align: center;
            flex: 1;
        }

        .summary-box .label {
            font-size: 10px;
            color: #666;
            text-transform: uppercase;
        }

        .summary-box .value {
            font-size: 16px;
            font-weight: bold;
            color: #333;
        }

        .section-title {
            font-size: 12px;
            font-weight: bold;
            margin: 20px 0 10px;
            padding-bottom: 5px;
            border-bottom: 1px solid #ddd;
        }

        .detail-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }

        .detail-table th,
        .detail-table td {
            border: 1px solid #ddd;
            padding: 6px 8px;
            text-align: left;
            font-size: 9px;
            vertical-align: top;
        }

        .detail-table th {
            background: #f5f5f5;
            font-weight: bold;
            text-transform: uppercase;
        }

        .text-right {
            text-align: right;
        }

        .text-center {
            text-align: center;
        }

        .grand-total-row {
            background: #333;
            color: white;
            font-weight: bold;
        }

        .footer {
            margin-top: 30px;
            padding-top: 15px;
            border-top: 1px solid #ddd;
            display: flex;
            justify-content: space-between;
            font-size: 9px;
            color: #666;
        }

        @media screen {
            body {
                max-width: 210mm;
                margin: 20px auto;
                border: 1px solid #ccc;
                box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            }
        }
    </style>
</head>
<body onload="window.print()">
    @php
        $tab = $tab ?? request('tab', 'transaksi');
    @endphp

    <div class="header">
        @if($shopLogo ?? false)
            <img src="{{ $shopLogo }}" alt="{{ $shopName }}" style="max-height: 50px; margin-bottom: 10px;">
        @endif

        <h1>{{ $shopName }}</h1>

        @if($shopAddress)
            <p>{{ $shopAddress }}</p>
        @endif

        <h2>RINCIAN PENDAPATAN</h2>

        <div class="period">
            Periode: {{ \Carbon\Carbon::parse($startDate)->format('d M Y') }} - {{ \Carbon\Carbon::parse($endDate)->format('d M Y') }}
        </div>

        <div class="period">
            Mode: {{ $tab === 'produk' ? 'Per Produk' : 'Per Pembayaran' }}
        </div>
    </div>

    <div class="summary-section">
        <div class="summary-box">
            <div class="label">Total Pendapatan</div>
            <div class="value">Rp {{ number_format((float) ($summary['total_revenue'] ?? 0), 0, ',', '.') }}</div>
        </div>
        <div class="summary-box">
            <div class="label">Pembayaran Masuk</div>
            <div class="value">{{ number_format((int) ($summary['total_transactions'] ?? 0), 0, ',', '.') }}</div>
        </div>
        <div class="summary-box">
            <div class="label">Total Qty</div>
            <div class="value">{{ number_format((float) ($summary['total_qty'] ?? 0), 0, ',', '.') }}</div>
        </div>
        <div class="summary-box">
            <div class="label">Jumlah Produk</div>
            <div class="value">{{ number_format((int) ($summary['total_products'] ?? 0), 0, ',', '.') }}</div>
        </div>
    </div>

    @if($tab === 'produk')
        <div class="section-title">Pendapatan Per Produk</div>
        <table class="detail-table">
            <thead>
                <tr>
                    <th>No</th>
                    <th>Produk</th>
                    <th class="text-center">Order</th>
                    <th class="text-center">Qty</th>
                    <th class="text-right">Pendapatan</th>
                    <th class="text-right">%</th>
                </tr>
            </thead>
            <tbody>
                @forelse($byProduct as $index => $product)
                    @php
                        $percentage = ((float) ($summary['total_revenue'] ?? 0)) > 0
                            ? (((float) $product->total_revenue / (float) $summary['total_revenue']) * 100)
                            : 0;
                    @endphp
                    <tr>
                        <td class="text-center">{{ $index + 1 }}</td>
                        <td>{{ $product->name }}</td>
                        <td class="text-center">{{ number_format($product->total_orders) }}</td>
                        <td class="text-center">{{ number_format($product->total_qty) }}</td>
                        <td class="text-right">Rp {{ number_format((float) $product->total_revenue, 0, ',', '.') }}</td>
                        <td class="text-right">{{ number_format($percentage, 1) }}%</td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="6" class="text-center">Tidak ada data</td>
                    </tr>
                @endforelse
            </tbody>
            @if(isset($byProduct) && method_exists($byProduct, 'count') && $byProduct->count() > 0)
                <tfoot>
                    <tr class="grand-total-row">
                        <td colspan="4" class="text-right">TOTAL</td>
                        <td class="text-right">Rp {{ number_format((float) ($summary['total_revenue'] ?? 0), 0, ',', '.') }}</td>
                        <td class="text-right">100%</td>
                    </tr>
                </tfoot>
            @endif
        </table>
    @else
        <div class="section-title">Detail Per Pembayaran</div>
        <table class="detail-table">
            <thead>
                <tr>
                    <th>No</th>
                    <th>Tanggal Bayar</th>
                    <th>Invoice</th>
                    <th>Pelanggan</th>
                    <th>Status</th>
                    <th>Jenis</th>
                    <th>Rekening</th>
                    <th>Kasir</th>
                    <th class="text-right">Nominal Masuk</th>
                </tr>
            </thead>
            <tbody>
                @forelse($byTransaction as $index => $row)
                    <tr>
                        <td class="text-center">{{ $index + 1 }}</td>
                        <td>{{ optional($row->payment_date)->format('d/m/Y H:i') ?? '-' }}</td>
                        <td>{{ $row->invoice_number ?? '-' }}</td>
                        <td>
                            {{ $row->customer_name ?? '-' }}
                            @if(!empty($row->notes))
                                <br><small><i>{{ $row->notes }}</i></small>
                            @endif
                        </td>
                        <td>{{ $row->status_label ?? '-' }}</td>
                        <td>{{ $row->payment_method_label ?? '-' }}</td>
                        <td>{{ $row->payment_account_label ?? '-' }}</td>
                        <td>{{ $row->cashier_name ?? '-' }}</td>
                        <td class="text-right">Rp {{ number_format((float) ($row->amount ?? 0), 0, ',', '.') }}</td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="9" class="text-center">Tidak ada data</td>
                    </tr>
                @endforelse
            </tbody>
            @if(isset($byTransaction) && method_exists($byTransaction, 'count') && $byTransaction->count() > 0)
                <tfoot>
                    <tr class="grand-total-row">
                        <td colspan="8" class="text-right">
                            TOTAL {{ number_format((int) ($summary['total_transactions'] ?? 0), 0, ',', '.') }} PEMBAYARAN MASUK
                        </td>
                        <td class="text-right">Rp {{ number_format((float) ($summary['total_revenue'] ?? 0), 0, ',', '.') }}</td>
                    </tr>
                </tfoot>
            @endif
        </table>
    @endif

    <div class="footer">
        <div>Dicetak pada: {{ now()->format('d M Y H:i:s') }}</div>
        <div>Halaman 1 dari 1</div>
    </div>
</body>
</html>