@extends('layouts.app')
@section('title', $title)
@section('page_title', $title)
@section('breadcrumbs')
    <li class="breadcrumb-item"><a href="{{ route('reports.index') }}">Reports</a></li><li class="breadcrumb-item active">Defaulters</li>
@endsection
@section('page_actions')
    @can('communication.send')
        <a href="{{ route('communication.compose', ['audience' => 'defaulters', 'channel' => 'sms']) }}" class="btn btn-outline-primary"><i class="bi bi-chat-dots me-1"></i>Send Reminder</a>
    @endcan
    @include('reports._export', ['route' => 'reports.defaulters'])
@endsection

@section('content')
    <div class="row g-3 mb-3">
        <div class="col-md-4"><div class="card stat-card"><div class="card-body d-flex align-items-center gap-3">
            <div class="icon bg-soft-danger"><i class="bi bi-exclamation-triangle"></i></div>
            <div><div class="value">{{ $challans->count() }}</div><div class="label">Overdue Challans</div></div>
        </div></div></div>
        <div class="col-md-4"><div class="card stat-card"><div class="card-body d-flex align-items-center gap-3">
            <div class="icon bg-soft-warning"><i class="bi bi-cash-stack"></i></div>
            <div><div class="value" style="font-size:1.3rem">Rs {{ number_format($total) }}</div><div class="label">Total Outstanding</div></div>
        </div></div></div>
    </div>

    <div class="card"><div class="card-body">
        <table class="table table-sm table-hover align-middle">
            <thead><tr><th>Challan</th><th>Family</th><th>Contact</th><th>Title</th><th>Due</th><th>Days</th><th class="text-end">Balance</th></tr></thead>
            <tbody>
                @forelse($challans as $c)
                    <tr>
                        <td class="small"><a href="{{ route('challans.show', $c) }}" class="text-decoration-none">{{ $c->challan_number }}</a></td>
                        <td class="small">{{ $c->family?->family_code }}</td>
                        <td class="small">{{ $c->family?->primary_mobile }}</td>
                        <td class="small">{{ $c->title }}</td>
                        <td class="small">{{ optional($c->due_date)->format('d M Y') }}</td>
                        <td><span class="badge bg-soft-danger">{{ (int) now()->diffInDays($c->due_date) }}</span></td>
                        <td class="text-end fw-semibold text-danger">{{ number_format($c->balance) }}</td>
                    </tr>
                @empty
                    <tr><td colspan="7" class="text-center text-muted py-3">No defaulters 🎉</td></tr>
                @endforelse
            </tbody>
            <tfoot class="table-light fw-bold"><tr><td colspan="6" class="text-end">Total Outstanding</td><td class="text-end">{{ number_format($total) }}</td></tr></tfoot>
        </table>
    </div></div>
@endsection
