@extends('layouts.app')

@section('title', 'Invoice ' . $invoice->invoice_number)

@section('content')
    <x-page-head title="{{ $invoice->invoice_number }}" subtitle="{{ $invoice->student?->full_name }}">
        <x-slot:actions>
            <a href="{{ route('finance.invoices.pdf', $invoice) }}" class="btn btn-outline" target="_blank">Download PDF</a>
            <a href="{{ route('communication.emails.invoice.compose', $invoice) }}" class="btn btn-outline">Send Email</a>
            <a href="{{ route('finance.receipts.create') }}?invoice_id={{ $invoice->id }}" class="btn btn-primary">Record Payment</a>
        </x-slot:actions>
    </x-page-head>

    <div style="display:grid; grid-template-columns: 2fr 1fr; gap:20px;">
        <x-panel title="Invoice Details">
            <dl style="display:grid; grid-template-columns: 140px 1fr; gap:8px 16px;">
                <dt>Student</dt><dd>{{ $invoice->student?->full_name }}</dd>
                <dt>Issue Date</dt><dd>{{ optional($invoice->issue_date)->format('d M Y') }}</dd>
                <dt>Due Date</dt><dd>{{ optional($invoice->due_date)->format('d M Y') ?? '—' }}</dd>
                <dt>Status</dt><dd><x-badge variant="blue">{{ $invoice->status }}</x-badge></dd>
                <dt>Email Status</dt><dd><x-badge variant="{{ match($invoice->email_status) { 'sent', 'resent' => 'green', 'failed' => 'red', default => 'gray' } }}">{{ str_replace('_', ' ', ucfirst($invoice->email_status ?? 'not_sent')) }}</x-badge></dd>
                <dt>Subtotal</dt><dd>{{ number_format($invoice->subtotal, 2) }}</dd>
                <dt>Tax</dt><dd>{{ number_format($invoice->tax_amount, 2) }}</dd>
                <dt>Total</dt><dd><strong>{{ number_format($invoice->total_amount, 2) }}</strong></dd>
                <dt>Amount Paid</dt><dd>{{ number_format($invoice->amount_paid, 2) }}</dd>
                <dt>Balance Due</dt><dd><strong>{{ number_format($invoice->balance_due, 2) }}</strong></dd>
            </dl>

            @if ($invoice->notes)
                <p style="margin-top:16px; color:var(--muted);">{{ $invoice->notes }}</p>
            @endif

            <h4 style="margin:20px 0 12px;">Line Items</h4>
            <div class="table-wrap">
                <table>
                    <thead>
                        <tr><th>Description</th><th>Qty</th><th>Unit Price</th><th>Account</th><th>Total</th></tr>
                    </thead>
                    <tbody>
                        @foreach ($invoice->lines as $line)
                            <tr>
                                <td>{{ $line->description }}</td>
                                <td>{{ $line->quantity }}</td>
                                <td>{{ number_format($line->unit_price, 2) }}</td>
                                <td>{{ $line->account?->code ?? 'Default' }}</td>
                                <td>{{ number_format($line->line_total, 2) }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </x-panel>

        <x-panel title="Journal Entry">
            @if ($journalEntry)
                <dl style="display:grid; grid-template-columns: 100px 1fr; gap:8px;">
                    <dt>Reference</dt><dd>{{ $journalEntry->reference_number }}</dd>
                    <dt>Date</dt><dd>{{ optional($journalEntry->date)->format('d M Y') }}</dd>
                    <dt>Description</dt><dd>{{ $journalEntry->description }}</dd>
                </dl>
                <div class="table-wrap" style="margin-top:12px;">
                    <table>
                        <thead><tr><th>Account</th><th>Debit</th><th>Credit</th></tr></thead>
                        <tbody>
                            @foreach ($journalEntry->lines as $line)
                                <tr>
                                    <td>{{ $line->account->code }} — {{ $line->account->name }}</td>
                                    <td>{{ number_format($line->debit_amount, 2) }}</td>
                                    <td>{{ number_format($line->credit_amount, 2) }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            @else
                <x-empty-state title="No journal entry" message="Journal entry not found for this invoice." />
            @endif
        </x-panel>
    </div>

    @if ($emailHistory->isNotEmpty())
        <x-panel title="Email History" style="margin-top:20px;">
            <div class="table-wrap">
                <table>
                    <thead>
                        <tr><th>Date</th><th>To</th><th>Subject</th><th>Status</th><th>Sent By</th><th></th></tr>
                    </thead>
                    <tbody>
                        @foreach ($emailHistory as $email)
                            <tr>
                                <td>{{ optional($email->sent_at ?? $email->created_at)->format('d M Y H:i') }}</td>
                                <td>{{ $email->to }}</td>
                                <td>{{ Str::limit($email->subject, 40) }}</td>
                                <td><x-badge :variant="$email->status->badgeVariant()">{{ $email->status->label() }}</x-badge></td>
                                <td>{{ $email->user?->name ?? 'System' }}</td>
                                <td><a href="{{ route('communication.emails.show', $email) }}" class="btn btn-ghost btn-sm">View</a></td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </x-panel>
    @endif

    <div style="margin-top:16px;">
        <a href="{{ route('finance.invoices.index') }}" class="btn btn-outline">Back to Invoices</a>
    </div>
@endsection
