@extends('super-admin.layout')

@section('title', 'Billing')

@section('content')
<div class="pg-head">
    <div>
        <h1>Billing</h1>
        <p>Per-event invoices, manual payments, promo codes, and organisation credits.</p>
    </div>
    <div class="pg-actions">
        <a href="{{ route('mexco.super.invoices') }}" class="btn-outline">All invoices</a>
        <a href="{{ route('mexco.super.promos.create') }}" class="btn-purple">+ Promo code</a>
    </div>
</div>

<div class="stat-row">
    <div class="card stat-card">
        <div class="stat-label">Outstanding</div>
        <div class="stat-num" style="font-size:1.25rem">MYR {{ number_format($stats['outstanding_cents'] / 100, 0) }}</div>
    </div>
    <div class="card stat-card">
        <div class="stat-label">Pending invoices</div>
        <div class="stat-num">{{ $stats['pending'] }}</div>
    </div>
    <div class="card stat-card">
        <div class="stat-label">Paid this month</div>
        <div class="stat-num" style="font-size:1.25rem;color:var(--mexco-success)">MYR {{ number_format($stats['paid_this_month_cents'] / 100, 0) }}</div>
    </div>
    <div class="card stat-card">
        <div class="stat-label">Overdue</div>
        <div class="stat-num" style="color:{{ $stats['overdue'] ? 'var(--mexco-danger)' : 'inherit' }}">{{ $stats['overdue'] }}</div>
    </div>
</div>

<div class="card">
    <div class="form-panel" style="padding-bottom:.75rem">
        <h2 style="font-size:1rem;margin:0 0 .35rem">Recent invoices</h2>
        <p class="panel-lead" style="margin:0">Draft invoices are created automatically when paid plans are assigned to events.</p>
    </div>
    <div class="tbl-wrap">
        <table class="data-tbl">
            <thead>
                <tr>
                    <th>Invoice</th>
                    <th>Organisation</th>
                    <th>Event</th>
                    <th>Total</th>
                    <th>Status</th>
                    <th style="text-align:right">Actions</th>
                </tr>
            </thead>
            <tbody>
                @forelse($recentInvoices as $inv)
                    @php
                        $pill = match($inv->status) {
                            'paid' => 'success',
                            'pending' => 'accent',
                            'cancelled', 'failed' => 'muted',
                            default => '',
                        };
                    @endphp
                    <tr>
                        <td class="mono">{{ $inv->invoice_number }}</td>
                        <td>{{ $inv->organisation?->name ?? '-' }}</td>
                        <td>
                            <div class="cell-name">{{ $inv->event?->name ?? '-' }}</div>
                            @if($inv->event_id)<div class="cell-sub mono">{{ $inv->event_id }}</div>@endif
                        </td>
                        <td>{{ $inv->formattedTotal() }}</td>
                        <td><span class="status-pill {{ $pill }}">{{ $inv->statusLabel() }}</span></td>
                        <td><a href="{{ route('mexco.super.invoices.show', $inv) }}" class="btn-outline btn-sm">View</a></td>
                    </tr>
                @empty
                    <tr><td colspan="6" class="text-center cell-sub py-4">No invoices yet. Create an event with a paid plan to generate one.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>
@endsection
