<?php
ini_set('session.gc_maxlifetime', 86400);
session_set_cookie_params(86400);
session_save_path("/home/evolucionamos/public_html/mejora/sessions");
session_start();
if (empty($_SESSION['adm']) || ($_SESSION['role'] != 1 && $_SESSION['role'] != 3)) {
    header("Location: index.php"); exit;
}
require_once '../app/models/votacion.php';

$model = new \app\models\votacion();
$vid   = isset($_GET['vid']) ? (int)$_GET['vid'] : 0;

if (!$vid) { header("Location: resultados.php"); exit; }

$votacion = $model->obtener($vid);
if (!$votacion) { header("Location: resultados.php"); exit; }

// BATCH QUERIES: UN query para totales por zona + UN query para censos
$totalVotosPorZona = $model->totalVotosPorZona($vid);     // [zona => total]
$censos = $model->censoPorZonas();                         // [zona => censo, '_total' => total]
$zonas  = $model->zonasVotacion($vid);
$totalGlobal = $model->totalVotosVotacion($vid);
$censoGlobal = $censos['_total'] ?? 0;
$participacionGlobal = ($censoGlobal > 0) ? round(($totalGlobal / $censoGlobal) * 100, 1) : 0;
$participantes = $model->participantesVotacion($vid);
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <?php require '../head.php'; ?>
    <style>
        body { background:#f8f9fa !important; background-image:none !important; font-family:'Segoe UI',Tahoma,Verdana,sans-serif; }
        .navbar-enterprise { background:#fff !important; border-bottom:2px solid #17c1e8; padding:12px 0; }
        .card-total { border:none; border-radius:20px; box-shadow:0 10px 30px rgba(0,0,0,0.08); padding:30px; background:#fff; }
        .kpi-card { background:#fff; border-radius:15px; box-shadow:0 4px 15px rgba(0,0,0,0.05); padding:20px; text-align:center; }
        .kpi-card .num { font-size:2.2em; font-weight:900; color:#2b5a9e; }
        .kpi-card .lbl { font-size:.72em; color:#888; text-transform:uppercase; letter-spacing:.5px; }
        .zona-card { background:#fff; border:1px solid #eee; border-radius:12px; padding:18px; }
        .progress-zona { height:16px; border-radius:10px; background:#eef2f7; margin-top:5px; }
        .progress-zona .bar { background:linear-gradient(90deg,#2b5a9e,#17c1e8); border-radius:10px; height:100%; transition:width .5s; }
    </style>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-enterprise mb-4">
    <div class="container">
        <img src="../assets/images/logo.png" style="height:48px;" alt="Logo">
        <span class="ms-3 fw-bold" style="color:#2b5a9e;"><?php echo htmlspecialchars($votacion->titulo); ?></span>
        <div class="ms-auto d-flex gap-2">
            <a href="zona.php?vid=<?php echo $vid; ?>" class="btn btn-sm" style="background:#2b5a9e;color:#fff;border:none;border-radius:8px;font-weight:700;font-size:.8em;">Por Zonas</a>
            <a href="generar_reporte.php?vid=<?php echo $vid; ?>" target="_blank" class="btn btn-sm btn-dark" style="border-radius:8px;font-weight:700;font-size:.8em;">PDF</a>
            <a href="resultados.php" class="btn btn-sm btn-outline-secondary" style="border-radius:8px;font-weight:700;font-size:.8em;">← Volver</a>
        </div>
    </div>
</nav>

<div class="container">
    <div class="card-total">

        <h2 class="text-center mb-1" style="color:#2b5a9e;font-weight:800;">PARTICIPACIÓN GENERAL</h2>
        <p class="text-center text-muted mb-4" style="font-size:.85em;"><?php echo htmlspecialchars($votacion->titulo); ?></p>

        <!-- KPIs globales -->
        <div class="row g-3 mb-4">
            <div class="col-6 col-md-3">
                <div class="kpi-card">
                    <div class="num"><?php echo number_format($totalGlobal,0,',','.'); ?></div>
                    <div class="lbl">Total Votos</div>
                </div>
            </div>
            <div class="col-6 col-md-3">
                <div class="kpi-card">
                    <div class="num"><?php echo $participacionGlobal; ?>%</div>
                    <div class="lbl">Participación</div>
                </div>
            </div>
            <div class="col-6 col-md-3">
                <div class="kpi-card">
                    <div class="num"><?php echo number_format($censoGlobal,0,',','.'); ?></div>
                    <div class="lbl">Censo Electoral</div>
                </div>
            </div>
            <div class="col-6 col-md-3">
                <div class="kpi-card">
                    <div class="num"><?php echo $participantes ? $participantes->votaron : 0; ?></div>
                    <div class="lbl">Delegados Votaron</div>
                </div>
            </div>
        </div>

        <hr>
        <h5 class="mb-3" style="color:#2b5a9e;font-weight:700;font-size:.9em;text-transform:uppercase;letter-spacing:1px;">
            Participación por Zona Electoral
        </h5>

        <div class="row g-3">
            <?php foreach ($zonas as $z):
                $votosZ = isset($totalVotosPorZona[$z->zona]) ? $totalVotosPorZona[$z->zona] : 0;
                $censoZ = isset($censos[$z->zona]) ? $censos[$z->zona] : 0;
                $pctZ   = ($censoZ > 0) ? round(($votosZ / $censoZ) * 100, 1) : 0;
            ?>
                <div class="col-md-6">
                    <div class="zona-card">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <strong style="color:#2b5a9e;text-transform:uppercase;font-size:.88em;"><?php echo htmlspecialchars($z->zona); ?></strong><br>
                                <small class="text-muted">Censo: <?php echo number_format($censoZ,0,',','.'); ?> poderes</small>
                            </div>
                            <div class="text-end">
                                <span class="badge" style="background:#2b5a9e;font-size:.88em;padding:8px 14px;">
                                    <?php echo number_format($votosZ,0,',','.'); ?> votos
                                </span>
                                <br>
                                <small style="font-weight:700;color:<?php echo $pctZ >= 50 ? '#28a745':'#dc3545'; ?>;"><?php echo $pctZ; ?>%</small>
                            </div>
                        </div>
                        <div class="progress-zona">
                            <div class="bar" style="width:<?php echo min($pctZ,100); ?>%;"></div>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>

        <div class="mt-4">
            <a href="resultados.php" class="btn btn-dark w-100" style="padding:12px;font-weight:700;border-radius:10px;">
                ← VOLVER A VOTACIONES
            </a>
        </div>
    </div>
</div>

<footer class="mt-4 py-3 text-center text-muted" style="font-size:.82em;">
    <p>FYCLS S.A.S — Brújula Virtual</p>
</footer>
</body>
</html>
