@extends('layouts.app')

@section('title', __('clinic.invoices'))
@section('heading', __('clinic.invoices'))

@section('content')
    <div class="card mb-1">
        <form method="GET" action="{{ route('invoices.index') }}" class="form-grid" style="--grid-cols: 3; gap: 0.75rem; align-items: flex-end;">
            <div class="form-group mb-0">
                <label for="q">{{ __('clinic.search') }}</label>
                <input id="q" class="form-control" name="q" value="{{ request('q') }}" placeholder="{{ __('clinic.search_placeholder') }}">
            </div>

            <div class="form-group mb-0">
                <label for="status">{{ __('clinic.payment_status') }}</label>
                <select id="status" class="form-control" name="status">
                    <option value="">{{ __('clinic.view_all') }}</option>
                    <option value="unpaid" {{ request('status') === 'unpaid' ? 'selected' : '' }}>{{ __('clinic.unpaid') }}</option>
                    <option value="partial" {{ request('status') === 'partial' ? 'selected' : '' }}>{{ __('clinic.partial') }}</option>
                    <option value="paid" {{ request('status') === 'paid' ? 'selected' : '' }}>{{ __('clinic.paid') }}</option>
                </select>
            </div>

            <div class="flex gap">
                <button type="submit" class="btn btn-primary btn-sm" style="flex: 1;">{{ __('clinic.filter') }}</button>
                <a href="{{ route('invoices.index') }}" class="btn btn-ghost btn-sm">✕</a>
            </div>
        </form>
    </div>

    <div class="flex-between mb-1" style="display:flex; justify-content:flex-end;">
        <a href="{{ route('invoices.create') }}" class="btn btn-primary btn-sm">➕ {{ __('clinic.new_invoice') }}</a>
    </div>

    <div class="table-wrap">
        <table class="data-table">
            <thead>
                <tr>
                    <th>{{ __('clinic.invoice_number') }}</th>
                    <th>{{ __('clinic.patients') }}</th>
                    <th>{{ __('clinic.total_amount') }}</th>
                    <th>{{ __('clinic.paid_amount') }}</th>
                    <th>{{ __('clinic.remaining_amount') }}</th>
                    <th>{{ __('clinic.payment_method') }}</th>
                    <th>{{ __('clinic.payment_status') }}</th>
                    <th>{{ __('clinic.visit_date') }}</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @forelse ($invoices as $inv)
                    <tr>
                        <td><strong><a href="{{ route('invoices.show', $inv) }}">{{ $inv->invoice_number }}</a></strong></td>
                        <td><a href="{{ route('patients.show', $inv->patient) }}">{{ $inv->patient->full_name }}</a></td>
                        <td>{{ number_format($inv->total_amount, 2) }}</td>
                        <td>{{ number_format($inv->paid_amount, 2) }}</td>
                        <td style="color: {{ $inv->remaining_amount > 0 ? 'var(--danger)' : 'inherit' }}">{{ number_format($inv->remaining_amount, 2) }}</td>
                        <td>{{ __('clinic.' . $inv->payment_method) }}</td>
                        <td>
                            <span class="pill @if($inv->payment_status === 'paid') pill-success @elseif($inv->payment_status === 'partial') pill-warning @else pill-danger @endif">
                                {{ __('clinic.' . $inv->payment_status) }}
                            </span>
                        </td>
                        <td>{{ $inv->created_at->format('Y-m-d') }}</td>
                        <td>
                            <a href="{{ route('invoices.show', $inv) }}" class="btn btn-ghost btn-sm">Open</a>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="9" style="text-align: center;">{{ __('clinic.no_records') }}</td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    @include('partials.pagination', ['paginator' => $invoices])
@endsection
