<?php
declare(strict_types=1);
require_once __DIR__ . "/config/database.php";
require_once __DIR__ . "/includes/auth.php";
requireAuth();

$homeImagesVersion = (string) @filemtime(__DIR__ . "/assets/images/home/slider-1.jpg");
if ($homeImagesVersion === "" || $homeImagesVersion === "0") {
    $homeImagesVersion = (string) time();
}

$totalStagiaires = (int) $pdo->query("SELECT COUNT(*) FROM stagiaires")->fetchColumn();
$totalFormateurs = (int) $pdo->query("SELECT COUNT(*) FROM formateurs")->fetchColumn();
$totalFormations = (int) $pdo->query("SELECT COUNT(*) FROM formations")->fetchColumn();
$totalInscriptions = (int) $pdo->query("SELECT COUNT(*) FROM inscriptions")->fetchColumn();
$totalPaiements = (float) $pdo->query("SELECT COALESCE(SUM(montant),0) FROM paiements")->fetchColumn();

$monthlyRevenue = $pdo->query(
    "SELECT DATE_FORMAT(date_paiement, '%Y-%m') AS mois, SUM(montant) AS total
     FROM paiements
     GROUP BY DATE_FORMAT(date_paiement, '%Y-%m')
     ORDER BY mois DESC
     LIMIT 6"
)->fetchAll();
$monthlyRevenue = array_reverse($monthlyRevenue);
$maxRevenue = 0.0;
foreach ($monthlyRevenue as $m) {
    $maxRevenue = max($maxRevenue, (float) $m["total"]);
}

$topFormations = $pdo->query(
    "SELECT f.titre, COUNT(i.id) AS total
     FROM formations f
     LEFT JOIN inscriptions i ON i.formation_id = f.id
     GROUP BY f.id, f.titre
     ORDER BY total DESC, f.titre ASC
     LIMIT 5"
)->fetchAll();

require_once __DIR__ . "/includes/header.php";
?>
<section class="home-hero">
    <div>
        <h2>La plateforme pour explorer, suivre et valoriser vos formations</h2>
        <p>
            Decouvrez les parcours du PÔLE INFORMATIQUE ET SERVICES, gerez les apprenants,
            suivez les inscriptions et accelerez la delivrance des attestations.
        </p>
        <div class="hero-actions">
            <a class="hero-btn primary" href="/PIS/formations/index.php">Explorer les formations</a>
            <a class="hero-btn secondary" href="/PIS/stagiaires/index.php">Voir les apprenants</a>
            <a class="hero-btn secondary" href="/PIS/attestations/index.php">Attestations</a>
            <?php if (isAdmin()): ?>
                <a class="hero-btn secondary" href="/PIS/utilisateurs.php">Utilisateurs</a>
            <?php endif; ?>
        </div>
        <div class="hero-slider-zone" id="heroSlider">
            <img class="hero-slider-image active" src="/PIS/assets/images/home/slider-1.jpg?v=<?= urlencode($homeImagesVersion) ?>" alt="Image informatique 1">
            <img class="hero-slider-image" src="/PIS/assets/images/home/slider-2.jpg?v=<?= urlencode($homeImagesVersion) ?>" alt="Image informatique 2">
            <img class="hero-slider-image" src="/PIS/assets/images/home/slider-3.jpg?v=<?= urlencode($homeImagesVersion) ?>" alt="Image informatique 3">
            <img class="hero-slider-image" src="/PIS/assets/images/home/slider-4.jpg?v=<?= urlencode($homeImagesVersion) ?>" alt="Image informatique 4">
            <img class="hero-slider-image" src="/PIS/assets/images/home/slider-5.jpg?v=<?= urlencode($homeImagesVersion) ?>" alt="Image informatique 5">
            <img class="hero-slider-image" src="/PIS/assets/images/home/slider-6.jpg?v=<?= urlencode($homeImagesVersion) ?>" alt="Image informatique 6">
            <img class="hero-slider-image" src="/PIS/assets/images/home/slider-7.jpg?v=<?= urlencode($homeImagesVersion) ?>" alt="Image informatique 7">
            <img class="hero-slider-image" src="/PIS/assets/images/home/slider-8.jpg?v=<?= urlencode($homeImagesVersion) ?>" alt="Image informatique 8">
            <img class="hero-slider-image" src="/PIS/assets/images/home/slider-9.jpg?v=<?= urlencode($homeImagesVersion) ?>" alt="Image informatique 9">
            <img class="hero-slider-image" src="/PIS/assets/images/home/slider-10.jpg?v=<?= urlencode($homeImagesVersion) ?>" alt="Image informatique 10">
        </div>
    </div>
    <div>
        <div class="hero-visual" style="background-image: url('/PIS/assets/images/home/hero-tech.jpg?v=<?= urlencode($homeImagesVersion) ?>');"></div>
        <div class="search-card" style="margin-top:12px;">
            <h3>Recherche rapide</h3>
            <p>Acces direct aux donnees essentielles.</p>
            <form method="GET" action="/PIS/stagiaires/index.php">
                <input type="text" name="q" placeholder="Nom, prenoms, numero...">
                <button type="submit">Rechercher</button>
            </form>
        </div>
    </div>
</section>

<section class="home-section">
    <h3>Categories en vedette</h3>
    <div class="home-categories">
        <div class="cat-card">Bureautique<span>Word, Excel, PowerPoint</span></div>
        <div class="cat-card">Developpement Web<span>HTML, CSS, PHP, JS</span></div>
        <div class="cat-card">Maintenance IT<span>Diagnostic et support</span></div>
        <div class="cat-card">Initiation Informatique<span>Fondamentaux numeriques</span></div>
        <div class="cat-card">Data et Reporting<span>Tableaux de bord et indicateurs</span></div>
    </div>
