@extends('eliteadmin::layouts.app')

@section('title', 'Wallet')
@section('page_class', 'page-wallet')

@section('content')
    <div class="page-header">
        <div>
            <h1 class="page-title">Wallet</h1>
            <nav class="breadcrumb">
                <a href="{{ route('dashboard') }}" class="breadcrumb-item">Home</a>
                <span class="breadcrumb-item active">Wallet</span>
            </nav>
        </div>
    </div>

    @if ($platformNotice ?? null)
        <x-eliteadmin::alert type="info" :message="$platformNotice" class="mb-4" />
    @endif

    @if ($wallet ?? null)
        <div class="card mb-4">
            <div class="card-body d-flex justify-content-between align-items-center flex-wrap gap-2">
                <div>
                    <div class="text-muted small">Platform Wallet Balance</div>
                    <strong class="fs-4">{{ platform_money($wallet->balance, $wallet->currency) }}</strong>
                </div>
                <span class="text-muted small">Managed by Product Owner Platform</span>
            </div>
        </div>
    @else
        <div class="card mb-4">
            <div class="card-body text-center text-muted py-4">
                No wallet balance recorded yet. Module purchases and top-ups from the Product Owner Platform appear here.
            </div>
        </div>
    @endif

    <div class="card">
        <div class="card-header">
            <h5 class="card-title mb-0">Transaction Ledger</h5>
        </div>
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead class="table-light">
                    <tr>
                        <th>Date</th>
                        <th>Type</th>
                        <th>Amount</th>
                        <th class="d-none d-md-table-cell">Opening</th>
                        <th class="d-none d-md-table-cell">Closing</th>
                        <th class="d-none d-lg-table-cell">Reference</th>
                        <th>Description</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($ledger as $entry)
                        <tr>
                            <td>{{ $entry->recorded_at->format('M j, Y H:i') }}</td>
                            <td>
                                <span class="badge bg-{{ $entry->type === 'credit' ? 'success' : 'danger' }}">
                                    {{ ucfirst($entry->type) }}
                                </span>
                            </td>
                            <td>{{ platform_money($entry->amount, $entry->currency) }}</td>
                            <td class="d-none d-md-table-cell">{{ number_format($entry->opening_balance, 2) }}</td>
                            <td class="d-none d-md-table-cell">{{ number_format($entry->closing_balance, 2) }}</td>
                            <td class="d-none d-lg-table-cell"><code class="small">{{ $entry->reference_number }}</code></td>
                            <td>{{ $entry->description ?? '—' }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="text-center text-muted py-4">
                                No wallet transactions yet. Module purchases and top-ups from the Product Owner Platform appear here.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if ($ledger->hasPages())
            <div class="card-footer">
                {{ $ledger->links() }}
            </div>
        @endif
    </div>
@endsection
