<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Invoice {{ $invoice->number }}</title>
    <style>
        body {
            font-family: 'Helvetica', Arial, sans-serif;
            font-size: 12px;
            color: #333;
            margin: 40px;
        }
        .company-name {
            font-size: 24px;
            font-weight: bold;
            color: #7367F0;
        }
        .invoice-title {
            font-size: 28px;
            font-weight: bold;
            text-align: right;
            color: #7367F0;
        }
        .invoice-meta {
            text-align: right;
            margin-top: 8px;
        }
        .meta-label {
            color: #666;
        }
        .address-label {
            font-weight: bold;
            font-size: 11px;
            text-transform: uppercase;
            color: #666;
            margin-bottom: 5px;
        }
        table.items {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        table.items th {
            background-color: #7367F0;
            color: white;
            padding: 8px 12px;
            text-align: left;
            font-size: 11px;
            text-transform: uppercase;
        }
        table.items th.text-right {
            text-align: right;
        }
        table.items td {
            padding: 8px 12px;
            border-bottom: 1px solid #eee;
        }
        table.items tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        .totals {
            width: 300px;
            margin-left: auto;
        }
        .totals table {
            width: 100%;
        }
        .totals td {
            padding: 4px 8px;
        }
        .totals .total-row td {
            font-size: 16px;
            font-weight: bold;
            border-top: 2px solid #7367F0;
            padding-top: 8px;
        }
        .status-badge {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 4px;
            font-size: 11px;
            font-weight: bold;
            text-transform: uppercase;
            color: white;
        }
        .status-paid {
            background-color: #28c76f;
        }
        .status-pending, .status-sent {
            background-color: #ff9f43;
        }
        .status-overdue {
            background-color: #ea5455;
        }
        .status-draft {
            background-color: #82868b;
        }
        .status-void {
            background-color: #82868b;
        }
        .footer {
            margin-top: 40px;
            text-align: center;
            font-size: 10px;
            color: #999;
            border-top: 1px solid #eee;
            padding-top: 15px;
        }
    </style>
</head>
<body>
    {{-- Header --}}
    <table width="100%" style="margin-bottom: 30px;">
        <tr>
            <td width="50%" style="vertical-align: top;">
                <div class="company-name">EZSCALE</div>
                <div style="margin-top: 5px; color: #666;">Cloud Infrastructure Services</div>
            </td>
            <td width="50%" style="vertical-align: top; text-align: right;">
                <div class="invoice-title">INVOICE</div>
                <div class="invoice-meta">
                    <div><span class="meta-label">Invoice #:</span> {{ $invoice->number }}</div>
                    <div><span class="meta-label">Date:</span> {{ $invoice->created_at->format('M d, Y') }}</div>
                    <div><span class="meta-label">Due Date:</span> {{ $invoice->due_date ? $invoice->due_date->format('M d, Y') : 'N/A' }}</div>
                    <div style="margin-top: 5px;">
                        <span class="status-badge status-{{ strtolower($invoice->status) }}">{{ ucfirst($invoice->status) }}</span>
                    </div>
                </div>
            </td>
        </tr>
    </table>

    {{-- Bill To / From --}}
    <table width="100%" style="margin-bottom: 30px;">
        <tr>
            <td width="50%" style="vertical-align: top;">
                <div class="address-label">Bill To</div>
                <div><strong>{{ $invoice->user->name }}</strong></div>
                <div>{{ $invoice->user->email }}</div>
                @if($invoice->user->company)<div>{{ $invoice->user->company }}</div>@endif
                @if($invoice->user->phone)<div>{{ $invoice->user->phone }}</div>@endif
            </td>
            <td width="50%" style="vertical-align: top;">
                <div class="address-label">From</div>
                <div><strong>EZSCALE</strong></div>
                <div>support@ezscale.cloud</div>
            </td>
        </tr>
    </table>

    {{-- Line Items --}}
    <table class="items">
        <thead>
            <tr>
                <th>Description</th>
                <th class="text-right" style="text-align: center;">Qty</th>
                <th class="text-right" style="text-align: right;">Unit Price</th>
                <th class="text-right" style="text-align: right;">Total</th>
            </tr>
        </thead>
        <tbody>
            @forelse($invoice->items as $item)
                <tr>
                    <td>{{ $item->description }}</td>
                    <td style="text-align: center;">{{ $item->quantity }}</td>
                    <td style="text-align: right;">${{ number_format((float) $item->amount, 2) }}</td>
                    <td style="text-align: right;">${{ number_format((float) $item->amount * $item->quantity, 2) }}</td>
                </tr>
            @empty
                <tr>
                    <td>Service charges</td>
                    <td style="text-align: center;">1</td>
                    <td style="text-align: right;">${{ number_format((float) $invoice->total - (float) $invoice->tax, 2) }}</td>
                    <td style="text-align: right;">${{ number_format((float) $invoice->total - (float) $invoice->tax, 2) }}</td>
                </tr>
            @endforelse
        </tbody>
    </table>

    {{-- Totals --}}
    <div class="totals">
        <table>
            <tr>
                <td>Subtotal</td>
                <td style="text-align: right;">
                    @if($invoice->items->count() > 0)
                        ${{ number_format($invoice->items->sum(fn ($item) => (float) $item->amount * $item->quantity), 2) }}
                    @else
                        ${{ number_format((float) $invoice->total - (float) $invoice->tax, 2) }}
                    @endif
                </td>
            </tr>
            @if((float) $invoice->tax > 0)
                <tr>
                    <td>Tax</td>
                    <td style="text-align: right;">${{ number_format((float) $invoice->tax, 2) }}</td>
                </tr>
            @endif
            <tr class="total-row">
                <td>Total</td>
                <td style="text-align: right;">${{ number_format((float) $invoice->total, 2) }}</td>
            </tr>
            @if($invoice->status === 'paid' && $invoice->paid_at)
                <tr>
                    <td colspan="2" style="text-align: right; font-size: 10px; color: #28c76f; padding-top: 4px;">
                        Paid on {{ $invoice->paid_at->format('M d, Y') }}
                    </td>
                </tr>
            @endif
        </table>
    </div>

    {{-- Footer --}}
    <div class="footer">
        <p>Thank you for your business!</p>
        <p>EZSCALE &mdash; Cloud Infrastructure Services &mdash; support@ezscale.cloud</p>
    </div>
</body>
</html>
