<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Faktur Penjualan - {{ $sale->invoice_number }}</title>
    <style>
        body { font-family: Helvetica, Arial, sans-serif; font-size: 12px; padding: 20px; }
        .header { text-align: center; border-bottom: 2px solid #333; padding-bottom: 10px; margin-bottom: 20px; }
        .header h1 { margin: 0; font-size: 24px; }
        .header p { margin: 2px; }
        
        .meta-table { width: 100%; margin-bottom: 20px; }
        .meta-table td { vertical-align: top; padding: 4px; }
        
        .items-table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
        .items-table th, .items-table td { border: 1px solid #333; padding: 8px; }
        .items-table th { background-color: #eee; text-align: center; }
        .text-right { text-align: right; }
        .text-center { text-align: center; }
        
        .totals-table { width: 40%; float: right; border-collapse: collapse; }
        .totals-table td { padding: 5px; font-weight: bold; }
        
        .signatures { width: 100%; margin-top: 100px; clear: both; text-align: center; }
        
        @media print {
            @page { size: A4; margin: 10mm; }
            .no-print { display: none; }
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>FAKTUR PENJUALAN</h1>
        <p><strong>{{ $sale->branch->name }}</strong></p>
        <p>{{ $sale->branch->address }} | Telp: {{ $sale->branch->phone }}</p>
        @if($sale->branch->npwp) <p>NPWP: {{ $sale->branch->npwp }}</p> @endif
    </div>

    <table class="meta-table">
        <tr>
            <td width="15%">No. Faktur</td>
            <td width="35%">: <strong>{{ $sale->invoice_number }}</strong></td>
            <td width="15%">Pelanggan</td>
            <td width="35%">: <strong>{{ $sale->customer_name }}</strong></td>
        </tr>
        <tr>
            <td>Tanggal</td>
            <td>: {{ $sale->created_at->format('d/m/Y H:i') }}</td>
            <td>Alamat</td>
            <td>: {{ $sale->shipping_address ?? ($sale->customer->address ?? '-') }}</td>
        </tr>
        @if($sale->customer && $sale->customer->is_pkp)
        <tr>
            <td><strong>Metode Bayar</strong></td>
            <td>: <strong>{{ strtoupper($sale->payment_method ?? 'TUNAI') }}</strong></td>
            <td>NPWP Pembeli</td>
            <td>: {{ $sale->customer->npwp ?? '-' }}</td>
        </tr>
        @endif
    </table>

    <table class="items-table">
        <thead>
            <tr>
                <th width="5%">No</th>
                <th>Nama Barang</th>
                <th width="10%">Qty</th>
                <th width="10%">Satuan</th>
                <th width="20%">Harga Satuan</th>
                <th width="20%">Total</th>
            </tr>
        </thead>
        <tbody>
            @foreach($sale->items as $index => $item)
            <tr>
                <td class="text-center">{{ $index + 1 }}</td>
                <td>{{ $item->product_name }}</td>
                <td class="text-center">{{ (float)$item->quantity }}</td>
                <td class="text-center">{{ $item->unit }}</td>
                <td class="text-right">Rp {{ number_format($item->unit_price, 0, ',', '.') }}</td>
                <td class="text-right">Rp {{ number_format($item->total_price, 0, ',', '.') }}</td>
            </tr>
            @endforeach
        </tbody>
    </table>

    <table class="totals-table">
        <tr>
            <td>Subtotal</td>
            <td class="text-right">Rp {{ number_format($sale->subtotal, 0, ',', '.') }}</td>
        </tr>
        @if($sale->tax_amount > 0)
        <tr>
            <td>PPN (11%)</td>
            <td class="text-right">Rp {{ number_format($sale->tax_amount, 0, ',', '.') }}</td>
        </tr>
        @endif
        <tr>
            <td style="font-size: 16px; border-top: 2px solid #000;">TOTAL BAYAR</td>
            <td class="text-right" style="font-size: 16px; border-top: 2px solid #000;">Rp {{ number_format($sale->grand_total, 0, ',', '.') }}</td>
        </tr>
    </table>
        
    <table class="signatures">
        <tr>
            <td width="33%">
                Penerima<br><br><br><br>
                (.......................)
            </td>
            <td width="33%"></td>
            <td width="33%">
                Hormat Kami<br><br><br><br>
                ( {{ $sale->user->name }} )
            </td>
        </tr>
    </table>

    <div class="no-print" style="margin-top: 20px; text-align: center;">
        <button onclick="window.print()" style="padding: 10px 20px; background: #000; color: #fff; border: none; cursor: pointer;">CETAK FAKTUR (A4)</button>
    </div>
</body>
</html>