@extends('layouts.admin', ['heading' => 'Wallet Ledger'])

@section('content')
    <div class="mb-4 admin-card p-4">
        <div class="text-sm text-slate-600">Member</div>
        <div class="text-lg font-semibold">{{ $member->member_code }} - {{ $member->name }}</div>
        <div class="mt-3 flex gap-2">
            <a href="{{ route('admin.wallet.ledger.export', [$member, 'export' => 'excel']) }}" class="rounded bg-emerald-600 px-3 py-1 text-xs text-white">Export Excel</a>
            <a href="{{ route('admin.wallet.ledger.export', [$member, 'export' => 'pdf']) }}" class="rounded bg-slate-700 px-3 py-1 text-xs text-white">Export PDF</a>
        </div>
    </div>

    <div class="mb-6 grid grid-cols-1 gap-4 lg:grid-cols-2">
        <div class="admin-card p-4">
            <div class="mb-2 text-sm font-semibold">Carry Forward</div>
            <div class="text-sm text-slate-600">Joining</div>
            <div class="mb-2 grid grid-cols-2 gap-2">
                <div class="rounded bg-slate-50 p-2">Left: {{ number_format((float) ($carry->joining_left ?? 0), 2) }}</div>
                <div class="rounded bg-slate-50 p-2">Right: {{ number_format((float) ($carry->joining_right ?? 0), 2) }}</div>
            </div>
            <div class="text-sm text-slate-600">Repurchase</div>
            <div class="grid grid-cols-2 gap-2">
                <div class="rounded bg-slate-50 p-2">Left: {{ number_format((float) ($carry->repurchase_left ?? 0), 2) }}</div>
                <div class="rounded bg-slate-50 p-2">Right: {{ number_format((float) ($carry->repurchase_right ?? 0), 2) }}</div>
            </div>
        </div>

        <div class="admin-card p-4">
            <div class="mb-2 text-sm font-semibold">Last Processed</div>
            <div class="text-sm text-slate-600">Joining: {{ optional($carry?->last_joining_processed_at)->format('Y-m-d H:i') ?? '-' }}</div>
            <div class="text-sm text-slate-600">Repurchase: {{ optional($carry?->last_repurchase_processed_at)->format('Y-m-d H:i') ?? '-' }}</div>
            <div class="text-sm text-slate-600">Legacy: {{ optional($carry?->last_processed_at)->format('Y-m-d H:i') ?? '-' }}</div>
        </div>
    </div>

    <div class="overflow-x-auto rounded-2xl border border-slate-200 bg-white shadow-sm">
        <table class="min-w-full text-sm">
            <thead class="bg-slate-100 text-left">
                <tr>
                    <th class="p-3">Date</th>
                    <th class="p-3">Type</th>
                    <th class="p-3">Direction</th>
                    <th class="p-3">Amount</th>
                    <th class="p-3">Opening</th>
                    <th class="p-3">Closing</th>
                    <th class="p-3">Reference</th>
                </tr>
            </thead>
            <tbody>
                @forelse($transactions as $txn)
                    <tr class="border-t">
                        <td class="p-3">{{ optional($txn->transacted_at)->format('Y-m-d H:i') }}</td>
                        <td class="p-3">{{ $txn->wallet_type }}</td>
                        <td class="p-3">{{ $txn->direction }}</td>
                        <td class="p-3">{{ number_format((float) $txn->amount, 2) }}</td>
                        <td class="p-3">{{ number_format((float) $txn->opening_balance, 2) }}</td>
                        <td class="p-3">{{ number_format((float) $txn->closing_balance, 2) }}</td>
                        <td class="p-3">{{ $txn->display_reference ?? $txn->reference }}</td>
                    </tr>
                @empty
                    <tr><td colspan="7" class="p-4 text-center text-slate-500">No transactions found.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="mt-4">{{ $transactions->links() }}</div>

    <div class="mt-8 grid grid-cols-1 gap-6 lg:grid-cols-2">
        <div class="admin-card p-4">
            <div class="mb-2 font-semibold">Joining Capping History (Last 20)</div>
            <div class="overflow-x-auto">
                <table class="min-w-full text-sm">
                    <thead class="bg-slate-100 text-left">
                        <tr>
                            <th class="p-2">Date</th>
                            <th class="p-2">Gross</th>
                            <th class="p-2">Capped</th>
                            <th class="p-2">Net</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($joiningCapping as $row)
                            <tr class="border-t">
                                <td class="p-2">{{ optional($row->run_date)->format('Y-m-d') }}</td>
                                <td class="p-2">{{ number_format((float) $row->gross_income, 2) }}</td>
                                <td class="p-2">{{ number_format((float) $row->capping_deduction, 2) }}</td>
                                <td class="p-2">{{ number_format((float) $row->net_income, 2) }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="p-3 text-center text-slate-500">No joining capping records.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <div class="admin-card p-4">
            <div class="mb-2 font-semibold">Repurchase Capping History (Last 20)</div>
            <div class="overflow-x-auto">
                <table class="min-w-full text-sm">
                    <thead class="bg-slate-100 text-left">
                        <tr>
                            <th class="p-2">Date</th>
                            <th class="p-2">Gross</th>
                            <th class="p-2">Capped</th>
                            <th class="p-2">Net</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($repurchaseCapping as $row)
                            <tr class="border-t">
                                <td class="p-2">{{ optional($row->run_date)->format('Y-m-d') }}</td>
                                <td class="p-2">{{ number_format((float) $row->gross_income, 2) }}</td>
                                <td class="p-2">{{ number_format((float) $row->capping_deduction, 2) }}</td>
                                <td class="p-2">{{ number_format((float) $row->net_income, 2) }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="p-3 text-center text-slate-500">No repurchase capping records.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection
