<?php
session_start();
if (!isset($_SESSION['usuario_id'])) {
	header('Location: index.php');
	exit;
}

require_once __DIR__ . '/../app/Core/Database.php';
require_once __DIR__ . '/../app/Controllers/ReporteController.php';

$db = (new Database())->conn;
$controlador = new ReporteController($db);
$reportes = $controlador->mostrarReportesPorUsuario($_SESSION['usuario_id']);
?>
<!DOCTYPE html>
<html lang="es">
<head>
	<meta charset="UTF-8">
	<title>Panel de Reportes</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
	<style>
		body { overflow: hidden; }
		.sidebar { height: 100vh; overflow-y: auto; background: #f8f9fa; }
		.content { height: calc(100vh - 56px); }
		iframe { width: 100%; height: 100%; border: none; }
	</style>
</head>
<body>
	<nav class="navbar navbar-expand-lg navbar-light bg-light px-3">
		<a class="navbar-brand" href="#">PowerBi</a>
		<div class="ms-auto dropdown">
			<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown">
				👤 <?= htmlspecialchars($_SESSION['username']) ?>
			</a>
			<ul class="dropdown-menu dropdown-menu-end">
				<li><a class="dropdown-item" href="#">Configuración</a></li>
				<li><a class="dropdown-item" href="logout.php">Cerrar sesión</a></li>
			</ul>
		</div>
	</nav>
	<div class="d-flex">
		<div class="sidebar p-3 col-2 border-end">
			<h5>Reportes</h5>
			<ul class="nav flex-column">
				<?php while($r = $reportes->fetch_assoc()): ?>
				<li class="nav-item">
					<a href="#" class="nav-link lh-1" onclick="cargarReporte('<?= htmlspecialchars($r['url']) ?>')">
						<?= htmlspecialchars($r['nombre']) ?>
					</a>
				</li>
				<?php endwhile; ?>
			</ul>
		</div>
		<div class="content col-10">
			<iframe id="visorReporte" src=""></iframe>
		</div>
	</div>

	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
	<script>
		function cargarReporte(url) {
			document.getElementById('visorReporte').src = url;
		}
	</script>
</body>
</html>
