<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>{{ $invoice->invoice_number ?: 'Draft Invoice' }}</title>
<style>
@page { size: A4 portrait; margin: 16mm; }
body { font-family: DejaVu Sans, sans-serif; font-size: 11px; color: #1f2937; }
* { box-sizing: border-box; }
.header-table, .info-table, .items-table, .summary-table, .footer-table { width: 100%; border-collapse: collapse; }
.title { font-size: 20px; font-weight: bold; color: #111827; }
.muted { color: #6b7280; }
.badge { display: inline-block; padding: 4px 10px; border-radius: 14px; font-size: 9px; font-weight: bold; }
.badge-issued { background: #dbeafe; color: #1d4ed8; }
.badge-review { background: #e0e7ff; color: #4338ca; }
.badge-draft { background: #fef3c7; color: #b45309; }
.badge-cancelled { background: #fee2e2; color: #b91c1c; }
.box { border: 1px solid #e5e7eb; border-radius: 8px; padding: 12px; }
.box-brand { border: 2px solid #1d4ed8; border-radius: 8px; padding: 12px; background: #f8faff; }
.section-title { font-size: 9px; text-transform: uppercase; letter-spacing: 1px; color: #9ca3af; font-weight: bold; margin-bottom: 8px; }
.items-table th, .items-table td { border: 1px solid #e5e7eb; padding: 8px; vertical-align: top; }
.items-table th { background: #1d4ed8; color: #fff; text-align: left; font-size: 10px; }
.items-table td { background: #fff; }
.items-table tr:nth-child(even) td { background: #f9fafb; }
.text-right { text-align: right; }
.signature { max-height: 70px; max-width: 180px; }
.hr-brand { border: none; border-top: 2px solid #1d4ed8; margin: 12px 0; }
.total-row td { font-weight: bold; background: #f1f5f9; }
.grand-total-row td { font-weight: bold; font-size: 13px; background: #1d4ed8; color: #fff; }
.footer-text { font-size: 9px; color: #6b7280; text-align: center; margin-top: 4px; }
.logo { max-height: 55px; max-width: 160px; }
</style>
</head>
<body>
@php
    $companyName = \App\Models\Setting::get('company_name');
    $companyAddress = \App\Models\Setting::get('company_address');
    $companyPhone = \App\Models\Setting::get('company_phone');
    $companyEmail = \App\Models\Setting::get('company_email');
    $signatureFile = $invoice->signature_path ? public_path('storage/' . $invoice->signature_path) : null;
    // SVG format — tidak butuh ekstensi imagick, aman di semua server
    $qrSvg = ($verifyUrl && \App\Models\Setting::get('invoice_show_qr', '1') === '1')
        ? 'data:image/svg+xml;base64,' . base64_encode(
            \SimpleSoftwareIO\QrCode\Facades\QrCode::format('svg')->size(140)->margin(1)->generate($verifyUrl)
          )
        : null;
    $invoicePrimaryColor   = \App\Models\Setting::get('invoice_primary_color', '#1d4ed8');
    $invoiceFooterText     = \App\Models\Setting::get('invoice_footer_text', 'Terima kasih atas kepercayaan Anda.');
    $invoicePaymentTerms   = \App\Models\Setting::get('invoice_payment_terms', '');
    $invoiceShowSignature  = \App\Models\Setting::get('invoice_show_signature', '1') === '1';
    $invoiceShowDueDate    = \App\Models\Setting::get('invoice_show_due_date', '1') === '1';
    $invoiceShowSource     = \App\Models\Setting::get('invoice_show_source', '1') === '1';
    $invoiceShowPublicNote = \App\Models\Setting::get('invoice_show_public_note', '1') === '1';
    $logoPathSetting       = \App\Models\Setting::get('invoice_logo_path', '');
    $logoAbsPath           = $logoPathSetting ? public_path('storage/' . $logoPathSetting) : null;
    $logoBase64            = ($logoAbsPath && file_exists($logoAbsPath))
        ? 'data:image/' . pathinfo($logoAbsPath, PATHINFO_EXTENSION) . ';base64,' . base64_encode(file_get_contents($logoAbsPath))
        : null;
@endphp

@php
    $brandColor = $invoicePrimaryColor;
@endphp
<style>
.badge-issued { background: {{ $brandColor }}22; color: {{ $brandColor }}; }
.items-table th { background: {{ $brandColor }}; }
.grand-total-row td { background: {{ $brandColor }}; }
.box-brand { border-color: {{ $brandColor }}; }
.hr-brand { border-top-color: {{ $brandColor }}; }
</style>

<table class="header-table">
    <tr>
        <td style="vertical-align:middle;">
            @if($logoBase64)
            <div style="margin-bottom:6px;"><img src="{{ $logoBase64 }}" class="logo" alt="Logo"></div>
            @endif
            <div class="title">{{ $companyName }}</div>
            <div class="muted" style="margin-top:4px;">{{ $companyAddress }}</div>
            <div class="muted">{{ $companyPhone }}{{ $companyEmail ? ' • ' . $companyEmail : '' }}</div>
        </td>
        <td class="text-right" style="vertical-align:middle;">
            <div style="font-size:26px; font-weight:900; color:{{ $brandColor }}; letter-spacing:2px;">INVOICE</div>
            @if(! empty($isCancellationInvoice))
            <div style="margin-top:4px; font-size:11px; font-weight:900; letter-spacing:1.5px; color:#b91c1c; border:2px solid #b91c1c; display:inline-block; padding:2px 8px; border-radius:4px; transform:rotate(-2deg);">PEMBATALAN</div>
            @endif
            <div style="margin-top:6px;"><span class="badge {{ $invoice->status === 'issued' ? 'badge-issued' : ($invoice->status === 'review' ? 'badge-review' : ($invoice->status === 'cancelled' ? 'badge-cancelled' : 'badge-draft')) }}">{{ strtoupper($invoice->status) }}</span></div>
            <div style="margin-top:6px; font-weight:bold; font-size:14px; color:{{ $brandColor }};">{{ $invoice->invoice_number ?: 'Draft #' . $invoice->id }}</div>
        </td>
    </tr>
</table>
<hr class="hr-brand">

<table class="info-table" style="margin-top:18px;">
    <tr>
        <td style="width:50%; padding-right:8px; vertical-align:top;">
            <div class="box">
                <div class="section-title">Pelanggan</div>
                <div style="font-weight:bold; font-size:13px;">{{ $invoice->customer->name }}</div>
                <div class="muted" style="margin-top:4px;">{{ $invoice->customer->whatsapp ?: '-' }}</div>
                <div class="muted">{{ $invoice->customer->email ?: '-' }}</div>
                <div class="muted" style="margin-top:6px;">{{ $invoice->customer->address ?: '-' }}</div>
            </div>
        </td>
        <td style="width:50%; padding-left:8px; vertical-align:top;">
            <div class="box">
                <div class="section-title">Informasi</div>
                <div>Tanggal Invoice: <strong>{{ optional($invoice->invoice_date)->format('d M Y') ?: '-' }}</strong></div>
                @if($invoiceShowDueDate)
                <div style="margin-top:4px;">Jatuh Tempo: <strong>{{ optional($invoice->due_date)->format('d M Y') ?: '-' }}</strong></div>
                @endif
                @if($invoiceShowSource)
                <div style="margin-top:4px;">Sumber: <strong>{{ ucfirst($invoice->source_type) }}</strong></div>
                @endif
            </div>
        </td>
    </tr>
</table>

<div style="margin-top:18px;" class="section-title">Item Tagihan</div>
<table class="items-table">
    <thead>
        <tr>
            <th>Deskripsi</th>
            <th style="width:60px;">Qty</th>
            <th style="width:110px;">Harga</th>
            <th style="width:100px;">Diskon</th>
            <th style="width:90px;">Pajak</th>
            <th style="width:120px;">Total</th>
        </tr>
    </thead>
    <tbody>
        @foreach($invoice->items as $item)
        <tr>
            <td>
                <div style="font-weight:bold;">{{ $item->description }}</div>
                <div class="muted" style="font-size:10px; margin-top:2px;">{{ ucfirst(str_replace('_', ' ', $item->item_type)) }}</div>
            </td>
            <td>{{ rtrim(rtrim(number_format((float) $item->qty, 2, '.', ''), '0'), '.') }}</td>
            <td>Rp {{ number_format((float) $item->unit_price, 0, ',', '.') }}</td>
            <td>Rp {{ number_format((float) $item->discount_amount, 0, ',', '.') }}</td>
            <td>Rp {{ number_format((float) $item->tax_amount, 0, ',', '.') }}</td>
            <td>Rp {{ number_format((float) $item->line_total, 0, ',', '.') }}</td>
        </tr>
        @endforeach
    </tbody>
</table>

<table class="summary-table" style="margin-top:18px;">
    <tr>
        @if($invoiceShowPublicNote)
        <td style="width:55%; vertical-align:top; padding-right:10px;">
            <div class="box" style="min-height:130px;">
                <div class="section-title">Catatan</div>
                <div>{{ $invoice->public_note ?: 'Tidak ada catatan invoice.' }}</div>
            </div>
        </td>
        <td style="width:45%; vertical-align:top; padding-left:10px;">
        @else
        <td style="width:100%; vertical-align:top;">
        @endif
            <div class="box">
                <table class="summary-table">
                    <tr><td>Subtotal</td><td class="text-right">Rp {{ number_format((float) $invoice->subtotal, 0, ',', '.') }}</td></tr>
                    <tr><td>Diskon</td><td class="text-right">- Rp {{ number_format((float) $invoice->discount_total, 0, ',', '.') }}</td></tr>
                    <tr><td>Pajak</td><td class="text-right">Rp {{ number_format((float) $invoice->tax_total, 0, ',', '.') }}</td></tr>
                    <tr class="total-row"><td><strong>Grand Total</strong></td><td class="text-right"><strong>Rp {{ number_format((float) $invoice->grand_total, 0, ',', '.') }}</strong></td></tr>
                    <tr><td>Sudah Dibayar</td><td class="text-right">Rp {{ number_format((float) $invoice->amount_paid, 0, ',', '.') }}</td></tr>
                    <tr class="grand-total-row"><td>Sisa Tagihan</td><td class="text-right">Rp {{ number_format((float) $invoice->balance_due, 0, ',', '.') }}</td></tr>
                </table>
            </div>
        </td>
    </tr>
</table>

<hr class="hr-brand" style="margin-top:18px;">

@if($invoicePaymentTerms)
<div style="background:#f8faff; border:1px solid #e5e7eb; border-radius:6px; padding:8px 12px; margin-bottom:14px;">
    <div class="section-title">Ketentuan Pembayaran</div>
    <div style="font-size:9.5px; color:#374151;">{{ $invoicePaymentTerms }}</div>
</div>
@endif

<table class="footer-table">
    <tr>
        <td style="width:50%; vertical-align:top;">
            @if($invoiceShowSignature)
            <div class="section-title">Tanda Tangan Penerbit</div>
            @if($signatureFile && file_exists($signatureFile))
            <img src="{{ $signatureFile }}" class="signature" alt="Signature">
            @else
            <div style="border-bottom:1px solid #111; width:160px; height:50px; margin-bottom:4px;"></div>
            @endif
            <div style="margin-top:4px; font-weight:bold; font-size:10px;">{{ $invoice->issuer?->name ?: auth()->user()->name ?? '-' }}</div>
            <div style="font-size:9px; color:#6b7280;">{{ ucfirst($invoice->issuer?->role ?? '') }}</div>
            @endif
        </td>
        <td style="width:50%; vertical-align:top; text-align:right;">
            @if($qrSvg)
            <div class="section-title">Verifikasi Keaslian</div>
            <img src="{{ $qrSvg }}" alt="QR Verifikasi" style="width:110px; height:110px;">
            <div class="muted" style="font-size:8px; margin-top:4px;">{{ $invoice->verification_code }}</div>
            @endif
        </td>
    </tr>
</table>

@if($invoiceFooterText)
<div style="border-top:1px solid #e5e7eb; margin-top:14px; padding-top:8px; text-align:center;">
    <div class="footer-text">{{ $invoiceFooterText }}</div>
</div>
@endif
</body>
</html>