<?php

    require ('../connectionBD.php');
    $mail = null;
    $db = connexionBD();
    init();

    function init(){
        session_start();
        global $mail;
        //Si on est pas connecté redirection vers la page de connexion
        if(!isset($_SESSION['mail'])){
            header('Location: ../index.php');
            exit();
        }
        else{
            $mail = $_SESSION['mail'];
        }
    }

    function getPrenom($mail){
        global $db;
        $result = pg_query_params($db, "SELECT prenom from Utilisateur WHERE mail = $1;", array($mail));
        if($result){
            $row = pg_fetch_array($result);
            $prenom = $row[0];
            return $prenom;
        }
        return null;
    }

    function getLogin($mail){
        global $db;
        $result = pg_query_params($db, "SELECT login from Utilisateur WHERE mail = $1;", array($mail));
        if($result){
            $row = pg_fetch_array($result);
            $login = $row[0];
            return $login;
        }
        return null;
    }

    function getTrajetsProposes($mail){
        global $db;
        $result = pg_query_params($db, "SELECT COUNT(*) from Trajet WHERE mailProposition = $1 AND estAnnule = false AND datedepart >= current_date;", array($mail));
        if($result){
            $row = pg_fetch_array($result);
            return $row[0];
        }
        return 0;
    }

    function getTrajetsReserves($mail){
        global $db;
        $result = pg_query_params($db, "SELECT COUNT(*) FROM trajet tr, reserver r WHERE r.codetrajet = tr.codetrajet AND mailutilisateur = $1 AND etatres != 'Annulée' AND estAnnule = false AND datedepart >= current_date;", array($mail));
        if($result){
            $row = pg_fetch_array($result);
            return $row[0];
        }
        return 0;
    }

    function getMessagesNonLus($mail){
        global $db;
        $result = pg_query_params($db, "SELECT COUNT(*) from Message WHERE mailRecepteur = $1 AND estLu = FALSE;", array($mail));
        if($result){
            $row = pg_fetch_array($result);
            $login = $row[0];
            return $login;
        }
    }

    function getAvisNonLus($mail){
        global $db;
        $result = pg_query_params($db, "SELECT COUNT(tr.mailProposition) FROM Trajet tr, Avis av WHERE tr.codeTrajet = av.codeTrajet AND av.estLu = false AND tr.mailProposition = $1;", array($mail));
        if($result){
            $row = pg_fetch_array($result);
            $login = $row[0];
            return $login;
        }
    }

    function getPathImgProfil($mail){
        if (file_exists("../imageProfil/$mail.png")){
            return "../imageProfil/$mail.png";
        }
        return "../imageProfil/default.svg";
    }

?>

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="tableauBord.css">
    <link rel="icon" type="image/png" href="" />
    <title>Tableau de bord</title>
</head>
<body>
<nav class="menu">
    <a class="menuItem" href="./../inscription.php">Tableau de bord</a>
    <a class="menuItem" href="../inscription.php">Trajets publiés</a>
    <a class="menuItem" href="../inscription.php">Trajets réservés</a>
    <a class="menuItem" href="../inscription.php">Messagerie</a>
    <a class="menuItem" href="../inscription.php">Avis reçus</a>
    <a class="menuItem" href="../inscription.php">Avis laissés</a>
    <a class="menuItem" href="../inscription.php">Profil</a>
    <a class="menuItem" href="../deconnexion.php">Se déconnecter</a>
</nav>
<div id="content">
    <div id="tableauBord">
        <h1>Bienvenue <?php echo getLogin($mail); ?></h1>
        <div id="divImgProfil">
            <img id="imgProfil" src="<?php echo getPathImgProfil($mail); ?>">
        </div>
        <div id="trajet">
            <a class="btnTrajet" href="../ah.php">Déposer un trajet</a>
            <a id="btnTrajet2" class="btnTrajet" href="../ah.php">Rechercher un trajet</a>
        </div>
        <div id="recap">
            <div class="recapLine">
                <img class="recapIcon" src="../car.png">
                <p>Vous avez <?php echo getTrajetsProposes($mail); ?> trajets proposés à effectuer.</p>
            </div>
            <div class="recapLine">
                <img class="recapIcon" src="../car.png">
                <p>Vous avez <?php echo getTrajetsReserves($mail); ?> trajets réservés à effectuer.</p>
            </div>
            <div class="recapLine">
                <img class="recapIcon" src="../mail.svg">
                <p>Vous avez <?php echo getMessagesNonLus($mail); ?> messages non lus.</p>
            </div>
            <div class="recapLine">
                <img class="recapIcon" src="../star.png">
                <p>Vous avez <?php echo getAvisNonLus($mail); ?> avis non lus.</p>
            </div>
        </div>
    </div>
</div>
</body>
</html>