<?php $__env->startSection('title', 'Events & access'); ?>

<?php $__env->startPush('styles'); ?>
<style>
    .ev-head { margin-bottom: 1.25rem; }
    .ev-head .kicker {
        font-size: .68rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase;
        color: var(--mexco-primary); margin-bottom: .3rem;
    }

    .ev-kpi { display: grid; grid-template-columns: repeat(5, 1fr); gap: .85rem; margin-bottom: 1.15rem; }
    @media (max-width: 1100px) { .ev-kpi { grid-template-columns: repeat(2, 1fr); } }
    @media (max-width: 560px) { .ev-kpi { grid-template-columns: 1fr; } }
    .ev-kpi-card { padding: 1rem 1.1rem; }
    .ev-kpi-label { font-size: .72rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
    .ev-kpi-num { font-size: 1.45rem; font-weight: 800; margin-top: .2rem; line-height: 1.1; }

    .ev-toolbar {
        display: flex; flex-wrap: wrap; gap: .75rem; align-items: center; justify-content: space-between;
        padding: 1rem 1.15rem; border-bottom: 1px solid var(--border); background: #FAFBFC;
    }
    .ev-filters { display: flex; flex-wrap: wrap; gap: .65rem; align-items: center; }
    .ev-filter-group { display: flex; flex-wrap: wrap; gap: .35rem; align-items: center; }
    .ev-filter-label {
        font-size: .68rem; font-weight: 800; letter-spacing: .05em; text-transform: uppercase;
        color: #94A3B8; margin-right: .1rem;
    }
    .ev-filter {
        border: 1px solid var(--border); background: #fff; color: var(--muted); border-radius: 999px;
        padding: .34rem .75rem; font-size: .76rem; font-weight: 700; cursor: pointer;
    }
    .ev-filter:hover { background: #F8FAFC; color: var(--ink); }
    .ev-filter.on { background: var(--primary-soft); border-color: #DDD6FE; color: var(--mexco-primary-dark); }
    .ev-search {
        min-width: min(260px, 100%); padding: .52rem .9rem .52rem 2.3rem; border: 1px solid var(--border);
        border-radius: 10px; font-size: .85rem;
        background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' stroke='%2394A3B8' stroke-width='2' viewBox='0 0 24 24'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='M20 20l-3-3'/%3E%3C/svg%3E") .7rem 50% / 16px no-repeat;
    }
    .ev-search:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(124,58,237,.12); }

    .ev-list { display: flex; flex-direction: column; }
    .ev-row {
        display: grid;
        grid-template-columns: minmax(200px, 1.2fr) minmax(140px, .75fr) minmax(120px, .65fr) minmax(140px, .8fr) minmax(100px, .55fr) auto;
        gap: 1rem; align-items: center;
        padding: 1rem 1.2rem; border-bottom: 1px solid #F1F5F9; transition: background .12s;
    }
    .ev-row:hover { background: #FAFBFF; }
    .ev-row:last-child { border-bottom: 0; }
    @media (max-width: 1200px) {
        .ev-row { grid-template-columns: 1fr 1fr; gap: .75rem; }
        .ev-row .ev-actions { grid-column: 1 / -1; }
    }
    @media (max-width: 640px) {
        .ev-row { grid-template-columns: 1fr; }
    }

    .ev-id {
        display: inline-block; font-family: ui-monospace, monospace; font-size: .7rem; font-weight: 700;
        color: var(--mexco-primary); background: var(--primary-soft); border: 1px solid #DDD6FE;
        border-radius: 8px; padding: .18rem .45rem; margin-bottom: .3rem;
    }
    .ev-name { margin: 0; font-size: .95rem; font-weight: 800; line-height: 1.25; color: var(--ink); }
    .ev-sub { margin: .25rem 0 0; font-size: .78rem; color: var(--muted); line-height: 1.35; }

    .ev-avail {
        display: inline-flex; align-items: center; gap: .3rem; margin-top: .35rem;
        padding: .2rem .55rem; border-radius: 999px; font-size: .68rem; font-weight: 800;
    }
    .ev-avail::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
    .ev-avail.live { background: #ECFDF5; color: var(--mexco-success); }
    .ev-avail.upcoming { background: #E3F2FD; color: var(--mexco-secondary); }
    .ev-avail.ended { background: #F1F5F9; color: var(--muted); }
    .ev-avail.open { background: var(--primary-soft); color: var(--mexco-primary-dark); }

    .plan-pill {
        display: inline-block; padding: .22rem .55rem; border-radius: 6px;
        font-size: .72rem; font-weight: 700; background: var(--primary-soft); color: var(--mexco-primary-dark);
    }
    .plan-pill.campus { background: #ECFDF5; color: var(--mexco-success); }
    .plan-pill.starter { background: #F1F5F9; color: var(--muted); }
    .plan-pill.plus { background: #E0F2FE; color: #0369A1; }
    .plan-pill.pro { background: var(--primary-soft); color: var(--mexco-primary-dark); }
    .plan-pill.enterprise { background: #FEF3C7; color: #B45309; }

    .bill-pill {
        display: inline-block; padding: .2rem .5rem; border-radius: 6px;
        font-size: .7rem; font-weight: 700;
    }
    .bill-pill.paid { background: #ECFDF5; color: var(--mexco-success); }
    .bill-pill.pending { background: #FEF3C7; color: #B45309; }
    .bill-pill.draft { background: #F1F5F9; color: var(--muted); }
    .bill-pill.sponsored { background: #EDE9FE; color: var(--mexco-primary-dark); }

    .usage-bar { margin-top: .35rem; }
    .usage-bar-track { background: #F1F5F9; border-radius: 99px; height: 6px; overflow: hidden; }
    .usage-bar-fill { height: 100%; background: var(--primary); border-radius: 99px; }
    .usage-bar-fill.warn80 { background: var(--mexco-warning); }
    .usage-bar-fill.warn90 { background: #F97316; }
    .usage-bar-fill.limit100 { background: var(--mexco-danger); }
    .usage-bar-label { font-size: .72rem; color: var(--muted); margin-top: .2rem; }

    .ev-actions { display: flex; flex-wrap: wrap; gap: .4rem; justify-content: flex-end; }
    .ev-empty { text-align: center; padding: 3rem 1.5rem; color: var(--muted); }
    .ev-empty strong { display: block; color: var(--ink); font-size: 1.05rem; margin-bottom: .35rem; }
    .ev-hidden { display: none !important; }

    .ev-col-label {
        font-size: .65rem; font-weight: 800; letter-spacing: .05em; text-transform: uppercase;
        color: #94A3B8; margin-bottom: .2rem; display: none;
    }
    @media (max-width: 1200px) { .ev-col-label { display: block; } }
</style>
<?php $__env->stopPush(); ?>

<?php $__env->startSection('content'); ?>
<?php
    $meetup = app(\App\Services\MeetupService::class);
    $counts = ['all' => $events->count(), 'live' => 0, 'upcoming' => 0, 'ended' => 0, 'open' => 0];
    $planCounts = [];
    $orgIds = [];

    $rows = $events->map(function ($event) use ($meetup, $invoices, $usageSummaries, &$counts, &$planCounts, &$orgIds) {
        $status = $meetup->eventAvailability($event);
        $counts[$status] = ($counts[$status] ?? 0) + 1;

        $planCode = $event->license?->plan?->code ?? 'none';
        $planCounts[$planCode] = ($planCounts[$planCode] ?? 0) + 1;

        if ($event->organisation_id) {
            $orgIds[$event->organisation_id] = true;
        }

        $invoice = $invoices[$event->id] ?? null;
        if ($event->license?->is_campus_sponsored) {
            $billing = 'sponsored';
            $billingLabel = 'Campus';
        } elseif ($invoice) {
            $billing = $invoice->status;
            $billingLabel = $invoice->statusLabel();
        } elseif (($event->license?->price_cents ?? 0) <= 0) {
            $billing = 'sponsored';
            $billingLabel = 'Free';
        } else {
            $billing = 'none';
            $billingLabel = 'No invoice';
        }

        $usage = $usageSummaries[$event->id]['participants'] ?? ['current' => 0, 'limit' => null, 'percent' => null, 'level' => null];

        return [
            'event' => $event,
            'status' => $status,
            'statusLabel' => match ($status) {
                'upcoming' => 'Upcoming',
                'live' => 'Live',
                'ended' => 'Ended',
                default => 'Open',
            },
            'planCode' => $planCode,
            'planName' => $event->license?->plan?->name ?? '-',
            'billing' => $billing,
            'billingLabel' => $billingLabel,
            'invoice' => $invoice,
            'usage' => $usage,
            'schedule' => $meetup->eventDurationLabel($event),
            'summary' => $summaries[$event->id] ?? ['participants' => 0, 'sessions' => 0, 'booths' => 0],
        ];
    });
?>

<div class="pg-head ev-head">
    <div>
        <div class="kicker">Event licensing</div>
        <h1>Events &amp; access</h1>
        <p>Each event has its own plan, organisation, billing, and organiser login, independent from other events under the same institution.</p>
    </div>
    <div class="pg-actions">
        <a href="<?php echo e(route('mexco.super.organisations')); ?>" class="btn-outline">Organisations</a>
        <a href="<?php echo e(route('mexco.super.events.create')); ?>" class="btn-purple">+ Create event</a>
    </div>
</div>

<div class="ev-kpi">
    <div class="card ev-kpi-card">
        <div class="ev-kpi-label">Total events</div>
        <div class="ev-kpi-num"><?php echo e($counts['all']); ?></div>
    </div>
    <div class="card ev-kpi-card">
        <div class="ev-kpi-label">Live now</div>
        <div class="ev-kpi-num" style="color:var(--mexco-success)"><?php echo e($counts['live']); ?></div>
    </div>
    <div class="card ev-kpi-card">
        <div class="ev-kpi-label">Upcoming</div>
        <div class="ev-kpi-num" style="color:var(--mexco-secondary)"><?php echo e($counts['upcoming']); ?></div>
    </div>
    <div class="card ev-kpi-card">
        <div class="ev-kpi-label">Organisations</div>
        <div class="ev-kpi-num"><?php echo e(count($orgIds)); ?></div>
    </div>
    <div class="card ev-kpi-card">
        <div class="ev-kpi-label">Ended</div>
        <div class="ev-kpi-num" style="color:var(--muted)"><?php echo e($counts['ended']); ?></div>
    </div>
</div>

<div class="card" style="padding:0;overflow:hidden">
    <div class="ev-toolbar">
        <div class="ev-filters">
            <div class="ev-filter-group" id="evStatusFilters">
                <span class="ev-filter-label">Status</span>
                <button type="button" class="ev-filter on" data-status="all">All</button>
                <button type="button" class="ev-filter" data-status="live">Live</button>
                <button type="button" class="ev-filter" data-status="upcoming">Upcoming</button>
                <button type="button" class="ev-filter" data-status="ended">Ended</button>
                <button type="button" class="ev-filter" data-status="open">Open</button>
            </div>
            <div class="ev-filter-group" id="evPlanFilters">
                <span class="ev-filter-label">Plan</span>
                <button type="button" class="ev-filter on" data-plan="all">All</button>
                <?php $__currentLoopData = $planCounts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $code => $count): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <?php if($code !== 'none'): ?>
                        <button type="button" class="ev-filter" data-plan="<?php echo e($code); ?>"><?php echo e(ucfirst($code)); ?> (<?php echo e($count); ?>)</button>
                    <?php endif; ?>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </div>
            <div class="ev-filter-group" id="evBillFilters">
                <span class="ev-filter-label">Billing</span>
                <button type="button" class="ev-filter on" data-bill="all">All</button>
                <button type="button" class="ev-filter" data-bill="paid">Paid</button>
                <button type="button" class="ev-filter" data-bill="pending">Pending</button>
                <button type="button" class="ev-filter" data-bill="draft">Draft</button>
                <button type="button" class="ev-filter" data-bill="sponsored">Sponsored</button>
            </div>
        </div>
        <input type="search" class="ev-search" id="evSearch" placeholder="Search event, org, organiser…" autocomplete="off">
    </div>

    <div class="ev-list" id="evList">
        <?php $__empty_1 = true; $__currentLoopData = $rows; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
            <?php
                $event = $row['event'];
                $u = $row['usage'];
                $usagePct = $u['percent'] ?? 0;
                $usageClass = $u['level'] ?? '';
                $search = strtolower(implode(' ', array_filter([
                    $event->id,
                    $event->name,
                    $event->organisation?->name,
                    $event->organiser_username,
                    $row['planName'],
                    $row['planCode'],
                    $row['statusLabel'],
                    $row['billingLabel'],
                ])));
            ?>
            <article class="ev-row"
                     data-status="<?php echo e($row['status']); ?>"
                     data-plan="<?php echo e($row['planCode']); ?>"
                     data-bill="<?php echo e($row['billing']); ?>"
                     data-search="<?php echo e(e($search)); ?>">
                <div>
                    <span class="ev-id"><?php echo e($event->id); ?></span>
                    <h2 class="ev-name"><?php echo e($event->name); ?></h2>
                    <p class="ev-sub"><?php echo e($row['schedule']); ?></p>
                    <span class="ev-avail <?php echo e($row['status']); ?>"><?php echo e($row['statusLabel']); ?></span>
                </div>

                <div>
                    <div class="ev-col-label">Organisation</div>
                    <div class="cell-name" style="font-size:.875rem"><?php echo e($event->organisation?->name ?? '-'); ?></div>
                    <p class="ev-sub mono"><?php echo e($event->organiser_username ?: 'No organiser'); ?></p>
                </div>

                <div>
                    <div class="ev-col-label">Plan</div>
                    <?php if($row['planCode'] !== 'none'): ?>
                        <span class="plan-pill <?php echo e($row['planCode']); ?>"><?php echo e($row['planName']); ?></span>
                    <?php else: ?>
                        <span class="ev-sub">No license</span>
                    <?php endif; ?>
                    <?php if($event->license?->is_campus_sponsored): ?>
                        <div class="ev-sub" style="margin-top:.25rem">Campus sponsored</div>
                    <?php endif; ?>
                </div>

                <div>
                    <div class="ev-col-label">Usage</div>
                    <?php if($u['limit']): ?>
                        <div class="usage-bar">
                            <div class="usage-bar-track">
                                <div class="usage-bar-fill <?php echo e($usageClass); ?>" style="width:<?php echo e(min(100, $usagePct)); ?>%"></div>
                            </div>
                            <div class="usage-bar-label"><?php echo e(number_format($u['current'])); ?> / <?php echo e(number_format($u['limit'])); ?> participants</div>
                        </div>
                    <?php else: ?>
                        <div class="ev-sub"><?php echo e(number_format($u['current'])); ?> participants · unlimited</div>
                    <?php endif; ?>
                    <div class="ev-sub" style="margin-top:.2rem">
                        <?php echo e($row['summary']['sessions'] ?? 0); ?> sessions · <?php echo e($row['summary']['booths'] ?? 0); ?> booths
                    </div>
                </div>

                <div>
                    <div class="ev-col-label">Billing</div>
                    <span class="bill-pill <?php echo e(in_array($row['billing'], ['paid','pending','draft']) ? $row['billing'] : ($row['billing'] === 'sponsored' ? 'sponsored' : 'draft')); ?>">
                        <?php echo e($row['billingLabel']); ?>

                    </span>
                    <?php if($row['invoice']): ?>
                        <div class="ev-sub mono" style="margin-top:.25rem"><?php echo e($row['invoice']->invoice_number); ?></div>
                    <?php endif; ?>
                </div>

                <div class="ev-actions">
                    <a href="<?php echo e(route('mexco.super.events.edit', $event->id)); ?>" class="btn-purple btn-sm">Manage</a>
                    <form method="POST" action="<?php echo e(route('mexco.super.events.organiser', $event->id)); ?>">
                        <?php echo csrf_field(); ?>
                        <button type="submit" class="btn-outline btn-sm">Organiser</button>
                    </form>
                    <?php if($row['invoice']): ?>
                        <a href="<?php echo e(route('mexco.super.invoices.show', $row['invoice'])); ?>" class="btn-outline btn-sm">Invoice</a>
                    <?php endif; ?>
                    <form method="POST" action="<?php echo e(route('mexco.super.events.destroy', $event->id)); ?>"
                          onsubmit="return confirm('Delete this event and all its data? This cannot be undone.')">
                        <?php echo csrf_field(); ?> <?php echo method_field('DELETE'); ?>
                        <button type="submit" class="btn-danger btn-sm">Delete</button>
                    </form>
                </div>
            </article>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
            <div class="ev-empty">
                <strong>No events yet</strong>
                Create an event, assign an organisation and plan, then provision organiser access.
                <div style="margin-top:1rem">
                    <a href="<?php echo e(route('mexco.super.events.create')); ?>" class="btn-purple">+ Create event</a>
                </div>
            </div>
        <?php endif; ?>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
document.addEventListener('DOMContentLoaded', function () {
    const statusFilters = document.getElementById('evStatusFilters');
    const planFilters = document.getElementById('evPlanFilters');
    const billFilters = document.getElementById('evBillFilters');
    const search = document.getElementById('evSearch');
    const rows = Array.from(document.querySelectorAll('#evList .ev-row'));
    let status = 'all', plan = 'all', bill = 'all';

    function apply() {
        const q = (search?.value || '').trim().toLowerCase();
        rows.forEach(row => {
            const statusOk = status === 'all' || row.dataset.status === status;
            const planOk = plan === 'all' || row.dataset.plan === plan;
            const billOk = bill === 'all' || row.dataset.bill === bill;
            const searchOk = !q || (row.dataset.search || '').includes(q);
            row.classList.toggle('ev-hidden', !(statusOk && planOk && billOk && searchOk));
        });
    }

    function bindGroup(group, attr, setter) {
        group?.addEventListener('click', e => {
            const btn = e.target.closest(`[${attr}]`);
            if (!btn) return;
            setter(btn.getAttribute(attr));
            group.querySelectorAll('.ev-filter').forEach(b => b.classList.toggle('on', b === btn));
            apply();
        });
    }

    bindGroup(statusFilters, 'data-status', v => { status = v; });
    bindGroup(planFilters, 'data-plan', v => { plan = v; });
    bindGroup(billFilters, 'data-bill', v => { bill = v; });
    search?.addEventListener('input', apply);
});
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('super-admin.layout', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\laragon\www\myapp\resources\views/super-admin/events/index.blade.php ENDPATH**/ ?>