<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Laporan Hutang Pembelian</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.success {
            color: #16a34a;
        }
        .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;
        }
        .status-badge {
            display: inline-block;
            padding: 2px 6px;
            border-radius: 10px;
            font-size: 8px;
            font-weight: bold;
        }
        .status-unpaid {
            background: #fee2e2;
            color: #dc2626;
        }
        .status-partial {
            background: #fef3c7;
            color: #d97706;
        }
        .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 HUTANG PEMBELIAN</h2>
        <div class="period">
            @if(isset($month))
                Periode: {{ \Carbon\Carbon::createFromFormat('Y-m', $month)->translatedFormat('F Y') }}
            @else
                Semua Hutang (s/d {{ now()->format('d M Y') }})
            @endif
        </div>
    </div>

    <div class="summary-section">
        <div class="summary-box">
            <div class="label">Total Pembelian</div>
            <div class="value">{{ $summary['total_purchases'] }}</div>
        </div>
        <div class="summary-box">
            <div class="label">Total Tagihan</div>
            <div class="value">Rp {{ number_format($summary['total_amount'], 0, ',', '.') }}</div>
        </div>
        <div class="summary-box">
            <div class="label">Sudah Dibayar</div>
            <div class="value success">Rp {{ number_format($summary['total_paid'], 0, ',', '.') }}</div>
        </div>
        <div class="summary-box">
            <div class="label">Total Hutang</div>
            <div class="value danger">Rp {{ number_format($summary['total_debt'], 0, ',', '.') }}</div>
        </div>
    </div>

    <table class="detail-table">
        <thead>
            <tr>
                <th>No</th>
                <th>Tanggal</th>
                <th>Bahan</th>
                <th>Supplier</th>
                <th class="text-right">Total</th>
                <th class="text-right">Dibayar</th>
                <th class="text-right">Sisa</th>
                <th class="text-center">Status</th>
            </tr>
        </thead>
        <tbody>
            @forelse($purchases as $index => $purchase)
            <tr>
                <td class="text-center">{{ $index + 1 }}</td>
                <td>{{ $purchase->purchase_date->format('d/m/Y') }}</td>
                <td>{{ $purchase->material->name ?? '-' }}</td>
                <td>{{ $purchase->supplierRelation->name ?? '-' }}</td>
                <td class="text-right">Rp {{ number_format($purchase->total_price, 0, ',', '.') }}</td>
                <td class="text-right" style="color:#16a34a">{{ $purchase->paid_amount > 0 ? 'Rp ' . number_format($purchase->paid_amount, 0, ',', '.') : '-' }}</td>
                <td class="text-right" style="color:#dc2626; font-weight:bold">Rp {{ number_format($purchase->remaining, 0, ',', '.') }}</td>
                <td class="text-center">
                    @if($purchase->payment_status === 'partial')
                        <span class="status-badge status-partial">Sebagian</span>
                    @else
                        <span class="status-badge status-unpaid">Belum Bayar</span>
                    @endif
                </td>
            </tr>
            @empty
            <tr>
                <td colspan="8" class="text-center">Tidak ada hutang pembelian</td>
            </tr>
            @endforelse
        </tbody>
        @if($purchases->count() > 0)
        <tfoot>
            <tr class="grand-total-row">
                <td colspan="6" class="text-right">TOTAL HUTANG</td>
                <td class="text-right">Rp {{ number_format($summary['total_debt'], 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>
