<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/** EVENT ATTENDEES — Expects: $event, $rsvps, $tickets. */
$paid_tickets = array_filter($tickets, function ($t) { return $t['payment_status'] === 'paid'; });
?>
<div class="page-head">
	<div><h2>Attendees</h2><p><?= html_escape($event['title']) ?> · <?= nice_date($event['event_date'], 'd M Y') ?></p></div>
	<a href="<?= base_url('admin/events') ?>" class="btn btn-ghost">← Back to Events</a>
</div>

<div class="stat-cards" style="grid-template-columns:repeat(3,1fr);">
	<div class="stat-card">
		<div class="ico blue"><?= bloom_icon('users', 24) ?></div>
		<div><div class="figure"><?= count($rsvps) ?></div><div class="label">Free RSVPs</div></div>
	</div>
	<div class="stat-card">
		<div class="ico amber"><?= bloom_icon('ticket', 24) ?></div>
		<div><div class="figure"><?= count($paid_tickets) ?></div><div class="label">Tickets Sold</div></div>
	</div>
	<div class="stat-card">
		<div class="ico green"><?= bloom_icon('tag', 24) ?></div>
		<div><div class="figure"><?= money(array_sum(array_column($paid_tickets, 'amount'))) ?></div><div class="label">Revenue</div></div>
	</div>
</div>

<?php if ($event['is_paid']): ?>
<div class="card">
	<div class="card-head"><h3>Ticket Purchases</h3></div>
	<div class="card-body flush">
		<?php if ($tickets): ?>
			<div class="table-wrap">
				<table class="data">
					<thead><tr><th>Buyer</th><th>Contact</th><th>Qty</th><th>Amount</th><th>Code</th><th>Status</th><th>Date</th></tr></thead>
					<tbody>
					<?php foreach ($tickets as $t): ?>
						<tr>
							<td class="row-title"><?= html_escape($t['name']) ?></td>
							<td class="row-sub"><?= html_escape($t['email']) ?><br><?= html_escape($t['phone']) ?></td>
							<td><?= (int) $t['quantity'] ?></td>
							<td><?= money($t['amount']) ?></td>
							<td><code><?= html_escape($t['ticket_code']) ?></code></td>
							<td><?= status_pill($t['payment_status'] === 'paid' ? 'published' : 'pending') ?></td>
							<td class="muted"><?= nice_date($t['created_at']) ?></td>
						</tr>
					<?php endforeach; ?>
					</tbody>
				</table>
			</div>
		<?php else: ?>
			<div class="empty"><?= bloom_icon('ticket', 44) ?><p>No tickets purchased yet.</p></div>
		<?php endif; ?>
	</div>
</div>
<?php endif; ?>

<?php if ($event['allow_rsvp']): ?>
<div class="card">
	<div class="card-head"><h3>Free RSVPs</h3></div>
	<div class="card-body flush">
		<?php if ($rsvps): ?>
			<div class="table-wrap">
				<table class="data">
					<thead><tr><th>Name</th><th>Contact</th><th>Guests</th><th>Date</th></tr></thead>
					<tbody>
					<?php foreach ($rsvps as $r): ?>
						<tr>
							<td class="row-title"><?= html_escape($r['name']) ?></td>
							<td class="row-sub"><?= html_escape($r['email']) ?><br><?= html_escape($r['phone']) ?></td>
							<td><?= (int) $r['guests'] ?></td>
							<td class="muted"><?= nice_date($r['created_at']) ?></td>
						</tr>
					<?php endforeach; ?>
					</tbody>
				</table>
			</div>
		<?php else: ?>
			<div class="empty"><?= bloom_icon('users', 44) ?><p>No RSVPs yet.</p></div>
		<?php endif; ?>
	</div>
</div>
<?php endif; ?>
