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

@section('content')
<h1 class="h4 fw-bold mb-3">💰 {{ __('Installments') }}</h1>

{{-- Outstanding-balance widgets --}}
<div class="row g-3 mb-4">
    @foreach([
        [__('Due today'), $widgets['due_today'], '#0EA5E9'],
        [__('Due this week'), $widgets['due_week'], '#8B5CF6'],
        [__('Overdue'), $widgets['overdue'], '#EF4444', $widgets['overdue_count'].' '.__('installments')],
        [__('Total outstanding'), $widgets['total_outstanding'], '#F5A524'],
    ] as $w)
        <div class="col-6 col-xl-3">
            <div class="mb-card p-3" style="border-inline-start: 4px solid {{ $w[2] }}">
                <div class="mb-muted small">{{ $w[0] }}</div>
                <div class="kpi-value" dir="ltr">{{ number_format($w[1]) }} <span class="fs-6 mb-muted">IQD</span></div>
                @isset($w[3])<div class="mb-muted small">{{ $w[3] }}</div>@endisset
            </div>
        </div>
    @endforeach
</div>

@if($overdue->isNotEmpty())
<h2 class="h6 fw-semibold text-danger">⚠ {{ __('Overdue') }} ({{ $overdue->count() }})</h2>
<div class="mb-card p-3 mb-4">
    <table class="table mb-table align-middle mb-0">
        <tbody>
        @foreach($overdue as $inst)
            <tr class="table-danger">
                <td>
                    <a href="{{ route('tenant.patients.show', $inst->invoice->patient_id) }}">{{ $inst->invoice->patient->full_name }}</a>
                    <div class="mb-muted small" dir="ltr">{{ $inst->invoice->patient->phone }} · {{ $inst->invoice->number }}</div>
                </td>
                <td class="text-danger fw-semibold">{{ $inst->due_date->format('d M Y') }}
                    <div class="mb-muted small">{{ $inst->due_date->diffForHumans() }}</div>
                </td>
                <td dir="ltr" class="fw-semibold">{{ number_format($inst->remaining()) }} IQD</td>
                <td style="min-width: 22rem">
                    @can('installments.manage')
                        @include('invoices._quickpay', ['invoice' => $inst->invoice, 'installment' => $inst, 'methods' => $methods])
                    @endcan
                </td>
            </tr>
        @endforeach
        </tbody>
    </table>
</div>
@endif

<h2 class="h6 fw-semibold">{{ __('Due in the next 30 days') }}</h2>
<div class="mb-card p-3">
    <table class="table mb-table align-middle mb-0">
        <tbody>
        @forelse($upcoming as $inst)
            <tr>
                <td>
                    <a href="{{ route('tenant.patients.show', $inst->invoice->patient_id) }}">{{ $inst->invoice->patient->full_name }}</a>
                    <div class="mb-muted small" dir="ltr">{{ $inst->invoice->number }}</div>
                </td>
                <td>{{ $inst->due_date->format('d M Y') }}
                    @if($inst->due_date->isToday())<span class="badge text-bg-info">{{ __('Today') }}</span>@endif
                </td>
                <td dir="ltr">{{ number_format($inst->remaining()) }} IQD</td>
                <td style="min-width: 22rem">
                    @can('installments.manage')
                        @include('invoices._quickpay', ['invoice' => $inst->invoice, 'installment' => $inst, 'methods' => $methods])
                    @endcan
                </td>
            </tr>
        @empty
            <tr><td class="mb-muted py-4 text-center">{{ __('Nothing due in the next 30 days.') }}</td></tr>
        @endforelse
        </tbody>
    </table>
</div>
{{ $upcoming->links() }}
@endsection
