<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Laporan Pendapatan Bersih</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-between;
            margin-bottom: 20px;
            padding: 15px;
            background: #f9f9f9;
            border-radius: 5px;
        }
        .summary-box {
            text-align: center;
            flex: 1;
        }
        .summary-box .label {
            font-size: 10px;
            color: #666;
            text-transform: uppercase;
            margin-bottom: 5px;
        }
        .summary-box .value {
            font-size: 14px;
            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;
        }
        .detail-table th {
            background: #f5f5f5;
            font-weight: bold;
            text-transform: uppercase;
        }
        .text-right {
            text-align: right;
        }
        .text-center {
            text-align: center;
        }
        .net-income-row {
            background: #333;
            color: white;
            font-weight: bold;
            font-size: 14px; /* Larger font for Net Income */
            padding: 10px;
        }
        .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()">
    <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>LAPORAN PENDAPATAN BERSIH</h2>
        <div class="period">
            Periode: {{ \Carbon\Carbon::parse($startDate)->format('d M Y') }} - {{ \Carbon\Carbon::parse($endDate)->format('d M Y') }}
        </div>
    </div>

    <div class="summary-section">
        <div class="summary-box">
            <div class="label">Total Pendapatan</div>
            <div class="value" style="color: #10B981;">Rp {{ number_format($summary['revenue'], 0, ',', '.') }}</div>
        </div>
        <div class="summary-box">
            <div class="label">Total HPP (Modal)</div>
            <div class="value" style="color: #F59E0B;">Rp {{ number_format($summary['cogs'], 0, ',', '.') }}</div>
        </div>
        <div class="summary-box">
            <div class="label">Total Biaya Klik</div>
            <div class="value" style="color: #F59E0B;">Rp {{ number_format($summary['click_cost'] ?? 0, 0, ',', '.') }}</div>
        </div>
        <div class="summary-box">
            <div class="label">Total Pengeluaran</div>
            <div class="value" style="color: #EF4444;">Rp {{ number_format($summary['expenses'], 0, ',', '.') }}</div>
        </div>
        <div class="summary-box" style="border-left: 1px solid #ddd;">
            <div class="label">Laba Bersih</div>
            <div class="value" style="color: #6366F1; font-size: 16px;">Rp {{ number_format($summary['net_income'], 0, ',', '.') }}</div>
        </div>
    </div>

    <div class="section-title">Rincian HPP (Modal Bahan)</div>
    <table class="detail-table">
        <thead>
            <tr>
                <th>Nama Bahan</th>
                <th class="text-right">Total Pemakaian</th>
                <th class="text-right">Harga Rata-rata</th>
                <th class="text-right">Total Biaya</th>
            </tr>
        </thead>
        <tbody>
            @forelse($cogsDetails as $detail)
            <tr>
                <td>{{ $detail['name'] }}</td>
                <td class="text-right">{{ number_format($detail['total_usage'], 2, ',', '.') }}</td>
                <td class="text-right">Rp {{ number_format($detail['avg_price'], 2, ',', '.') }}</td>
                <td class="text-right">Rp {{ number_format($detail['total_cost'], 0, ',', '.') }}</td>
            </tr>
            @empty
            <tr>
                <td colspan="4" class="text-center">Tidak ada penggunaan bahan pada periode ini</td>
            </tr>
            @endforelse
        </tbody>
        <tfoot>
            <tr style="font-weight: bold; background: #f0f0f0;">
                <td colspan="3" class="text-right">TOTAL HPP</td>
                <td class="text-right">Rp {{ number_format($summary['cogs'], 0, ',', '.') }}</td>
            </tr>
        </tfoot>
    </table>

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