@extends('layouts.aura-main')

@section('main-content')
<div class="max-w-7xl mx-auto pb-20">
    <div class="flex justify-between items-end mb-8 no-print">
        <div>
            <h1 class="text-3xl font-black text-slate-900 tracking-tighter  leading-none">Aging Analysis</h1>
            <p class="text-[10px] font-black text-slate-400  tracking-[0.2em] mt-2">Accounts Receivable Status • {{ now()->format('d M, Y') }}</p>
        </div>
        <button onclick="window.print()" class="border border-slate-900 px-6 py-2 rounded-sm text-[10px] font-black  tracking-widest hover:bg-slate-900 hover:text-white transition shadow-sm">
            Print Full Report
        </button>
    </div>

    <div class="grid grid-cols-5 gap-4 mb-10 no-print">
        @php
            $buckets = ['1-30', '31-60', '61-90', '90+'];
            $currentTotal = $invoices->filter(fn($i) => $i->getAgingBucket() == 'current')->sum('outstanding_balance');
        @endphp

        <div class="bg-white border-t-2 border-slate-200 p-4 shadow-sm border-l border-r border-b">
            <label class="block text-[9px] font-black text-slate-400  tracking-widest mb-1">Current</label>
            <p class="text-xl font-black text-slate-900 font-mono italic">K{{ number_format($currentTotal, 2) }}</p>
            <div class="mt-2 h-1 w-full bg-slate-100"></div>
        </div>

        @foreach($buckets as $b)
            @php 
                $bTotal = $invoices->filter(fn($i) => $i->getAgingBucket() == $b)->sum('outstanding_balance');
                $accentColor = match($b) {
                    '1-30' => 'slate-400',
                    '31-60' => 'slate-600',
                    '61-90' => 'slate-800',
                    '90+'   => 'rose-600',
                    default => 'slate-200'
                };
            @endphp
            <div class="bg-white border-t-2 border-{{ $accentColor }} p-4 shadow-sm border-l border-r border-b">
                <label class="block text-[9px] font-black text-slate-400  tracking-widest mb-1">{{ $b }} Days</label>
                <p class="text-xl font-black text-slate-900 font-mono italic">K{{ number_format($bTotal, 2) }}</p>
                <div class="mt-2 h-1 w-full bg-{{ $accentColor }}"></div>
            </div>
        @endforeach
    </div>

    <div class="bg-white border-t border-slate-300">
        <table class="w-full text-left border-collapse">
            <thead>
                <tr class="border-b border-slate-900">
                    <th class="px-2 py-3 text-[9px] font-black text-slate-900  tracking-widest">Customer</th>
                    <th class="px-2 py-3 text-[9px] font-black text-slate-900  tracking-widest">Inv #</th>
                    <th class="px-2 py-3 text-[9px] font-black text-slate-900  tracking-widest">Date</th>
                    <th class="px-2 py-3 text-[9px] font-black text-slate-900  tracking-widest">Due</th>
                    <th class="px-2 py-3 text-[9px] font-black text-slate-900  tracking-widest text-center">Days</th>
                    <th class="px-2 py-3 text-[9px] font-black text-slate-900  tracking-widest text-right">Original</th>
                    <th class="px-2 py-3 text-[9px] font-black text-slate-900  tracking-widest text-right">1-30</th>
                    <th class="px-2 py-3 text-[9px] font-black text-slate-900  tracking-widest text-right">31-60</th>
                    <th class="px-2 py-3 text-[9px] font-black text-slate-900  tracking-widest text-right">61-90</th>
                    <th class="px-2 py-3 text-[9px] font-black text-slate-900  tracking-widest text-right">90+</th>
                    <th class="px-2 py-3 text-[9px] font-black text-slate-900  tracking-widest text-right bg-slate-50">Total Balance</th>
                </tr>
            </thead>
            <tbody class="text-[10px] font-medium divide-y divide-slate-100">
                @foreach($invoices as $invoice)
                    @php 
                        $bucket = $invoice->getAgingBucket(); 
                        $balance = $invoice->outstanding_balance;
                        $daysOverdue = now()->diffInDays($invoice->due_date, false);
                        $displayDays = $daysOverdue < 0 ? abs($daysOverdue) : 0;
                    @endphp
                    <tr class="hover:bg-slate-50 transition-colors">
                        <td class="px-2 py-2 font-bold text-slate-800  tracking-tighter">{{ $invoice->customer->name }}</td>
                        <td class="px-2 py-2 font-mono text-indigo-600 font-bold underline decoration-indigo-100">{{ $invoice->invoice_no }}</td>
                        <td class="px-2 py-2 text-slate-400 font-mono">{{ $invoice->invoice_date->format('d/m/y') }}</td>
                        <td class="px-2 py-2 text-slate-400 font-mono">{{ $invoice->due_date->format('d/m/y') }}</td>
                        <td class="px-2 py-2 text-center font-mono {{ $displayDays > 60 ? 'text-rose-600 font-black' : 'text-slate-400' }}">{{ $displayDays }}</td>
                        <td class="px-2 py-2 text-right font-mono text-slate-400">{{ number_format($invoice->total_amount, 2) }}</td>
                        
                        <td class="px-2 py-2 text-right font-mono text-slate-600">{{ $bucket == '1-30' ? number_format($balance, 2) : '-' }}</td>
                        <td class="px-2 py-2 text-right font-mono text-slate-600">{{ $bucket == '31-60' ? number_format($balance, 2) : '-' }}</td>
                        <td class="px-2 py-2 text-right font-mono text-slate-600">{{ $bucket == '61-90' ? number_format($balance, 2) : '-' }}</td>
                        <td class="px-2 py-2 text-right font-mono text-slate-600">{{ $bucket == '90+' ? number_format($balance, 2) : '-' }}</td>

                        <td class="px-2 py-2 text-right font-mono font-black text-slate-900 bg-slate-50/50 border-l border-slate-100">
                            {{ number_format($balance, 2) }}
                        </td>
                    </tr>
                @endforeach
            </tbody>
            <tfoot class="border-t-2 border-slate-900">
                <tr class="font-black text-[10px] text-slate-900  italic">
                    <td colspan="5" class="px-2 py-4">Consolidated Totals (ZMW)</td>
                    <td class="px-2 py-4 text-right font-mono">{{ number_format($invoices->sum('total_amount'), 2) }}</td>
                    <td class="px-2 py-4 text-right font-mono">{{ number_format($invoices->filter(fn($i) => $i->getAgingBucket() == '1-30')->sum('outstanding_balance'), 2) }}</td>
                    <td class="px-2 py-4 text-right font-mono">{{ number_format($invoices->filter(fn($i) => $i->getAgingBucket() == '31-60')->sum('outstanding_balance'), 2) }}</td>
                    <td class="px-2 py-4 text-right font-mono">{{ number_format($invoices->filter(fn($i) => $i->getAgingBucket() == '61-90')->sum('outstanding_balance'), 2) }}</td>
                    <td class="px-2 py-4 text-right font-mono">{{ number_format($invoices->filter(fn($i) => $i->getAgingBucket() == '90+')->sum('outstanding_balance'), 2) }}</td>
                    <td class="px-2 py-4 text-right font-mono underline underline-offset-4 decoration-2">
                        {{ number_format($invoices->sum('outstanding_balance'), 2) }}
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>

    <div class="mt-8 pt-4 border-t border-slate-100 text-[8px] text-slate-400 font-bold  tracking-[0.3em] flex justify-between italic">
        <span>AuraAccountify Management Reporting</span>
        <span>Secure Financial Document</span>
    </div>
</div>

<style>
    @media print {
        .no-print { display: none !important; }
        body { background: white !important; }
        .max-w-7xl { max-width: 100% !important; margin: 0 !important; }
        td, th { border-bottom: 0.5px solid #cbd5e1 !important; }
    }
</style>
@endsection