<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Laporan Klik</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;
        }
        .summary-box {
            text-align: center;
        }
        .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;
        }
        .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()">
    <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 KLIK</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 Order</div>
            <div class="value">{{ number_format($summary['total_orders']) }}</div>
        </div>
        <div class="summary-box">
            <div class="label">Total Qty</div>
            <div class="value">{{ number_format($summary['total_qty']) }}</div>
        </div>
        <div class="summary-box">
            <div class="label">Total Klik</div>
            <div class="value">{{ number_format($summary['total_clicks']) }}</div>
        </div>
        <div class="summary-box">
            <div class="label">Total Biaya</div>
            <div class="value">Rp {{ number_format($summary['total_cost'] ?? 0, 0, ',', '.') }}</div>
        </div>
    </div>
    
    @if(count($machineStats) > 0)
    <div class="section-title">Rincian per Mesin</div>
    <table class="detail-table" style="width: 80%; margin-bottom: 20px;">
        <thead>
            <tr>
                <th>Mesin</th>
                <th class="text-right">Total Klik</th>
                <th class="text-right">Biaya/Klik</th>
                <th class="text-right">Total Biaya</th>
            </tr>
        </thead>
        <tbody>
            @foreach($machineStats as $stat)
            <tr>
                <td>{{ $stat['name'] }}</td>
                <td class="text-right">{{ number_format($stat['total_clicks']) }}</td>
                <td class="text-right">Rp {{ number_format($stat['cost_per_click'] ?? 0, 0, ',', '.') }}</td>
                <td class="text-right">Rp {{ number_format($stat['total_cost'] ?? 0, 0, ',', '.') }}</td>
            </tr>
            @endforeach
        </tbody>
    </table>
    @endif

    <div class="section-title">Detail Penjualan per Produk & Bahan</div>
    <table class="detail-table">
        <thead>
            <tr>
                <th>No</th>
                <th>Produk</th>
                <th>Bahan</th>
                <th>Mesin</th>
                <th class="text-center">Order</th>
                <th class="text-center">Qty</th>
                <th class="text-right">Total Klik</th>
            </tr>
        </thead>
        <tbody>
            @forelse($reportData as $index => $row)
            <tr>
                <td class="text-center">{{ $index + 1 }}</td>
                <td>{{ $row->product_name ?: '-' }}</td>
                <td>{{ $row->material_name ?: '-' }}</td>
                <td>{{ $materialMachines[$row->material_id] ?? '-' }}</td>
                <td class="text-center">{{ number_format($row->total_orders) }}</td>
                <td class="text-center">{{ number_format($row->total_qty) }}</td>
                <td class="text-right">{{ number_format($row->total_clicks) }}</td>
            </tr>
            @empty
            <tr>
                <td colspan="7" class="text-center">Tidak ada data</td>
            </tr>
            @endforelse
        </tbody>
        @if($reportData->count() > 0)
        <tfoot>
            <tr class="grand-total-row">
                <td colspan="4" class="text-right">TOTAL</td>
                <td class="text-center">{{ number_format($summary['total_orders']) }}</td>
                <td class="text-center">{{ number_format($summary['total_qty']) }}</td>
                <td class="text-right">{{ number_format($summary['total_clicks']) }}</td>
            </tr>
        </tfoot>
        @endif
    </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>
