<!DOCTYPE html>
<!-- Page: point of interest view -->
<html lang="fr">

	<?php include('blocks/d.head.html'); ?>

	<body>

		<?php include('blocks/d.nav.html'); ?>

		<?php
			$isArchiveOrPrivate = $poi->is_archive == "t" || $poi->is_public == "f";
			$canViewHistory = $user->rankIsHigher('premium');
			$canModerate = $user->rankIsHigher('moderator');
			$canWriteComment = $user->rankIsHigher("registered");
			$canOpenCommentForm = $canWriteComment && !$isArchiveOrPrivate;
			$hasHistory = isset($PoiHistory);
			$isVisitor = $user->rank == "visitor";
			$isCommentable = $poi->is_commentable == 't';
		?>

		<!-- Map canvas -->
		<div id="mapid"></div>

		<!-- POI details panel -->
		<div id="sticky">
			<section <?=$isArchiveOrPrivate ? 'class="archive"' : ''?>>
				<i id="slide-icon" class="fas fa-chevron-up"></i>

				<!-- POI header -->
				<h1 class="flex_line">
					<span class="poi-icon-wrapper" data-tooltip="<?=htmlspecialchars($poi_types[$poi->poi_type][4] ?? '')?>">
						<img class="poi-icon"
						     src="<?=$config['rel_root_folder']?>views/img/<?=$poi->poi_type?>.svg"
						     alt="">
					</span>
					<span><?=$poi->name?></span>
				</h1>

				<!-- POI card -->
				<div class="poi-idcard">

					<div class="poi-id-main">
						<div class="poi-id-coords">
							<span class="poi-id-label">📍 Coordonnées</span>
							<a href="geo:<?=$poi->lat?>,<?=$poi->lon?>" class="poi-id-value">
								<?=$poi->lat?>, <?=$poi->lon?>
							</a>
							<?php if ($poi->ele !== null && $poi->ele !== '') { ?>
								<span class="poi-id-elev">⛰️ <?=$poi->ele?> m</span>
							<?php } ?>
						</div>

						<div class="poi-id-type">
							<span class="poi-id-label">🏷️ Type</span>
							<span class="poi-id-value">
								<?=$poi_types[$poi->poi_type][1] ?? $poi->poi_type?>
							</span>
						</div>
					</div>

					<div class="poi-id-dates">
						<span>Créé : <?=$poi->creation_date?></span>
						<span>Mis à jour : <?=$poi->update_date?></span>
					</div>

				</div>

				<?php if ($canViewHistory) { ?>
					<!-- History and actions -->
					<span class="subtitle">
					<?php if ($hasHistory) { ?>
						<select id="PoiHistory">
							<?php $historyIndex = 0; ?>
							<?php foreach ($PoiHistory->objs as $row) { ?>
								<?php
									$isSelected = $row->version_id == $poi->version_id ? 'selected' : '';
									$isActive = $row->is_archive == "f" ? '&bull; ' : '';
									$formattedDate = datefmt_format(
										$user->datetimeshort_format,
										date_create($row->update_date, new DateTimeZone("UTC"))
									);
								?>
								<option <?=$isSelected?> value="<?=$historyIndex?>"><?=$isActive?><?=$formattedDate?></option>
								<?php $historyIndex++; ?>
							<?php } ?>
						</select>
					<?php }
					if ($canModerate && $hasHistory) { ?>
						 &mdash; 
					<?php }
					if ($canModerate) { ?>
						<a href="<?=$config['rel_root_folder']?>poi/<?=$poi->permalink?>/edit"><i class="fas fa-pencil-alt"></i> Éditer le point</a> &mdash; 
						<?php if ($poi->is_public == 't') { ?>
							<a href="<?=$config['rel_root_folder']?>poi/<?=$poi->permalink?>/delete"><i class="fas fa-trash"></i> Effacer le point</a>
						<?php } else { ?>
							<a href="<?=$config['rel_root_folder']?>poi/<?=$poi->permalink?>/restore"><i class="fas fa-eye"></i> Restaurer le point</a>
						<?php } ?>
					<?php } ?>
					</span>
				<?php } ?>

                <br>
                <br>

                <?php
                $poiFields = $poi_types[$poi->poi_type][5];
                $poiValues = $poi->parameters ?? [];

                // Séparer les booléens des autres champs
                $boolFields = [];
                $otherFields = [];

                foreach ($poiFields as $key => $label) {
                    if ($key[0] === 'b') {
                        $boolFields[$key] = $label;
                    } else {
                        $otherFields[$key] = $label;
                    }
                }
                ?>

				<div id="specific_form" class="view-mode">
					<!-- Detail fields -->
					<?php foreach ($otherFields as $key => $label): ?>
						<?php
							$value = $poiValues[$key] ?? null;
							$type = $key[0];
						?>

						<div class="field field-<?= $type ?>">
							<label><?= $label ?></label>

							<?php if ($type === 'n'): ?>
								<span class="inline-value"><?= $value !== null ? $value : "<em>—</em>" ?></span>

							<?php elseif ($type === 't'): ?>
								<div class="field-value">
									<?= $value ? nl2br(htmlspecialchars($value)) : "<em>Aucune information.</em>" ?>
								</div>

							<?php elseif ($type === 'l'): ?>
								<span class="inline-value">
									<?php if ($value): ?>
										<a href="<?= htmlspecialchars($value) ?>" target="_blank"><?= $value ?></a>
									<?php else: ?>
										<em>—</em>
									<?php endif; ?>
								</span>

							<?php endif; ?>
						</div>


					<?php endforeach; ?>

					<!-- Boolean grid -->
					<?php if (!empty($boolFields)): ?>
					<?php
						$boolIcons = [
							0 => "<span class='boolean-pill boolean-no'>❌</span>",
							1 => "<span class='boolean-pill boolean-maybe'>❓</span>",
							2 => "<span class='boolean-pill boolean-yes'>✔️</span>",
							-1 => "<span class='boolean-pill boolean-unknown'>➖</span>"
						];
					?>

						<div class="bool-grid">
						    <?php foreach ($boolFields as $key => $label): ?>
						        <?php $icon = $boolIcons[$poiValues[$key] ?? 2]; ?>
						        <div class="bool-cell">
						            <div class="bool-label"><?= $label ?></div>
						            <div class="bool-icon"><?= $icon ?></div>
						        </div>
						    <?php endforeach; ?>
						</div>
					<?php endif; ?>
				</div>


				<?php if ($isCommentable && $poi->source_id=='kab') { ?>
					<?php if (isset($poi_comments) && $poi_comments->number > 0) { ?>
						<div id="comments_photos_gallery" class="gallery">
							<?php foreach ($poi_comments->objs as $comment) { ?>
								<?php if (!empty($comment->photo) && $comment->is_archive != 't' && $comment->is_public != 'f') { ?>
						            <a href="<?=$config['rel_root_folder'].'medias/comment_photos/'.$comment->photo?>">
						            	<img src="<?=$config['rel_root_folder'].'medias/comment_photos/'.$comment->photo?>" title="<?= (mb_strlen($comment->comment) > 50 ? mb_substr($comment->comment, 0, 50) . '...' : $comment->comment) ?>">
						            </a>
								<?php } ?>
							<?php } ?>
						</div>

						<script type="text/javascript">
							var lightbox = new SimpleLightbox('.gallery a');
						</script>
					<?php } ?>

					<!-- Comments -->
					<div id="new_comment">
						<form class="form" action="<?=$config['rel_root_folder']?>poi/<?=$poi->permalink?>/new_comment" method="post" enctype="multipart/form-data">
							<div id="new_comment_label" <?=$isVisitor ? "class='sent' " : ""?>>
								<?php if ($isArchiveOrPrivate) { ?>
									<p>Impossible de commenter un point non publié</p>
								<?php } else if ($canWriteComment) { ?>
									<input name="submit" type="submit" value="Envoyer">
									<p>Ajouter un nouveau commentaire</p>
								<?php } else { ?>
									<p>Veuillez vous connecter pour ajouter un commentaire</p>
								<?php } ?>
							</div>
							<div id="new_comment_form">
								<textarea id="comment" name="comment" rows="5" placeholder="Votre commentaire"></textarea>

								<label for="photo" id="upload-btn">
								    <i class="fas fa-file-image"></i> Ajouter une photo
								</label>
								<input type="file" id="photo" name="photo" accept="image/*">
							</div>
						</form>
					</div>

					<script>
					document.getElementById('photo').addEventListener('change', function() {
					    document.getElementById('upload-btn').innerHTML = this.files.length
					        ? "<i class='fas fa-check-square'></i> Photo sélectionnée"
					        : "<i class='fas fa-file-image'></i> Ajouter une photo";
					});
					</script>

					<?php if (isset($poi_comments) && $poi_comments->number > 0) { ?>
						<!-- Comment list -->
						<div id="comments">
							<?php foreach ($poi_comments->objs as $comment) { ?>
								<?php $canManageComment = $canModerate || $user->id == $comment->author; ?>
								<article <?php if($comment->is_archive == 't' || $comment->is_public == 'f') echo 'class="comment_archive" '; ?>>
									<div class="comment_title">

										<?php if ($comment->author_obj->is_avatar_present == 't') { ?>
											<img alt="Avatar" class="icon avatar" src="<?=$config['rel_root_folder']?>medias/avatars/<?=$comment->author_obj->id?>_s.jpg">
										<?php } else { ?>
											<i class="icon fas fa-user-secret"></i>
										<?php } ?>

										<?php if ($user->rankIsHigher("blocked")) { ?>
											<a class="username" href="<?=$config['rel_root_folder']?>user/p/<?=$comment->author_obj->id?>">
												<?=$comment->author_obj->name?>
											</a>
										<?php } else { ?>
											<?=$comment->author_obj->name?>
										<?php } ?>

										le <?php echo datefmt_format($user->datetime_format, date_create($comment->update_date, new DateTimeZone("UTC"))) ?>

										<?php if ($canManageComment && $comment->is_public == 't') { ?>
											<span class="delete_link">
												<a href="<?=$config['rel_root_folder']?>poi/<?=$poi->permalink?>/delete_comment/<?=$comment->id?>">
													<i class="fas fa-trash"></i> Effacer le commentaire
												</a>
											</span>
										<?php } ?>

										<?php if ($canManageComment && $comment->is_public == 'f') { ?>
											<span class="delete_link">
												<a href="<?=$config['rel_root_folder']?>poi/<?=$poi->permalink?>/restore_comment/<?=$comment->id?>">
													<i class="fas fa-eye"></i> Restaurer le commentaire
												</a>
											</span>
										<?php } ?>

									</div>

									<div class="comment_content">
										<?=$comment->comment_html?>

									    <?php if (!empty($comment->photo)) { ?>
									        <div class="comment-photo">
									            <img src="<?=$config['rel_root_folder'].'medias/comment_photos/'.$comment->photo?>" alt="Photo du commentaire">
									        </div>
									    <?php } ?>
									</div>
								</article>

							<?php } ?>
						<br></div>
					<?php } else { ?>
						<p>Aucun commentaire pour le moment.</p>
					<?php } ?>
				<?php } ?>

				<?php if($poi->source_id!='kab') {?><br><div style="text-align: center; font-style: italic;">Données fournies par <a href="https://refuges.info/point/<?=$poi->remote_source_id?>" target="_blank"><i class="fas fa-external-link-alt"></i> <?=$poi->source?></a> sous licence CC BY-SA</div><?php } ?>

			</section>

			<?php include('blocks/d.footer.html'); ?>
		</div>

		<!-- Map variables -->
		<script>
			var poi_lat = <?=$poi->lat?>;
			var poi_lon = <?=$poi->lon?>;
			var poi_type = "<?=$poi->poi_type?>";
			var poi_mode = "view";
            var root = "<?=$config['rel_root_folder']?>";
		</script>

		<?php if ($canViewHistory) { ?>
		<!-- History selector -->
		<script type="text/javascript">
			$( "#PoiHistory" ).change(function() {
				window.location.href = "<?=$config['rel_root_folder']?>poi/<?=$poi->permalink?>/"+$( this ).val();
			});
		</script>
		<?php } ?>

		<?php if ($canOpenCommentForm) { ?>
		<!-- Comment form toggle -->
		<script type="text/javascript">
			$( "#new_comment_label" ).click(function() {
				$( "#new_comment_form" ).show(400);
				$( "#new_comment_label input" ).show(0);
				$( "#new_comment_label").addClass('sent');
			});
		</script>
		<?php } ?>

		<!-- Scroll helper -->
		<script type="text/javascript">
			$( "#slide-icon" ).click(function() {
				$( "html, body" ).animate({scrollTop: "300px"});
			});
		</script>

	</body>
</html>
