@extends('layouts.parent')
@section('title', 'Fee Statement')
@section('content')
    <x-page-head title="Fee Statement" subtitle="Invoices & payments" />
    <x-panel>
        @if ($students->isEmpty())
            <x-empty-state title="No children linked" message="Link your guardian account to view fee statements." />
        @else
            <form method="GET" style="margin-bottom:16px;">
                <select class="input" name="student_id" onchange="this.form.submit()">
                    @foreach ($students as $student)
                        <option value="{{ $student->id }}" @selected($studentId == $student->id)>{{ $student->full_name }}</option>
                    @endforeach
                </select>
            </form>
            @if ($statement)
                <div class="stat-grid">
                    <x-stat-card label="Total Invoiced" :value="number_format($statement['total_invoiced'], 2)" />
                    <x-stat-card label="Total Paid" :value="number_format($statement['total_paid'], 2)" color="teal" />
                    <x-stat-card label="Outstanding" :value="number_format($statement['total_outstanding'], 2)" color="violet" />
                </div>
                <h3 style="margin:16px 0 8px;">Invoices</h3>
                <div class="table-wrap"><table><thead><tr><th>Number</th><th>Date</th><th>Total</th><th>Paid</th><th>Balance</th><th>Status</th><th></th></tr></thead><tbody>
                    @forelse ($statement['invoices'] as $invoice)
                        <tr>
                            <td>{{ $invoice->invoice_number }}</td>
                            <td>{{ $invoice->issue_date?->format('M j, 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 :status="$invoice->status" /></td>
                            <td class="text-right">
                                @if ($invoice->balance_due > 0)
                                    <a href="{{ route('parent.fees.pay', $invoice) }}" class="btn btn-primary btn-sm">Pay</a>
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="7">No invoices.</td></tr>
                    @endforelse
                </tbody></table></div>
            @endif
        @endif
    </x-panel>
@endsection
