<?php
	include '../lib/init.php';

	/**
	 * Initialisation
	 */
	use Lib\Utilisateur;
	use Lib\Tool;
	use Lib\Search;

    Utilisateur::ifConnect();
    
    /**
     * Variables de recherche
     */
    if(isset($_POST['addRecherche'])){
        Search::postRecherche('comite');
    }
    extract(Search::getRecherche('comite',array('recherche')));

    /**
     * Variables de pagination
     */
    extract(Tool::initPaginate(50));
?>
<!doctype html>
<html lang="fr">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width; initial-scale=1;">
	<title><?= TITLEBACK ?></title>
	<link rel="icon" type="image/png" href="<?= BASEADMIN ?>img/layout/favicon.png">
    <link href="<?= BASEFRONT ?>js/scroll/scroll.css" rel="stylesheet" type="text/css">
    <link href="<?= BASEFRONT ?>js/sweet/sweet.css" rel="stylesheet" type="text/css">
	<link href="<?= BASEADMIN ?>css/app.css" rel="stylesheet" type="text/css">
</head>

<body>

	<main id="main">

		<?php
			include '../include/menu.php';
		?>

		<div id="container">

			<?php
				include '../include/header.php';
			?>

			<div id="contentTitre">
				<h1>Gestion des comités</h1>
			</div>

			<div id="content">

                <!-- Recherche -->
                <div id="recherche">

                    <form action="#" method="post">
                        <input type="text" name="recherche" class="form-elem medium" placeholder="Nom prénom" value="<?php echo $recherche ?>">
                        <button name="addRecherche" type="submit"><i class="fa fa-search"></i></button>
                    </form>

                </div>

                <?= Tool::getFlash() ?>

                <a href="<?= BASEADMIN ?>page/addComite.php" class="btn btnAddContent turquoise"><i class="fa fa-plus"></i> Ajouter un comité</a>
                
                <!-- Tableau de gestion -->
                <table class="table">
                    
                    <tr>
                    	<th width="5%"></th>
                        <th width="80%" class="left">Comité</th>
                        <th width="15%" colspan="3">Actions</th>
                    </tr>

                    <?php

                        $requete = "SELECT * FROM comite ";
                                    if(!empty($recherche))
                                        $requete .= " WHERE comiteNom LIKE '%$recherche%' ";
                                    $requete .= " ORDER BY comiteId DESC
                                    LIMIT $debut, $nbPage ";

                        $sql = $bdd->query($requete);

                        /**
                         * Si aucun comité
                         */
                        if($sql->rowCount() == 0){
                            echo'<tr><td colspan="5">Aucun comité</td></tr>';
                        }

                        while($data = $sql->fetchObject()){
                            echo'<tr>';

                                /**
                                 * Image
                                 */
                                echo'<td>';
                                    echo'<img class="tableImage" src="'.BASEFRONT.'img/comite/tmp/'.$data->comiteImage.'">';
                                echo'</td>';

                                /**
                                 * Informations
                                 */
                                echo '<td class="left">';
                                    echo '<p class="tableDate"> Ajout : '.Tool::dateTime('d/m/Y à H:i',$data->comiteCreated).'</p>';
                                    if(!is_null($data->comiteChanged))
                                        echo'<p class="tableDate">Modification : '.Tool::dateTime('d/m/Y à H:i',$data->comiteChanged).'</p>';
                                    echo '<p><strong>'.$data->comiteNom.'</strong></p>';
                                echo'</td>';

                                /**
                                 * Actions
                                 */
                                if(!$data->comiteEtat){
                                    echo'<td data-url="'.BASEADMIN.'page/publicationComite.php?comite='.$data->comiteId.'" class="tablePublucation">
                                            <a href="#" title="Publication"><i class="tableAction rouge fa fa-check"></i></a>
                                        </td>';
                                }
                                else{
                                    echo'<td data-url="'.BASEADMIN.'page/publicationComite.php?comite='.$data->comiteId.'" class="tablePublucation">
                                            <a href="#" title="Publication"><i class="tableAction turquoise fa fa-check"></i></a>
                                         </td>';
                                }

                                echo'<td>
                                        <a href="'.BASEADMIN.'page/editComite.php?comite='.$data->comiteId.'" title="Modifier le comité"><i class="tableAction fa fa-pencil"></i></a>
                                    </td>';

                                echo'<td>
                                        <a href="'.BASEADMIN.'page/deleteComite.php?comite='.$data->comiteId.'" class="sweetDelete" data-text="êtes vous sur ?" data-title="Supprimer un comité" title="Supprimer un comité"><i class="tableAction fa fa-times"></i></a>
                                    </td>';

                            echo'</tr>';

                        }

                    ?>

                </table>

                <!-- Pagination -->
                <div id="pagination">
                    <?php
                        $requetePage = "SELECT count(comiteId) as total FROM comite ";
                                        if(!empty($recherche))
                                            $requetePage .= " WHERE comiteNom LIKE '%$recherche%' ";

                        Tool::addPaginate($requetePage,BASEADMIN.'page/managerComite.php',$nbPage,$page);
                    ?>
                </div>

			</div>

		</div>

	</main>

	<script type="text/javascript" src="<?= BASEFRONT ?>js/jquery/jquery.js"></script>
	<script type="text/javascript" src="<?= BASEFRONT ?>js/jquery/jquery-ui.js"></script>
    <script type="text/javascript" src="<?= BASEFRONT ?>js/scroll/scroll.js"></script>
    <script type="text/javascript" src="<?= BASEFRONT ?>js/sweet/sweet.js"></script>
	<script type="text/javascript" src="<?= BASEADMIN ?>js/app.js"></script>	
	
</body>
</html>