<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Invoice {{ $invoice->invoice_number ?? 'INV-001' }}</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; color: #1f2937; line-height: 1.6; }
        .invoice-container { width: 100%; max-width: 899px; margin: 0 auto; padding: 5px; background: #fff; }
        .header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 40px; padding-bottom: 20px; border-bottom: 2px solid #e5e7eb; }
        .logo-section { flex: 1; }
        .logo { width: 160px; max-height: 60px; margin-bottom: 10px; }
        .company-name { font-size: 20px; font-weight: 700; color: #111827; margin-bottom: 5px; }
        .company-info { font-size: 14px; color: #6b7280; line-height: 1.8; }
        .invoice-header { text-align: right; }
        .invoice-title { font-size: 30px; font-weight: 500; color: rgba(19, 127, 236, 1); letter-spacing: 2px; margin-bottom: 10px; }
        .invoice-number { font-size: 18px; font-weight: 600; color: #374151; }
        .invoice-details-box { background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 8px; padding: 20px; margin-top: 20px; }
        .detail-row { display: flex; justify-content: space-between; margin-bottom: 8px; font-size: 13px; }
        .detail-label { color: #6b7280; }
        .detail-value { font-weight: 600; color: #111827; }
        .customer-section { margin: 30px 0; }
        .section-title { font-size: 13px; font-weight: 700; text-transform: uppercase; color: #137fec; letter-spacing: 1px; margin-bottom: 10px; }
        .customer-name { font-size: 18px; font-weight: 700; color: #111827; margin-bottom: 5px; }
        .customer-info { font-size: 14px; color: #6b7280; line-height: 1.8; }
        .items-table { width: 100%; border-collapse: collapse; margin: 30px 0; }
        .items-table thead { background: #137fec; color: #fff; }
        .items-table th { padding: 12px 15px; text-align: left; font-size: 14px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; }
        .items-table th.text-right { text-align: right !important; }
        .items-table td { padding: 15px; border-bottom: 1px solid #e5e7eb; font-size: 14px; }
        .items-table td.text-right { text-align: right; color: rgba(0, 0, 0, 1); }
        .items-table strong { color: rgba(0, 0, 0, 1); }
        .items-table tbody tr:hover { background: #f9fafb; }
        .text-right { text-align: right; }
        .totals-section { margin-top: 30px; display: flex; justify-content: flex-end; }
        .totals-box { width: 300px; }
        .total-row { display: flex; justify-content: space-between; padding: 10px 0; font-size: 14px; border-bottom: 1px solid #e5e7eb; }
        .total-row:last-child { border-bottom: none; }
        .total-label { color: #6b7280; font-size: 16px; }
        .total-value { font-weight: 600; color: #111827; }
        .grand-total { font-size: 18px; font-weight: 700; color: #137fec; padding-top: 15px; border-top: 2px solid #137fec; }
        .footer { margin-top: 50px; padding-top: 20px; border-top: 1px solid #e5e7eb; }
        .notes-section { margin-bottom: 20px; }
        .notes-title { font-size: 15px; font-weight: 600; color: #374151; margin-bottom: 8px; }
        .notes-content { font-size: 14px; color: #6b7280; line-height: 1.8; }
        .payment-instructions { background: #eff6ff; border-left: 4px solid #137fec; padding: 15px; border-radius: 4px; }
        .payment-title { font-size: 15px; font-weight: 600; color: #137fec; margin-bottom: 8px; }
        .payment-text { font-size: 13px; color: #1e40af; line-height: 1.6; }
        .payment-button { display: inline-block; background: #137fec; color: #fff; padding: 12px 24px; border-radius: 6px; text-decoration: none; font-weight: 600; font-size: 14px; margin-top: 12px; transition: background 0.2s; }
        .payment-button:hover { background: #0d5aa7; }
        .payment-instructions-details { margin-top: 15px; padding-top: 15px; border-top: 1px solid #dbeafe; }
        .watermark {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%) rotate(-45deg);
            font-size: 72px;
            color: rgba(255, 0, 0, 0.1);
            z-index: 1000;
            pointer-events: none;
            font-weight: bold;
        }
        @media (max-width: 640px) {
            .header { flex-direction: column; align-items: stretch; gap: 24px; }
            .invoice-header { text-align: left; }
            .items-table th, .items-table td { padding: 10px 8px; font-size: 12px; }
            .totals-section { justify-content: stretch; }
            .totals-box { width: 100%; max-width: 100%; }
        }
        @media print {
            * {
                -webkit-print-color-adjust: exact !important;
                print-color-adjust: exact !important;
            }
            .invoice-container { padding: 0; max-width: 100% !important; width: 100% !important; }
            body { background: #fff !important; }
        }
    </style>
</head>
<body style="background-color: #ffffff !important;">
    @if(isset($showWatermark) && $showWatermark)
        <div class="watermark">UNLICENSED</div>
    @endif
    <div class="invoice-container">
        <!-- Header -->
        <div class="header">
            <div class="logo-section">
                @php
                    $companySettings = \App\Models\CompanySetting::getSettings();
                @endphp
                @if($companySettings && $companySettings->company_logo && \Illuminate\Support\Facades\Storage::disk('public')->exists($companySettings->company_logo))
                    <img src="{{ \Illuminate\Support\Facades\Storage::url($companySettings->company_logo) }}" alt="Logo" class="logo">
                @endif
                <div class="company-name">{{ $companySettings->company_name ?? config('app.name') }}</div>
                <div class="company-info">
                    @if($companySettings->company_address){{ $companySettings->company_address }}<br>@endif
                    @if($companySettings->company_city || $companySettings->company_state || $companySettings->company_zip)
                        {{ trim(($companySettings->company_city ?? '') . ', ' . ($companySettings->company_state ?? '') . ' ' . ($companySettings->company_zip ?? ''), ', ') }}<br>
                    @endif
                    @if($companySettings->company_phone){{ $companySettings->company_phone }}<br>@endif
                    @if($companySettings->company_email){{ $companySettings->company_email }}@endif
                </div>
            </div>
            <div class="invoice-header">
                <div class="invoice-title">INVOICE</div>
                <div class="invoice-details-box">
                    <div class="detail-row">
                        <span class="detail-label">Invoice #</span>
                        <span class="detail-value">{{ $invoice->invoice_number ?? 'INV-001' }}</span>
                    </div>
                    <div class="detail-row">
                        <span class="detail-label">Issue Date</span>
                        <span class="detail-value">{{ ($invoice->issue_date ?? now())->format('M d, Y') }}</span>
                    </div>
                    <div class="detail-row">
                        <span class="detail-label">Due Date</span>
                        <span class="detail-value">{{ ($invoice->due_date ?? now()->addDays(30))->format('M d, Y') }}</span>
                    </div>
                    @if($invoice->status ?? null)
                    <div class="detail-row">
                        <span class="detail-label">Status</span>
                        <span class="detail-value">{{ ucfirst($invoice->status) }}</span>
                    </div>
                    @endif
                </div>
            </div>
        </div>

        <!-- Customer Section -->
        @if($invoice->customer ?? null)
        <div class="customer-section">
            <div class="section-title">Bill To</div>
            <div class="customer-name">{{ $invoice->customer->name }}</div>
            <div class="customer-info">
                @if($invoice->customer->email){{ $invoice->customer->email }}<br>@endif
                @if($invoice->customer->phone){{ $invoice->customer->phone }}<br>@endif
                @if($invoice->customer->address){{ $invoice->customer->address }}@endif
            </div>
        </div>
        @endif

        <!-- Items Table -->
        <table class="items-table">
            <thead>
                <tr>
                    <th>Description</th>
                    <th class="text-right">Quantity</th>
                    <th class="text-right">Unit Price</th>
                    <th class="text-right">Tax</th>
                    <th class="text-right">Total</th>
                </tr>
            </thead>
            <tbody>
                @forelse($invoice->items ?? [] as $item)
                <tr>
                    <td>
                        <strong>{{ $item->description }}</strong>
                        @if($item->notes)
                            <br><small style="color: #6b7280;">{{ $item->notes }}</small>
                        @endif
                    </td>
                    <td class="text-right">{{ number_format($item->quantity, 2) }}</td>
                    <td class="text-right">{{ $invoice->currency ?? 'USD' }} {{ number_format($item->unit_price, 2) }}</td>
                    <td class="text-right">{{ $item->tax_rate ?? 0 }}%</td>
                    <td class="text-right"><strong>{{ $invoice->currency ?? 'USD' }} {{ number_format(($item->quantity * $item->unit_price) * (1 + ($item->tax_rate ?? 0) / 100), 2) }}</strong></td>
                </tr>
                @empty
                <tr>
                    <td colspan="5" style="text-align: center; padding: 30px; color: #9ca3af;">No items</td>
                </tr>
                @endforelse
            </tbody>
        </table>

        <!-- Totals -->
        <div class="totals-section">
            <div class="totals-box">
                <div class="total-row">
                    <span class="total-label">Subtotal</span>
                    <span class="total-value">{{ $invoice->currency ?? 'USD' }} {{ number_format($invoice->subtotal ?? 0, 2) }}</span>
                </div>
                @if(($invoice->tax_amount ?? 0) > 0)
                <div class="total-row">
                    <span class="total-label">Tax</span>
                    <span class="total-value">{{ $invoice->currency ?? 'USD' }} {{ number_format($invoice->tax_amount ?? 0, 2) }}</span>
                </div>
                @endif
                @if(($invoice->discount_amount ?? 0) > 0)
                <div class="total-row">
                    <span class="total-label">Discount</span>
                    <span class="total-value">-{{ $invoice->currency ?? 'USD' }} {{ number_format($invoice->discount_amount ?? 0, 2) }}</span>
                </div>
                @endif
                <div class="total-row grand-total">
                    <span>Total Due</span>
                    <span>{{ $invoice->currency ?? 'USD' }} {{ number_format($invoice->total ?? 0, 2) }}</span>
                </div>
                @if(($invoice->paid_amount ?? 0) > 0)
                <div class="total-row">
                    <span class="total-label">Paid</span>
                    <span class="total-value">{{ $invoice->currency ?? 'USD' }} {{ number_format($invoice->paid_amount ?? 0, 2) }}</span>
                </div>
                <div class="total-row grand-total">
                    <span>Balance Due</span>
                    <span>{{ $invoice->currency ?? 'USD' }} {{ number_format($invoice->balance ?? 0, 2) }}</span>
                </div>
                @endif
            </div>
        </div>

        <!-- Footer -->
        <div class="footer">
            @if($invoice->notes ?? null)
            <div class="notes-section">
                <div class="notes-title">Notes</div>
                <div class="notes-content">{{ $invoice->notes }}</div>
            </div>
            @endif
            @if($invoice->terms ?? null)
            <div class="notes-section">
                <div class="notes-title">Terms & Conditions</div>
                <div class="notes-content">{{ $invoice->terms }}</div>
            </div>
            @endif
            <div class="payment-instructions">
                <div class="payment-title">Payment Instructions</div>
                <div class="payment-text">
                    @php
                        // Calculate days until due
                        $issueDate = isset($invoice) && is_object($invoice) && $invoice->issue_date 
                            ? \Carbon\Carbon::parse($invoice->issue_date) 
                            : (isset($invoiceData) && is_array($invoiceData) && isset($invoiceData['issue_date']) 
                                ? \Carbon\Carbon::parse($invoiceData['issue_date']) 
                                : now());
                        $dueDate = isset($invoice) && is_object($invoice) && $invoice->due_date 
                            ? \Carbon\Carbon::parse($invoice->due_date) 
                            : (isset($invoiceData) && is_array($invoiceData) && isset($invoiceData['due_date']) 
                                ? \Carbon\Carbon::parse($invoiceData['due_date']) 
                                : now()->addDays(30));
                        $daysUntilDue = max(0, $issueDate->diffInDays($dueDate));
                    @endphp
                    <p>Please make payment within <strong>{{ $daysUntilDue }}</strong> days of the invoice date.</p>
                    
                    @php
                        // Get payment method details
                        $paymentMethod = null;
                        if (isset($invoice) && is_object($invoice) && !empty($invoice->payment_method)) {
                            $paymentMethod = $invoice->payment_method;
                        } elseif (isset($invoiceData) && is_array($invoiceData) && !empty($invoiceData['payment_method'])) {
                            $paymentMethod = $invoiceData['payment_method'];
                        }
                        
                        $selectedBankAccount = null;
                        if ($paymentMethod && is_string($paymentMethod) && str_starts_with($paymentMethod, 'bank_')) {
                            $bankId = str_replace('bank_', '', $paymentMethod);
                            $selectedBankAccount = \App\Models\BankAccount::find($bankId);
                        }
                    @endphp
                    
                    <div class="payment-instructions-details">
                        @if($paymentMethod === 'online')
                            <p><strong>Payment Method:</strong> Online Payment</p>
                            <p>Payments can be made online via our payment gateway.</p>
                            @php
                                $paymentUrl = null;
                                if (isset($invoice) && is_object($invoice) && $invoice->id) {
                                    if ($invoice->public_token) {
                                        $paymentUrl = route('invoice.public', ['token' => $invoice->public_token]);
                                    } elseif (method_exists($invoice, 'getPublicUrl')) {
                                        $paymentUrl = $invoice->getPublicUrl();
                                    }
                                }
                            @endphp
                            @if($paymentUrl)
                                <a href="{{ $paymentUrl }}" class="payment-button" target="_blank">
                                    Pay Now
                                </a>
                            @else
                                <p style="margin-top: 12px; padding: 10px; background: #f3f4f6; border-radius: 6px; font-size: 13px; color: #6b7280; display: inline-block;">
                                    Payment link will be available after invoice is saved.
                                </p>
                            @endif
                        @elseif($selectedBankAccount)
                            <p><strong>Payment Method:</strong> Bank Transfer</p>
                            <p><strong>Bank:</strong> {{ $selectedBankAccount->bank_name }}</p>
                            @if($selectedBankAccount->account_name)
                                <p><strong>Account Name:</strong> {{ $selectedBankAccount->account_name }}</p>
                            @endif
                            @if($selectedBankAccount->account_number)
                                <p><strong>Account Number:</strong> {{ $selectedBankAccount->account_number }}</p>
                            @endif
                            @if($selectedBankAccount->swift_code)
                                <p><strong>SWIFT Code:</strong> {{ $selectedBankAccount->swift_code }}</p>
                            @endif
                            @if($selectedBankAccount->iban)
                                <p><strong>IBAN:</strong> {{ $selectedBankAccount->iban }}</p>
                            @endif
                            @if($selectedBankAccount->branch_name)
                                <p><strong>Branch:</strong> {{ $selectedBankAccount->branch_name }}</p>
                            @endif
                            @if($selectedBankAccount->currency)
                                <p><strong>Currency:</strong> {{ $selectedBankAccount->currency }}</p>
                            @endif
                        @else
                            <p>Payment can be made via bank transfer or other agreed methods.</p>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
