@extends('layouts.aura-main')

@section('main-content')
<div class="pb-20">
    <div class="mb-6 flex justify-between items-end">
        <div>
            <h1 class="text-2xl font-bold text-slate-900 tracking-tight">Bills Aging Analysis</h1>
            <p class="text-[11px] font-bold uppercase tracking-widest text-rose-600 italic">Liability Distribution & Cash Flow Risk</p>
        </div>
        
        <a href="{{ route('suppliers.bills.aging.download') }}" class="bg-slate-800 text-white px-4 py-2 rounded-sm text-[9px] font-black uppercase tracking-widest hover:bg-slate-900 transition shadow-md flex items-center gap-2">
            <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"></path>
            </svg>
            Download Report (PDF)
        </a>
    </div>

    <div class="grid grid-cols-6 gap-4 mb-8">
        <div class="bg-white border border-slate-200 p-4 rounded-sm shadow-sm border-l-4 border-l-slate-400">
            <p class="text-[8px] font-black text-slate-400 uppercase tracking-widest mb-1">Current</p>
            <p class="text-sm font-black text-slate-900">K{{ number_format($buckets['current'], 2) }}</p>
        </div>
        <div class="bg-white border border-slate-200 p-4 rounded-sm shadow-sm border-l-4 border-l-amber-400">
            <p class="text-[8px] font-black text-amber-600 uppercase tracking-widest mb-1">1 - 30 Days</p>
            <p class="text-sm font-black text-slate-900">K{{ number_format($buckets['1_30'], 2) }}</p>
        </div>
        <div class="bg-white border border-slate-200 p-4 rounded-sm shadow-sm border-l-4 border-l-orange-500">
            <p class="text-[8px] font-black text-orange-600 uppercase tracking-widest mb-1">31 - 60 Days</p>
            <p class="text-sm font-black text-slate-900">K{{ number_format($buckets['31_60'], 2) }}</p>
        </div>
        <div class="bg-white border border-slate-200 p-4 rounded-sm shadow-sm border-l-4 border-l-rose-500">
            <p class="text-[8px] font-black text-rose-600 uppercase tracking-widest mb-1">61 - 90 Days</p>
            <p class="text-sm font-black text-slate-900">K{{ number_format($buckets['61_90'], 2) }}</p>
        </div>
        <div class="bg-rose-50 border border-rose-200 p-4 rounded-sm shadow-sm border-l-4 border-l-rose-900">
            <p class="text-[8px] font-black text-rose-700 uppercase tracking-widest mb-1">Over 90 Days</p>
            <p class="text-sm font-black text-rose-800">K{{ number_format($buckets['over_90'], 2) }}</p>
        </div>
        <div class="bg-slate-900 p-4 rounded-sm shadow-md">
            <p class="text-[8px] font-black text-slate-400 uppercase tracking-widest mb-1">Total Payables</p>
            <p class="text-sm font-black text-white">K{{ number_format($buckets['total'], 2) }}</p>
        </div>
    </div>
    
    <div class="bg-white border border-slate-300 rounded-sm p-6 mb-8 shadow-sm">
        <div class="mb-4 flex justify-between items-center">
            <h3 class="text-[10px] font-black uppercase tracking-[0.2em] text-slate-400">Aging Distribution (%)</h3>
            <span class="text-[9px] font-bold text-slate-500 italic">Total Liability Concentration</span>
        </div>

        @php
            $total = $buckets['total'] > 0 ? $buckets['total'] : 1;
            $pCurrent = ($buckets['current'] / $total) * 100;
            $p1_30 = ($buckets['1_30'] / $total) * 100;
            $p31_60 = ($buckets['31_60'] / $total) * 100;
            $p61_90 = ($buckets['61_90'] / $total) * 100;
            $pOver90 = ($buckets['over_90'] / $total) * 100;
        @endphp

        <div class="w-full h-8 flex rounded-sm overflow-hidden border border-slate-200 shadow-inner">
            <div style="width: {{ $pCurrent }}%" class="bg-slate-400 h-full group relative" title="Current"></div>
            <div style="width: {{ $p1_30 }}%" class="bg-amber-400 h-full group relative" title="1-30 Days"></div>
            <div style="width: {{ $p31_60 }}%" class="bg-orange-500 h-full group relative" title="31-60 Days"></div>
            <div style="width: {{ $p61_90 }}%" class="bg-rose-500 h-full group relative" title="61-90 Days"></div>
            <div style="width: {{ $pOver90 }}%" class="bg-rose-900 h-full group relative" title="90+ Days"></div>
        </div>

        <div class="mt-4 grid grid-cols-5 gap-2">
            <div class="flex items-center gap-2">
                <span class="w-2 h-2 rounded-full bg-slate-400"></span>
                <span class="text-[9px] font-black text-slate-600 uppercase">{{ number_format($pCurrent, 1) }}% Current</span>
            </div>
            <div class="flex items-center gap-2">
                <span class="w-2 h-2 rounded-full bg-amber-400"></span>
                <span class="text-[9px] font-black text-slate-600 uppercase">{{ number_format($p1_30, 1) }}% 1-30d</span>
            </div>
            <div class="flex items-center gap-2">
                <span class="w-2 h-2 rounded-full bg-orange-500"></span>
                <span class="text-[9px] font-black text-slate-600 uppercase">{{ number_format($p31_60, 1) }}% 31-60d</span>
            </div>
            <div class="flex items-center gap-2">
                <span class="w-2 h-2 rounded-full bg-rose-500"></span>
                <span class="text-[9px] font-black text-slate-600 uppercase">{{ number_format($p61_90, 1) }}% 61-90d</span>
            </div>
            <div class="flex items-center gap-2">
                <span class="w-2 h-2 rounded-full bg-rose-900"></span>
                <span class="text-[9px] font-black text-rose-800 uppercase">{{ number_format($pOver90, 1) }}% Critical (90+)</span>
            </div>
        </div>
    </div>

    <div class="bg-white border border-slate-300 rounded-sm overflow-hidden shadow-sm">
        <table class="w-full text-left border-collapse">
            <thead class="bg-slate-50 border-b border-slate-300 text-[10px] font-black text-slate-500 uppercase tracking-widest">
                <tr>
                    <th class="px-4 py-3 border-r">Vendor</th>
                    <th class="px-4 py-3 border-r">Bill No</th>
                    <th class="px-4 py-3 border-r">Due Date</th>
                    <th class="px-4 py-3 border-r text-center">Overdue Days</th>
                    <th class="px-4 py-3 border-r text-right">Balance</th>
                    <th class="px-4 py-3 border-r text-center">Bracket</th>
                    <th class="px-4 py-3 text-right">Action</th>
                </tr>
            </thead>
            <tbody class="text-xs font-medium">
                @forelse($overdueBills as $bill)
                <tr class="border-b border-slate-200 hover:bg-slate-50/50 transition">
                    <td class="px-4 py-3 border-r font-bold text-slate-800">{{ $bill->supplier->name }}</td>
                    <td class="px-4 py-3 border-r font-mono text-indigo-600 font-bold uppercase">{{ $bill->bill_no }}</td>
                    <td class="px-4 py-3 border-r text-slate-500 font-bold">{{ $bill->due_date->format('d M, Y') }}</td>
                    <td class="px-4 py-3 border-r text-center">
                        @if($bill->days_overdue > 0)
                            <span class="font-black text-rose-600">{{ number_format($bill->days_overdue) }} Days</span>
                        @else
                            <span class="text-[10px] font-black text-emerald-600 uppercase tracking-tighter italic">Current</span>
                        @endif
                    </td>
                    <td class="px-4 py-3 border-r text-right font-mono font-black text-slate-900 bg-slate-50/20">
                        K{{ number_format($bill->outstanding_balance, 2) }}
                    </td>
                    <td class="px-4 py-3 border-r text-center">
                        @php
                            if($bill->days_overdue == 0) { $bracket = 'CURRENT'; $color = 'bg-slate-400'; }
                            elseif($bill->days_overdue <= 30) { $bracket = '1-30'; $color = 'bg-amber-400'; }
                            elseif($bill->days_overdue <= 60) { $bracket = '31-60'; $color = 'bg-orange-500'; }
                            elseif($bill->days_overdue <= 90) { $bracket = '61-90'; $color = 'bg-rose-500'; }
                            else { $bracket = '90+'; $color = 'bg-rose-900'; }
                        @endphp
                        <span class="{{ $color }} text-white px-2 py-0.5 rounded-sm text-[8px] font-black uppercase shadow-sm">{{ $bracket }}</span>
                    </td>
                    <td class="px-4 py-3 text-right">
                        <a href="{{ route('suppliers.bills.show', $bill->id) }}" class="text-indigo-600 hover:text-indigo-900 text-[10px] font-black uppercase tracking-widest flex items-center justify-end gap-1">
                            Details
                            <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M9 5l7 7-7 7"></path></svg>
                        </a>
                    </td>
                </tr>
                @empty
                <tr><td colspan="7" class="p-20 text-center opacity-30 font-black uppercase tracking-widest text-xs text-slate-400">No outstanding liabilities found</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>
@endsection