<?php
session_start();

if (!isset($_SESSION['usuario'])) {
    header("Location: ../../Controlador/Utilidades/Sesion_Destroy.php");
    exit;
}

require_once '../../Controlador/Utilidades/Conexion_BD.php';

$conn = new Conexion_BD();
$mysqli = $conn->getConnection();

// Total Vehículos
$totalVehiculos = 0;
$sqlVehiculos = "SELECT COUNT(*) as total FROM vehiculos WHERE estado = 'activo'";
$resultVehiculos = $mysqli->query($sqlVehiculos);
if ($resultVehiculos && $row = $resultVehiculos->fetch_assoc()) {
    $totalVehiculos = $row['total'];
}

// Total Choferes
$totalChoferes = 0;
$sqlChoferes = "SELECT COUNT(*) as total FROM choferes WHERE estado = 'activo'";
$resultChoferes = $mysqli->query($sqlChoferes);
if ($resultChoferes && $row = $resultChoferes->fetch_assoc()) {
    $totalChoferes = $row['total'];
}

$conn->closeConnection();

$nombre = $_SESSION['nombre'];
?>

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>TSGJ - Inicio Supervisor</title>
    <link href='https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css' rel='stylesheet'>
    <link rel="stylesheet" href="../../Modelo/Archivos_CSS/Style_Supervisor.css">
</head>
<body>

<div class="container">
    <div class="logo">
        <img src="../../Modelo/Archivos_Media/LogotipoFull.png" alt="Logo TSGJ">
    </div>

    <h1>¡Bienvenido, <?= htmlspecialchars($nombre) ?>!</h1>
    <p>Supervisor - TSGJ Sistema de Gestión</p>

    <div class="metrics">
        <div class="card">
            <h3>Total Vehículos</h3>
            <p><?= $totalVehiculos ?></p>
        </div>

        <div class="card">
            <h3>Total Choferes</h3>
            <p><?= $totalChoferes ?></p>
        </div>
    </div>

</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
    $(".btn-ir-flota").on("click", function(e) {
        e.preventDefault();
        let section = $(this).data("section");
        if (window.parent && section) {
            window.parent.$("[data-section='" + section + "']").click();
        }
    });
</script>

</body>
</html>
