@extends('super-admin.layout')

@section('title', $organisation->name)

@section('content')
<div class="pg-head">
    <div>
        <h1>{{ $organisation->name }}</h1>
        <p class="cell-sub mono">{{ $organisation->slug }} · {{ \App\Models\MexcoOrganisation::types()[$organisation->type] ?? $organisation->type }}</p>
    </div>
    <div class="pg-actions">
        <a href="{{ route('mexco.super.organisers.create') }}" class="btn-outline">+ Add organiser</a>
        <a href="{{ route('mexco.super.organisations.edit', $organisation) }}" class="btn-purple">Edit</a>
    </div>
</div>

<div class="stat-row">
    <div class="card stat-card"><div class="stat-label">Total events</div><div class="stat-num">{{ $stats['total_events'] }}</div></div>
    <div class="card stat-card"><div class="stat-label">Active events</div><div class="stat-num">{{ $stats['active_events'] }}</div></div>
    <div class="card stat-card"><div class="stat-label">Participants</div><div class="stat-num">{{ number_format($stats['total_participants']) }}</div></div>
    <div class="card stat-card"><div class="stat-label">Organisers</div><div class="stat-num">{{ $stats['total_organisers'] }}</div></div>
</div>

<div class="card">
    <div style="padding:1rem 1.2rem;border-bottom:1px solid var(--border);font-weight:700">Events</div>
    <div class="tbl-wrap">
        <table class="data-tbl">
            <thead>
                <tr>
                    <th>Event</th>
                    <th>Plan</th>
                    <th>Participants</th>
                    <th>Status</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @forelse($stats['events'] as $event)
                    <tr>
                        <td>
                            <div class="cell-name">{{ $event->name }}</div>
                            <div class="cell-sub mono">{{ $event->id }}</div>
                        </td>
                        <td>{{ $event->license?->plan?->name ?? '-' }}</td>
                        <td>{{ $event->usage?->participants_count ?? 0 }}</td>
                        <td><span class="status-pill {{ $event->platform_status === 'active' ? 'success' : 'muted' }}">{{ ucfirst($event->platform_status) }}</span></td>
                        <td><a href="{{ route('mexco.super.events.edit', $event->id) }}" class="btn-outline btn-sm">Manage</a></td>
                    </tr>
                @empty
                    <tr><td colspan="5" class="text-center cell-sub py-4">No events linked yet.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>

<div class="card section-card" style="margin-top:1rem">
    <div style="padding:1rem 1.2rem;border-bottom:1px solid var(--border);font-weight:700">Billing credit</div>
    <div class="form-panel">
        <p class="panel-lead">Available credit: <strong>MYR {{ number_format($availableCredit / 100, 2) }}</strong>. Applies to future event invoices.</p>
        <form method="POST" action="{{ route('mexco.super.organisations.credits.store', $organisation) }}" class="d-flex flex-wrap gap-2 align-items-end">
            @csrf
            <div class="form-row" style="margin:0">
                <label class="form-label">Add credit (sen)</label>
                <input type="number" name="amount_cents" class="form-input" style="max-width:160px" min="100" placeholder="10000" required>
            </div>
            <div class="form-row" style="margin:0;flex:1;min-width:200px">
                <label class="form-label">Notes</label>
                <input type="text" name="notes" class="form-input" placeholder="Refund, goodwill, etc.">
            </div>
            <button type="submit" class="btn-outline">Add credit</button>
        </form>
    </div>
</div>

@if($organisation->status === 'active')
    <form method="POST" action="{{ route('mexco.super.organisations.suspend', $organisation) }}" class="mt-3">
        @csrf
        <button type="submit" class="btn-danger btn-sm" onclick="return confirm('Suspend this organisation?')">Suspend organisation</button>
    </form>
@else
    <form method="POST" action="{{ route('mexco.super.organisations.activate', $organisation) }}" class="mt-3">
        @csrf
        <button type="submit" class="btn-outline btn-sm">Activate organisation</button>
    </form>
@endif
@endsection