</section>

<section class="home-section">
    <h3>Univers technologiques</h3>
    <div class="tech-gallery">
        <div class="tech-tile">
            <img src="/PIS/assets/images/home/coding-team.jpg?v=<?= urlencode($homeImagesVersion) ?>" alt="Developpement web">
            <span>Developpement Web</span>
        </div>
        <div class="tech-tile">
            <img src="/PIS/assets/images/home/cloud-network.jpg?v=<?= urlencode($homeImagesVersion) ?>" alt="Reseaux et cloud">
            <span>Reseaux & Cloud</span>
        </div>
        <div class="tech-tile">
            <img src="/PIS/assets/images/home/data-office.jpg?v=<?= urlencode($homeImagesVersion) ?>" alt="Data et bureautique">
            <span>Data & Bureautique</span>
        </div>
    </div>
</section>

<section class="home-section">
    <h3>Indicateurs cles</h3>
    <div class="kpi-grid">
        <div class="kpi-box"><div>Apprenants</div><div class="value"><?= $totalStagiaires ?></div></div>
        <div class="kpi-box"><div>Formateurs</div><div class="value"><?= $totalFormateurs ?></div></div>
        <div class="kpi-box"><div>Formations</div><div class="value"><?= $totalFormations ?></div></div>
        <div class="kpi-box"><div>Inscriptions</div><div class="value"><?= $totalInscriptions ?></div></div>
        <div class="kpi-box"><div>Recettes (FCFA)</div><div class="value"><?= number_format($totalPaiements, 0, ",", " ") ?></div></div>
    </div>
</section>

<section class="dashboard-section home-section">
    <h3>Nouvelles inscriptions</h3>
    <div class="kpi-box">
        <div>Total des inscriptions</div>
        <div class="value"><?= $totalInscriptions ?></div>
    </div>
</section>

<section class="grid home-section">
    <div class="card">
        <h3>Recettes mensuelles</h3>
        <?php if (empty($monthlyRevenue)): ?>
            <p>Aucune donnee de paiement.</p>
        <?php else: ?>
            <?php foreach ($monthlyRevenue as $item): ?>
                <?php $width = $maxRevenue > 0 ? ((float) $item["total"] / $maxRevenue) * 100 : 0; ?>
                <div class="bar-row">
                    <span><?= htmlspecialchars($item["mois"]) ?></span>
                    <div class="bar"><div class="bar-fill" style="width: <?= (float) $width ?>%"></div></div>
                    <strong><?= number_format((float) $item["total"], 0, ",", " ") ?> FCFA</strong>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
    <div class="card">
        <h3>Top formations</h3>
        <table>
            <thead><tr><th>Formation</th><th>Inscriptions</th></tr></thead>
            <tbody>
            <?php foreach ($topFormations as $formation): ?>
                <tr>
                    <td><?= htmlspecialchars($formation["titre"]) ?></td>
                    <td><?= (int) $formation["total"] ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>

<section class="home-section">
    <h3>Galerie technologique</h3>
    <div class="showcase-grid">
        <div class="showcase-card">
            <img src="/PIS/assets/images/home/cyber-security.jpg?v=<?= urlencode($homeImagesVersion) ?>" alt="Cybersecurite">
            <div>Cybersecurite et bonnes pratiques numeriques</div>
        </div>
        <div class="showcase-card">
            <img src="/PIS/assets/images/home/coding-team.jpg?v=<?= urlencode($homeImagesVersion) ?>" alt="Equipe de developpement">
            <div>Travail collaboratif et projets de developpement</div>
        </div>
        <div class="showcase-card">
            <img src="/PIS/assets/images/home/digital-learning.jpg?v=<?= urlencode($homeImagesVersion) ?>" alt="Apprentissage digital">
            <div>Apprentissage digital moderne et interactif</div>
        </div>
        <div class="showcase-card">
            <img src="/PIS/assets/images/home/hero-tech.jpg?v=<?= urlencode($homeImagesVersion) ?>" alt="Innovation technologique">
            <div>Innovation technologique et transformation digitale</div>
        </div>
    </div>
</section>

<footer class="landing-footer">
    <div class="landing-footer-grid">
        <div>
            <h4>PÔLE INFORMATIQUE ET SERVICES</h4>
            <p>Plateforme de gestion des formations du centre.</p>
            <p>Suivi des apprenants, inscriptions, attestations et pilotage pedagogique.</p>
        </div>
        <div>
            <h4>Navigation</h4>
            <p><a href="/PIS/index.php">Accueil</a></p>
            <p><a href="/PIS/stagiaires/index.php">Apprenants</a></p>
            <p><a href="/PIS/formations/index.php">Formations</a></p>
            <p><a href="/PIS/attestations/index.php">Attestations</a></p>
        </div>
        <div>
            <h4>Services</h4>
            <p>Gestion administrative</p>
            <p>Suivi des parcours</p>
            <p>Production des attestations</p>
            <p>Statistiques et reporting</p>
        </div>
        <div>
            <h4>Contact</h4>
            <p>Email: daas@univkara.tg</p>
            <p>Presidence: B.P. 404 Kara-Togo</p>
            <p>Campus Sud: B.P. 43 Kara-Togo</p>
            <p>Tel: (228) 26601269</p>
        </div>
    </div>
    <div class="landing-footer-bottom">
        <span>© <?= date("Y") ?> PÔLE INFORMATIQUE ET SERVICES</span>
        <span>Universite de Kara - Direction de la DAAS</span>
    </div>
</footer>
<?php require_once __DIR__ . "/includes/footer.php"; ?>
