@extends('layouts.aura-main')

@section('main-content')
<div class="max-w-5xl mx-auto pb-20">
    <div class="flex justify-between items-center mb-8 no-print">
        <div>
            <h1 class="text-2xl font-black text-slate-900 tracking-tight uppercase">Sales Receipt History</h1>
            <p class="text-[11px] font-bold text-indigo-600 tracking-widest uppercase">Statement for Receipt #{{ $receipt->receipt_no }}</p>
        </div>
        <div class="flex gap-3">
            <a href="{{ route('sales-receipts.index') }}" class="text-[11px] font-black text-slate-400 hover:text-slate-600 uppercase tracking-widest pt-2">Back</a>
            <button onclick="window.print()" class="bg-slate-900 text-white px-6 py-2 rounded-sm text-[10px] font-black uppercase tracking-widest shadow-lg">Print Statement</button>
        </div>
    </div>

    <div class="grid grid-cols-3 gap-6 mb-8">
        <div class="bg-white border border-slate-300 p-6 rounded-sm shadow-sm border-t-4 border-t-indigo-500">
            <label class="block text-[10px] font-black text-slate-400 uppercase mb-1">Total Sale</label>
            <p class="text-xl font-black text-slate-900 font-mono">ZMW {{ number_format($receipt->amount, 2) }}</p>
        </div>
        <div class="bg-white border border-slate-300 p-6 rounded-sm shadow-sm border-t-4 border-t-amber-500">
            <label class="block text-[10px] font-black text-slate-400 uppercase mb-1">Total Reversed</label>
            <p class="text-xl font-black text-amber-600 font-mono">ZMW {{ number_format($receipt->total_reversed_amount, 2) }}</p>
        </div>
        <div class="bg-white border border-slate-300 p-6 rounded-sm shadow-sm border-t-4 border-t-emerald-500">
            <label class="block text-[10px] font-black text-slate-400 uppercase mb-1">Net Revenue</label>
            <p class="text-xl font-black text-emerald-600 font-mono">ZMW {{ number_format($receipt->remaining_amount, 2) }}</p>
        </div>
    </div>

    <div class="bg-white border border-slate-300 rounded-sm shadow-sm overflow-hidden p-10">
        <div class="mb-10 pb-10 border-b border-slate-100 grid grid-cols-2">
            <div>
                <label class="block text-[10px] font-black text-slate-400 uppercase tracking-widest mb-3">Customer Information</label>
                <h3 class="text-xl font-black text-slate-900 leading-tight">{{ $receipt->customer->name }}</h3>
                <p class="text-xs text-slate-500 mt-1 italic font-medium">{{ $receipt->customer->email }}</p>
            </div>
            <div class="text-right">
                <label class="block text-[10px] font-black text-slate-400 uppercase tracking-widest mb-3">Deposit Account</label>
                <p class="text-sm font-bold text-slate-700">{{ $receipt->bankAccount->name ?? 'N/A' }}</p>
                <p class="text-[10px] text-slate-400 uppercase font-black mt-1">{{ $receipt->payment_method }}</p>
            </div>
        </div>

        <table class="w-full text-left border-collapse border border-slate-200">
            <thead class="bg-slate-50 text-[10px] font-black text-slate-500 uppercase tracking-widest border-b border-slate-200">
                <tr>
                    <th class="px-6 py-4">Date</th>
                    <th class="px-6 py-4">Activity</th>
                    <th class="px-6 py-4 text-right">Debit (+)</th>
                    <th class="px-6 py-4 text-right">Credit (-)</th>
                    <th class="px-6 py-4 text-right bg-slate-100/30">Net Impact</th>
                </tr>
            </thead>
            <tbody class="text-[11px] font-medium divide-y divide-slate-100">
                @php $runningBalance = 0; @endphp
                @foreach($history as $trx)
                    @php 
                        if($trx->is_debit) $runningBalance += $trx->amount;
                        else $runningBalance -= $trx->amount;
                    @endphp
                    <tr>
                        <td class="px-6 py-5 text-slate-500 font-mono">{{ \Carbon\Carbon::parse($trx->date)->format('d M, Y') }}</td>
                        <td class="px-6 py-5">
                            <span class="block font-black text-slate-800 uppercase text-[10px]">{{ $trx->type }}</span>
                            <span class="text-[9px] text-slate-400 font-mono italic">REF: {{ $trx->reference }}</span>
                        </td>
                        <td class="px-6 py-5 text-right font-mono text-slate-900">
                            {{ $trx->is_debit ? 'ZMW '.number_format($trx->amount, 2) : '-' }}
                        </td>
                        <td class="px-6 py-5 text-right font-mono text-amber-600">
                            {{ !$trx->is_debit ? 'ZMW '.number_format($trx->amount, 2) : '-' }}
                        </td>
                        <td class="px-6 py-5 text-right font-mono font-black text-slate-900 bg-slate-50/30">
                            ZMW {{ number_format($runningBalance, 2) }}
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>

        @if($receipt->reversals->count() > 0)
        <div class="mt-10 p-6 bg-slate-50 border border-slate-200 rounded-sm">
            <label class="block text-[10px] font-black text-slate-400 uppercase tracking-widest mb-4">Reversal Log</label>
            <div class="space-y-4">
                @foreach($receipt->reversals as $rev)
                <div class="flex gap-4 items-start">
                    <div class="w-2 h-2 rounded-full bg-amber-400 mt-1"></div>
                    <div>
                        <p class="text-[11px] font-black text-slate-800 uppercase tracking-tight">{{ $rev->reversal_no }} (ZMW {{ number_format($rev->reversal_amount, 2) }})</p>
                        <p class="text-[11px] text-slate-500 italic mt-0.5 leading-relaxed font-medium">"{{ $rev->reversal_reason }}"</p>
                    </div>
                </div>
                @endforeach
            </div>
        </div>
        @endif
    </div>
</div>

<style>
    @media print {
        .no-print { display: none !important; }
        .max-w-5xl { max-width: 100% !important; }
    }
</style>
@endsection