<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/** ADMIN DASHBOARD — Expects: $stats,$app_counts,$inq_counts,$revenue,
 *  $pending_apps,$new_inq,$recent_acts,$upcoming,$user. */
?>
<div class="page-head">
	<div>
		<h2>Welcome back, <?= html_escape(explode(' ', $user['name'])[0]) ?> 👋</h2>
		<p>Here's what's happening across Bloom Iris Agency.</p>
	</div>
	<a href="<?= base_url() ?>" target="_blank" class="btn btn-ghost"><?= bloom_icon('arrow', 16) ?> View Website</a>
</div>

<!-- STAT CARDS -->
<div class="stat-cards">
	<div class="stat-card">
		<div class="ico plum"><?= bloom_icon('grid', 24) ?></div>
		<div><div class="figure"><?= (int) $stats['portfolio'] ?></div><div class="label">Portfolio Projects</div></div>
	</div>
	<div class="stat-card">
		<div class="ico green"><?= bloom_icon('star', 24) ?></div>
		<div><div class="figure"><?= (int) $stats['talent'] ?></div><div class="label">Talent Roster</div></div>
	</div>
	<div class="stat-card">
		<div class="ico amber"><?= bloom_icon('calendar', 24) ?></div>
		<div><div class="figure"><?= (int) $stats['events'] ?></div><div class="label">Events</div></div>
	</div>
	<div class="stat-card">
		<div class="ico blue"><?= bloom_icon('ticket', 24) ?></div>
		<div><div class="figure"><?= money($revenue) ?></div><div class="label">Ticket Revenue</div></div>
	</div>
</div>

<div class="grid grid-2-1">
	<!-- LEFT: pending engagement -->
	<div>
		<!-- Pending applications -->
		<div class="card">
			<div class="card-head">
				<h3>Pending Talent Applications <?php if ($app_counts['pending']): ?><span class="pill pill-amber"><?= $app_counts['pending'] ?></span><?php endif; ?></h3>
				<a href="<?= base_url('admin/applications') ?>" class="btn btn-ghost btn-sm">View All</a>
			</div>
			<div class="card-body flush">
				<?php if ($pending_apps): ?>
					<div class="table-wrap">
						<table class="data">
							<thead><tr><th>Applicant</th><th>Category</th><th>Received</th><th></th></tr></thead>
							<tbody>
							<?php foreach ($pending_apps as $a): ?>
								<tr>
									<td>
										<div class="row-title"><?= html_escape($a['name']) ?></div>
										<div class="row-sub"><?= html_escape($a['email']) ?></div>
									</td>
									<td><?= html_escape(ucwords(str_replace('_', ' ', $a['category']))) ?></td>
									<td class="muted"><?= nice_date($a['created_at']) ?></td>
									<td class="text-right">
										<a href="<?= base_url('admin/applications/view/'.$a['id']) ?>" class="icon-btn"><?= bloom_icon('arrow', 16) ?></a>
									</td>
								</tr>
							<?php endforeach; ?>
							</tbody>
						</table>
					</div>
				<?php else: ?>
					<div class="empty"><?= bloom_icon('check', 44) ?><p>No pending applications. All caught up!</p></div>
				<?php endif; ?>
			</div>
		</div>

		<!-- New inquiries -->
		<div class="card">
			<div class="card-head">
				<h3>New Inquiries <?php if ($inq_counts['new']): ?><span class="pill pill-blue"><?= $inq_counts['new'] ?></span><?php endif; ?></h3>
				<a href="<?= base_url('admin/inquiries') ?>" class="btn btn-ghost btn-sm">View All</a>
			</div>
			<div class="card-body flush">
				<?php if ($new_inq): ?>
					<div class="table-wrap">
						<table class="data">
							<thead><tr><th>From</th><th>Type</th><th>Subject</th><th>Received</th></tr></thead>
							<tbody>
							<?php foreach ($new_inq as $q): ?>
								<tr onclick="location.href='<?= base_url('admin/inquiries/view/'.$q['id']) ?>'" style="cursor:pointer;">
									<td>
										<div class="row-title"><?= html_escape($q['name']) ?></div>
										<div class="row-sub"><?= html_escape($q['email']) ?></div>
									</td>
									<td><span class="pill <?= $q['type'] === 'brand' ? 'pill-amber' : 'pill-grey' ?>"><?= html_escape(ucfirst($q['type'])) ?></span></td>
									<td><?= html_escape(excerpt($q['subject'] ?: $q['message'], 8)) ?></td>
									<td class="muted"><?= nice_date($q['created_at']) ?></td>
								</tr>
							<?php endforeach; ?>
							</tbody>
						</table>
					</div>
				<?php else: ?>
					<div class="empty"><?= bloom_icon('check', 44) ?><p>No new inquiries.</p></div>
				<?php endif; ?>
			</div>
		</div>
	</div>

	<!-- RIGHT: activity + upcoming -->
	<div>
		<!-- Upcoming events -->
		<div class="card">
			<div class="card-head"><h3>Upcoming Events</h3></div>
			<div class="card-body">
				<?php if ($upcoming): ?>
					<?php foreach ($upcoming as $ev): ?>
						<div class="feed-item">
							<div class="dot"><?= bloom_icon('calendar', 16) ?></div>
							<div class="txt">
								<strong><?= html_escape($ev['title']) ?></strong><br>
								<span class="when"><?= nice_date($ev['event_date'], 'd M Y') ?> · <?= html_escape($ev['location']) ?></span>
							</div>
						</div>
					<?php endforeach; ?>
				<?php else: ?>
					<p class="muted" style="font-size:.86rem;">No upcoming events scheduled.</p>
				<?php endif; ?>
			</div>
		</div>

		<!-- Recent activity -->
		<div class="card">
			<div class="card-head"><h3>Recent Activity</h3></div>
			<div class="card-body">
				<?php if ($recent_acts): ?>
					<?php foreach ($recent_acts as $act): ?>
						<div class="feed-item">
							<div class="dot"><?= bloom_icon('sparkle', 16) ?></div>
							<div class="txt">
								<strong><?= html_escape($act['user_name'] ?: 'System') ?></strong>
								<?= html_escape(strtolower($act['action'])) ?>
								<?php if ($act['detail']): ?>— <?= html_escape($act['detail']) ?><?php endif; ?>
								<br><span class="when"><?= nice_date($act['created_at'], 'd M Y, H:i') ?></span>
							</div>
						</div>
					<?php endforeach; ?>
				<?php else: ?>
					<p class="muted" style="font-size:.86rem;">No activity recorded yet.</p>
				<?php endif; ?>
			</div>
		</div>
	</div>
</div>
