@extends('layouts.app')

@section('title', 'Statement — ' . $student->full_name)

@section('content')
    <x-page-head title="{{ $student->full_name }}" subtitle="Fee statement & outstanding balance">
        <x-slot:actions>
            <a href="{{ route('finance.billing.create') }}?student_id={{ $student->id }}" class="btn btn-primary">Assign Fee</a>
        </x-slot:actions>
    </x-page-head>

    <div style="display:grid; grid-template-columns: repeat(3, 1fr); gap:16px; margin-bottom:20px;">
        <x-stat-card label="Total Invoiced" :value="number_format($total_invoiced, 2)" />
        <x-stat-card label="Total Paid" :value="number_format($total_paid, 2)" />
        <x-stat-card label="Outstanding" :value="number_format($total_outstanding, 2)" />
    </div>

    <x-panel title="Invoices">
        @if ($invoices->isEmpty())
            <x-empty-state title="No invoices" message="No invoices for this student." />
        @else
            <div class="table-wrap">
                <table>
                    <thead><tr><th>Invoice #</th><th>Date</th><th>Total</th><th>Paid</th><th>Balance</th><th>Status</th><th></th></tr></thead>
                    <tbody>
                        @foreach ($invoices as $invoice)
                            <tr>
                                <td>{{ $invoice->invoice_number }}</td>
                                <td>{{ optional($invoice->issue_date)->format('d M Y') }}</td>
                                <td>{{ number_format($invoice->total_amount, 2) }}</td>
                                <td>{{ number_format($invoice->amount_paid, 2) }}</td>
                                <td>{{ number_format($invoice->balance_due, 2) }}</td>
                                <td><x-badge variant="blue">{{ $invoice->status }}</x-badge></td>
                                <td><a href="{{ route('finance.invoices.show', $invoice) }}">View</a></td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @endif
    </x-panel>

    <x-panel title="Receipts" style="margin-top:20px;">
        @if ($receipts->isEmpty())
            <x-empty-state title="No receipts" message="No payments recorded." />
        @else
            <div class="table-wrap">
                <table>
                    <thead><tr><th>Receipt #</th><th>Date</th><th>Amount</th><th>Method</th></tr></thead>
                    <tbody>
                        @foreach ($receipts as $receipt)
                            <tr>
                                <td>{{ $receipt->receipt_number }}</td>
                                <td>{{ optional($receipt->payment_date)->format('d M Y') }}</td>
                                <td>{{ number_format($receipt->amount, 2) }}</td>
                                <td>{{ $receipt->payment_method ?? '—' }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @endif
    </x-panel>

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