@extends('layouts.aura-main')

@section('main-content')
<div class="pb-10 max-w-5xl mx-auto">
    <div class="mb-4 flex justify-between items-center print:hidden">
        <a href="{{ route('suppliers.bills.index') }}" class="text-[10px] font-black uppercase tracking-widest text-slate-400 hover:text-indigo-600 flex items-center gap-2 transition">
            <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="M15 19l-7-7 7-7"></path></svg>
            Back to Bills
        </a>
        
        <div class="flex gap-2">
            <a href="{{ route('suppliers.bills.download', $bill->id) }}" class="bg-white border border-slate-300 text-slate-600 px-3 py-1.5 rounded-sm text-[9px] font-black uppercase tracking-widest hover:bg-slate-50 transition shadow-sm flex items-center gap-2">
                Download PDF
            </a>
           
            @if($bill->outstanding_balance > 0)
                <a href="{{ route('suppliers.billpayments.pay', $bill->id) }}" class="bg-indigo-600 text-white px-3 py-1.5 rounded-sm text-[9px] font-black uppercase tracking-widest hover:bg-indigo-700 transition shadow-sm">
                    Record Payment
                </a>
            @endif
        </div>
    </div>

    <div class="bg-white border border-slate-300 rounded-sm overflow-hidden shadow-sm print:border-0 print:shadow-none">
        <div class="p-6 border-b border-slate-200 flex justify-between items-start bg-slate-50/30">
            <div class="space-y-4">
                <div>
                    <p class="text-[9px] font-black text-indigo-600 uppercase tracking-[0.2em] mb-1">Purchases Account</p>
                    <h1 class="text-xl font-black text-slate-900 uppercase leading-none">Bill #{{ $bill->bill_no }}</h1>
                </div>
                <div>
                    <p class="text-[8px] font-black text-slate-400 uppercase tracking-tighter">Vendor Information</p>
                    <p class="text-xs font-bold text-slate-800">{{ $bill->supplier->name }}</p>
                    <p class="text-[10px] font-medium text-slate-500 italic">{{ $bill->supplier->email ?? 'no-email@vendor.com' }}</p>
                </div>
            </div>

            <div class="text-right flex flex-col items-end gap-3">
                @php
                    $statusStyles = [
                        'unpaid' => 'bg-rose-50 text-rose-600 border-rose-200',
                        'paid' => 'bg-emerald-50 text-emerald-600 border-emerald-200',
                        'partial' => 'bg-amber-50 text-amber-600 border-amber-200'
                    ];
                @endphp
                <span class="px-3 py-1 rounded-sm text-[9px] font-black uppercase tracking-widest border {{ $statusStyles[$bill->status] ?? 'bg-slate-50 text-slate-500 border-slate-200' }}">
                    {{ $bill->status }}
                </span>
                <div class="text-right">
                    <p class="text-[8px] font-black text-slate-400 uppercase tracking-widest">Balance Due</p>
                    <p class="text-lg font-black text-slate-900 font-mono leading-none">K{{ number_format($bill->outstanding_balance, 2) }}</p>
                </div>
            </div>
        </div>

        <div class="grid grid-cols-4 border-b border-slate-200 bg-white text-center">
            <div class="py-3 border-r border-slate-200">
                <p class="text-[8px] font-black text-slate-400 uppercase mb-0.5">Bill Date</p>
                <p class="text-[11px] font-bold text-slate-700">{{ $bill->bill_date->format('d M, Y') }}</p>
            </div>
            <div class="py-3 border-r border-slate-200">
                <p class="text-[8px] font-black text-slate-400 uppercase mb-0.5">Due Date</p>
                <p class="text-[11px] font-bold {{ $bill->due_date->isPast() && $bill->outstanding_balance > 0 ? 'text-rose-600' : 'text-slate-700' }}">
                    {{ $bill->due_date->format('d M, Y') }}
                </p>
            </div>
            <div class="py-3 border-r border-slate-200">
                <p class="text-[8px] font-black text-slate-400 uppercase mb-0.5">Payment Terms</p>
                <p class="text-[11px] font-bold text-slate-700 uppercase italic">Net {{ $bill->bill_date->diffInDays($bill->due_date) }} Days</p>
            </div>
            <div class="py-3">
                <p class="text-[8px] font-black text-slate-400 uppercase mb-0.5">Recorded By</p>
                <p class="text-[11px] font-bold text-slate-700">{{ $bill->user->name ?? 'System' }}</p>
            </div>
        </div>

        <table class="w-full text-left border-collapse">
            <thead class="bg-slate-50 border-b border-slate-200 text-[8px] font-black text-slate-500 uppercase tracking-widest">
                <tr>
                    <th class="px-6 py-2 border-r">Account Mapping</th>
                    <th class="px-6 py-2 border-r">Description / Memo</th>
                    <th class="px-6 py-2 text-right">Amount</th>
                </tr>
            </thead>
            <tbody class="text-[11px] font-medium">
                @foreach($bill->items as $item)
                <tr class="border-b border-slate-100 last:border-0 hover:bg-slate-50/50 transition">
                    <td class="px-6 py-2.5 border-r">
                        <span class="block font-bold text-slate-800 uppercase tracking-tighter">{{ $item->account->name }}</span>
                        <span class="text-[8px] text-slate-400 font-bold uppercase">{{ $item->account->code }}</span>
                    </td>
                    <td class="px-6 py-2.5 border-r text-slate-500 italic">
                        {{ $item->description ?? 'Standard purchase entry' }}
                    </td>
                    <td class="px-6 py-2.5 text-right font-mono font-black text-slate-900">
                        K{{ number_format($item->amount, 2) }}
                    </td>
                </tr>
                @endforeach
            </tbody>
        </table>

        <div class="flex justify-end p-4 bg-slate-50/20">
            <div class="w-48 space-y-1">
                <div class="flex justify-between text-[10px] font-bold text-slate-400 uppercase tracking-tighter">
                    <span>Subtotal</span>
                    <span>K{{ number_format($bill->total_amount, 2) }}</span>
                </div>
                <div class="pt-2 border-t border-slate-200 flex justify-between items-center">
                    <span class="text-[9px] font-black text-slate-900 uppercase">Total Liability</span>
                    <span class="text-sm font-black text-indigo-600 font-mono">K{{ number_format($bill->total_amount, 2) }}</span>
                </div>
            </div>
        </div>
    </div>

    <div class="mt-8">
        <div class="mb-3 flex items-center gap-3">
            <h3 class="text-[9px] font-black uppercase tracking-[0.2em] text-slate-400">Payment Audit Trail</h3>
            <div class="flex-1 h-px bg-slate-200"></div>
        </div>

        <div class="bg-white border border-slate-300 rounded-sm shadow-sm overflow-hidden">
            <table class="w-full text-left border-collapse">
                <thead class="bg-slate-50 border-b border-slate-300 text-[8px] font-black text-slate-500 uppercase tracking-widest">
                    <tr>
                        <th class="px-6 py-2 border-r">Date</th>
                        <th class="px-6 py-2 border-r">Reference</th>
                        <th class="px-6 py-2 border-r">Source Account</th>
                        <th class="px-6 py-2 text-right border-r">Amount Paid</th>
                        <th class="px-6 py-2 text-right print:hidden">Action</th>
                    </tr>
                </thead>
                <tbody class="text-[10px]">
                    @forelse($bill->cashbookEntries as $payment)
                    @php
                        $isReversed = $payment->status === 'reversed';
                        $isContra = $payment->status === 'reversal';
                    @endphp
                    <tr class="border-b border-slate-100 last:border-0 transition {{ $isReversed ? 'bg-slate-50 opacity-50' : ($isContra ? 'bg-rose-50/30' : 'hover:bg-slate-50') }}">
                        <td class="px-6 py-2.5 border-r font-bold text-slate-600">{{ $payment->transaction_date->format('d M, Y') }}</td>
                        <td class="px-6 py-2.5 border-r">
                            <span class="font-black text-slate-800 uppercase tracking-tighter">{{ $payment->reference }}</span>
                            @if($isReversed)
                                <span class="block text-[7px] text-rose-600 font-black uppercase italic mt-0.5">Reversed (Void)</span>
                            @elseif($isContra)
                                <span class="block text-[7px] text-rose-600 font-black uppercase italic mt-0.5">Correction Entry</span>
                            @endif
                        </td>
                        <td class="px-6 py-2.5 border-r font-medium text-slate-500">{{ $payment->bankAccount->name ?? 'Unknown Account' }}</td>
                        
                        <td class="px-6 py-2.5 border-r text-right font-mono font-black {{ $isContra ? 'text-rose-600' : 'text-emerald-600' }}">
                            @php
                                $val = ($payment->credit > 0) ? $payment->credit : $payment->debit;
                                if ($isContra) $val *= -1;
                            @endphp
                            {{ $val < 0 ? '-' : '' }}K{{ number_format(abs($val), 2) }}
                        </td>

                        <td class="px-6 py-2.5 text-right print:hidden">
                            @if(!$isReversed && !$isContra)
                                <a href="{{ route('suppliers.billpayments.reverse_confirm', $payment->id) }}" class="text-rose-500 font-black hover:text-rose-700 uppercase text-[8px] tracking-tighter transition underline decoration-dotted">
                                    Reverse
                                </a>
                            @else
                                <span class="text-slate-300 text-[8px] font-black uppercase tracking-tighter italic">No Action</span>
                            @endif
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="5" class="px-6 py-12 text-center text-slate-300 uppercase font-black tracking-widest text-[9px]">
                            No disbursement records linked to this bill
                        </td>
                    </tr>
                    @endforelse
                </tbody>
                
                <tfoot class="border-t-2 border-slate-200 bg-white">
                    <tr class="hover:bg-slate-50 transition">
                        <td colspan="3" class="px-6 py-3 text-[9px] font-black uppercase tracking-widest text-slate-500">Total Net Settled</td>
                        <td class="px-6 py-3 text-right font-mono font-black text-emerald-600 text-xs" colspan="2">
                            K{{ number_format($bill->total_paid, 2) }}
                        </td>
                    </tr>
                    <tr class="border-t border-slate-100 hover:bg-slate-50 transition">
                        <td colspan="3" class="px-6 py-3 text-[9px] font-black uppercase tracking-widest text-slate-500">Current Remaining Liability</td>
                        <td class="px-6 py-3 text-right font-mono font-black {{ $bill->outstanding_balance > 0 ? 'text-rose-600' : 'text-emerald-600' }} text-xs" colspan="2">
                            K{{ number_format($bill->outstanding_balance, 2) }}
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>

<style>
    @media print {
        body { background: white !important; }
        .pb-10 { padding-bottom: 0; }
        nav, .mb-4, .print\:hidden { display: none !important; }
        .max-w-5xl { max-width: 100% !important; margin: 0 !important; }
    }
</style>
@endsection