<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Laporan Piutang</title>
    <style>
        @media print {
            @page {
                size: A4 portrait;
                margin: 15mm;
            }
            .no-print {
                display: none !important;
            }
        }
        * {
            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;
        }
        .summary-box .value.danger {
            color: #dc2626;
        }
        .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: #dc2626;
            color: white;
            font-weight: bold;
        }
        .age-badge {
            display: inline-block;
            padding: 2px 6px;
            border-radius: 10px;
            font-size: 8px;
            font-weight: bold;
        }
        .age-danger {
            background: #fee2e2;
            color: #dc2626;
        }
        .age-warning {
            background: #fef3c7;
            color: #d97706;
        }
        .age-normal {
            background: #f3f4f6;
            color: #374151;
        }
        .footer {
            margin-top: 30px;
            padding-top: 15px;
            border-top: 1px solid #ddd;
            display: flex;
            justify-content: space-between;
            font-size: 9px;
            color: #666;
        }
        .print-btn {
            background: #1a1a1a;
            color: white;
            padding: 10px 30px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            margin-top: 20px;
        }
        .print-btn:hover {
            background: #333;
        }
        .no-print {
            text-align: center;
        }
        @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>
    <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 PIUTANG</h2>
        <div class="period">
            @if(isset($month))
                Periode: {{ \Carbon\Carbon::createFromFormat('Y-m', $month)->translatedFormat('F Y') }}
            @else
                Semua Piutang (s/d {{ now()->format('d M Y') }})
            @endif
        </div>
    </div>

    <div class="summary-section">
        <div class="summary-box">
            <div class="label">Total Order Belum Lunas</div>
            <div class="value">{{ $summary['total_orders'] }}</div>
        </div>
        <div class="summary-box">
            <div class="label">Total Piutang</div>
            <div class="value danger">Rp {{ number_format($summary['total_receivables'], 0, ',', '.') }}</div>
        </div>
    </div>

    <table class="detail-table">
        <thead>
            <tr>
                <th>No</th>
                <th>Tanggal</th>
                <th>Invoice</th>
                <th>Pelanggan</th>
                <th class="text-right">Total</th>
                <th class="text-right">Dibayar</th>
                <th class="text-right">Sisa</th>
                <th class="text-center">Umur</th>
            </tr>
        </thead>
        <tbody>
            @forelse($orders as $index => $order)
            @php
                $age = (int) floor($order->created_at->diffInDays(now()));
                $ageClass = $age > 30 ? 'age-danger' : ($age > 7 ? 'age-warning' : 'age-normal');
                $totalPaid = $order->getTotalPaid();
                $remaining = $order->getRemainingBalance();
            @endphp
            <tr>
                <td class="text-center">{{ $index + 1 }}</td>
                <td>{{ $order->created_at->format('d/m/Y') }}</td>
                <td>{{ $order->invoice_number }}@if($order->isPartiallyPaid()) <small style="color:#3b82f6">(Cicilan)</small>@endif</td>
                <td>{{ $order->customer_name }}</td>
                <td class="text-right">Rp {{ number_format($order->total_price, 0, ',', '.') }}</td>
                <td class="text-right" style="color:#16a34a">{{ $totalPaid > 0 ? 'Rp ' . number_format($totalPaid, 0, ',', '.') : '-' }}</td>
                <td class="text-right" style="color:#dc2626; font-weight:bold">Rp {{ number_format($remaining, 0, ',', '.') }}</td>
                <td class="text-center">
                    <span class="age-badge {{ $ageClass }}">{{ $age }} hari</span>
                </td>
            </tr>
            @empty
            <tr>
                <td colspan="8" class="text-center">Tidak ada piutang</td>
            </tr>
            @endforelse
        </tbody>
        @if($orders->count() > 0)
        <tfoot>
            <tr class="grand-total-row">
                <td colspan="6" class="text-right">TOTAL PIUTANG</td>
                <td class="text-right">Rp {{ number_format($summary['total_receivables'], 0, ',', '.') }}</td>
                <td></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>

    <div class="no-print">
        <button class="print-btn" onclick="window.print()">🖨️ Cetak</button>
    </div>
</body>
</html>
