<link href="<?php echo base_url('assets/css/admin-panel.css'); ?>?v=20260827a" rel="stylesheet">

<?php
$p = $post;
$photos = isset($photos) && is_array($photos) ? $photos : (isset($p['images']) && is_array($p['images']) ? $p['images'] : array());
$comments = isset($comments) && is_array($comments) ? $comments : array();
$reports = isset($reports) && is_array($reports) ? $reports : array();
$stMap = array('visible' => 'ca-status--active', 'hidden' => 'ca-status--pending', 'removed' => 'ca-status--danger');
$stClass = isset($stMap[$p['status']]) ? $stMap[$p['status']] : 'ca-status--muted';
?>

<div class="care-admin">
	<div class="ca-hero">
		<div class="ca-hero-inner row align-items-center g-3">
			<div class="col-lg-8">
				<p class="ca-kicker">Content</p>
				<h1 class="ca-title">Post detail</h1>
				<p class="ca-sub mb-2"><code><?php echo htmlspecialchars($p['id']); ?></code></p>
				<span class="ca-status <?php echo $stClass; ?>"><?php echo htmlspecialchars(ucfirst($p['status'])); ?></span>
				<span class="ca-role-chip"><?php echo htmlspecialchars($p['contentType']); ?></span>
				<span class="ca-role-chip"><?php echo count($photos); ?> photos</span>
				<span class="ca-role-chip"><?php echo count($comments); ?> comments</span>
			</div>
			<div class="col-lg-4">
				<div class="ca-nav justify-content-lg-end flex-wrap">
					<a class="btn btn-light" href="<?php echo base_url('admin/posts'); ?>"><i class="fa-solid fa-arrow-left me-2"></i>Back</a>
					<?php if ($p['status'] !== 'hidden'): ?>
						<button type="button" class="btn btn-outline-warning" data-act="hidden">Hide</button>
					<?php endif; ?>
					<?php if ($p['status'] !== 'visible'): ?>
						<button type="button" class="btn btn-outline-success" data-act="visible">Restore</button>
					<?php endif; ?>
					<?php if ($p['status'] !== 'removed'): ?>
						<button type="button" class="btn btn-danger" data-act="removed">Remove</button>
					<?php endif; ?>
				</div>
			</div>
		</div>
	</div>

	<div class="row g-3">
		<div class="col-lg-5">
			<div class="ca-card mb-3">
				<div class="ca-card-header d-flex justify-content-between align-items-center">
					<h5 class="ca-card-title mb-0">Photos</h5>
					<span class="text-muted small"><?php echo count($photos); ?></span>
				</div>
				<div class="ca-card-body">
					<?php if (empty($photos)): ?>
						<div class="tu-empty"><i class="fa-regular fa-image"></i><div class="fw-semibold">No photos on this post</div></div>
					<?php else: ?>
						<div class="row g-2">
							<?php foreach ($photos as $img): ?>
								<div class="col-6">
									<a href="<?php echo htmlspecialchars($img); ?>" target="_blank" rel="noopener">
										<img src="<?php echo htmlspecialchars($img); ?>" alt="" class="img-fluid rounded-3" style="height: 160px; width: 100%; object-fit: cover;">
									</a>
								</div>
							<?php endforeach; ?>
						</div>
					<?php endif; ?>
				</div>
			</div>
			<div class="ca-card">
				<div class="ca-card-header"><h5 class="ca-card-title mb-0">Details</h5></div>
				<div class="ca-card-body">
					<p class="mb-2"><span class="text-muted small d-block">User</span>
						<?php if ($p['authorUid'] !== ''): ?>
							<a href="<?php echo base_url('admin/user_view/' . rawurlencode($p['authorUid'])); ?>"><?php echo htmlspecialchars($p['authorName'] !== '' ? $p['authorName'] : $p['authorUid']); ?></a>
						<?php else: ?>
							<?php echo htmlspecialchars($p['authorName'] !== '' ? $p['authorName'] : '—'); ?>
						<?php endif; ?>
					</p>
					<p class="mb-2"><span class="text-muted small d-block">Caption</span><?php echo $p['caption'] !== '' ? nl2br(htmlspecialchars($p['caption'])) : '—'; ?></p>
					<p class="mb-2"><span class="text-muted small d-block">Privacy</span><?php echo htmlspecialchars($p['privacy'] !== '' ? $p['privacy'] : '—'); ?></p>
					<p class="mb-2"><span class="text-muted small d-block">Place</span><?php echo htmlspecialchars($p['placeLabel']); ?></p>
					<p class="mb-2"><span class="text-muted small d-block">Created</span><?php echo htmlspecialchars($p['createdAtLabel']); ?></p>
					<p class="mb-2"><span class="text-muted small d-block">Likes</span><?php echo (int) $p['likesCount']; ?></p>
					<p class="mb-0"><span class="text-muted small d-block">Topics</span>
						<?php if (empty($p['topics'])): ?>—<?php else: foreach ($p['topics'] as $t): ?>
							<span class="ca-role-chip"><?php echo htmlspecialchars($t); ?></span>
						<?php endforeach; endif; ?>
					</p>
				</div>
			</div>
		</div>
		<div class="col-lg-7">
			<div class="ca-card mb-3">
				<div class="ca-card-header d-flex justify-content-between align-items-center">
					<h5 class="ca-card-title mb-0">Comments</h5>
					<span class="text-muted small"><?php echo count($comments); ?></span>
				</div>
				<div class="ca-card-body p-0">
					<?php if (empty($comments)): ?>
						<div class="tu-empty">
							<i class="fa-regular fa-comments"></i>
							<div class="fw-semibold">No comments yet</div>
							<div class="small">Comments on this post will appear here.</div>
						</div>
					<?php else: ?>
						<div class="ca-table-wrap border-0 rounded-0 border-top">
							<table class="table ca-table mb-0 align-middle">
								<thead>
									<tr>
										<th>User</th>
										<th>Comment</th>
										<th class="text-end">When</th>
									</tr>
								</thead>
								<tbody>
									<?php foreach ($comments as $c): ?>
										<tr>
											<td class="fw-semibold">
												<?php if ($c['authorUid'] !== ''): ?>
													<a href="<?php echo base_url('admin/user_view/' . rawurlencode($c['authorUid'])); ?>"><?php echo htmlspecialchars($c['authorName'] !== '' ? $c['authorName'] : $c['authorUid']); ?></a>
												<?php else: ?>
													<?php echo htmlspecialchars($c['authorName'] !== '' ? $c['authorName'] : '—'); ?>
												<?php endif; ?>
											</td>
											<td><?php echo $c['text'] !== '' ? nl2br(htmlspecialchars($c['text'])) : '—'; ?></td>
											<td class="small text-muted text-end"><?php echo htmlspecialchars($c['createdAtLabel']); ?></td>
										</tr>
									<?php endforeach; ?>
								</tbody>
							</table>
						</div>
					<?php endif; ?>
				</div>
			</div>
			<div class="ca-card">
				<div class="ca-card-header"><h5 class="ca-card-title mb-0">Associated reports</h5></div>
				<div class="ca-card-body p-0">
					<?php if (empty($reports)): ?>
						<div class="tu-empty"><i class="fa-regular fa-flag"></i><div class="fw-semibold">No reports on this post</div></div>
					<?php else: ?>
						<div class="ca-table-wrap border-0 rounded-0 border-top">
							<table class="table ca-table mb-0">
								<thead><tr><th>Reason</th><th>Reporter</th><th>Status</th><th>When</th></tr></thead>
								<tbody>
									<?php foreach ($reports as $r): ?>
										<tr>
											<td><?php echo htmlspecialchars($r['reason'] !== '' ? $r['reason'] : 'Report'); ?></td>
											<td><?php echo htmlspecialchars($r['reporter'] !== '' ? $r['reporter'] : '—'); ?></td>
											<td><?php echo htmlspecialchars(ucfirst(str_replace('_', ' ', $r['status']))); ?></td>
											<td class="small text-muted"><?php echo htmlspecialchars($r['createdAtLabel']); ?></td>
										</tr>
									<?php endforeach; ?>
								</tbody>
							</table>
						</div>
					<?php endif; ?>
				</div>
			</div>
		</div>
	</div>
</div>

<script>
(function () {
	var id = <?php echo json_encode($p['docId']); ?>;
	document.querySelectorAll('[data-act]').forEach(function (btn) {
		btn.addEventListener('click', function () {
			var st = btn.getAttribute('data-act');
			if (!confirm('Set this post to ' + st + '?')) return;
			var body = new FormData();
			body.append('status', st);
			fetch(<?php echo json_encode(base_url('admin/post_status/')); ?> + encodeURIComponent(id), {
				method: 'POST', headers: { 'Accept': 'application/json' }, body: body
			}).then(function (r) { return r.json(); }).then(function (j) {
				if (!j || !j.ok) throw new Error((j && j.error) || 'Failed');
				window.location.reload();
			}).catch(function (e) { alert(e.message); });
		});
	});
})();
</script>
