@extends('layouts.app')
@section('title', __('Invoices'))

@section('content')
<div class="d-flex flex-wrap justify-content-between align-items-center gap-2 mb-3">
    <h1 class="h4 fw-bold mb-0">🧾 {{ __('Invoices') }}</h1>
    <a href="{{ route('tenant.installments.index') }}" class="btn mb-btn-ghost btn-sm">💰 {{ __('Installments due') }}</a>
</div>

<form class="d-flex gap-2 mb-3 flex-wrap" method="GET">
    <input name="q" value="{{ request('q') }}" class="form-control" style="max-width:20rem"
           placeholder="{{ __('Search number or patient…') }}">
    <select name="status" class="form-select" style="max-width:12rem" onchange="this.form.submit()">
        <option value="">{{ __('All') }}</option>
        @foreach($statuses as $s)
            <option value="{{ $s->value }}" @selected(request('status') === $s->value)>{{ $s->label() }}</option>
        @endforeach
    </select>
</form>

<div class="mb-card p-3">
    <table class="table mb-table align-middle mb-0">
        <thead><tr>
            <th>{{ __('Number') }}</th><th>{{ __('Patient') }}</th><th>{{ __('Total') }}</th>
            <th>{{ __('Paid') }}</th><th>{{ __('Remaining') }}</th><th>{{ __('Status') }}</th>
        </tr></thead>
        <tbody>
        @forelse($invoices as $invoice)
            <tr>
                <td><a href="{{ route('tenant.invoices.show', $invoice) }}" dir="ltr">{{ $invoice->number }}</a></td>
                <td>{{ $invoice->patient->full_name }}</td>
                <td dir="ltr">{{ number_format($invoice->total) }}</td>
                <td dir="ltr" class="text-success">{{ number_format($invoice->paid_amount) }}</td>
                <td dir="ltr" class="{{ $invoice->remaining() ? 'text-danger fw-semibold' : 'mb-muted' }}">{{ number_format($invoice->remaining()) }}</td>
                <td><span class="status-chip" style="background: {{ $invoice->status->color() }}">{{ $invoice->status->label() }}</span></td>
            </tr>
        @empty
            <tr><td colspan="6" class="mb-muted py-4 text-center">{{ __('No invoices yet. Create one from a treatment plan.') }}</td></tr>
        @endforelse
        </tbody>
    </table>
</div>
{{ $invoices->links() }}
@endsection
