<?php
session_start();
include('../connexion.php');
if (!isset($_SESSION['celest']))
{
	header ('Location: index.php');
}
$active='user';
$sql='SELECT * FROM user WHERE id_user="'.$_SESSION['celest'].'"';
$reponse=$bdd-> query($sql) or die('Erreur SQL !<br />'.$sql.'<br />'.mysql_error());
$data=$reponse->fetch();
?>
<?php include('tete.php'); ?>
<link rel="stylesheet" type="text/css" href="style/style_admin.css" />
</head>
<body>
    <div id="wrapper">
    	<?php include('menu_admin.php'); ?>
        <div id="page-wrapper">
            <div class="row">
                <div class="col-lg-2 col-md-1"></div>
                <div class="col-lg-8 col-md-10 col-sm-11 col-xs-12" id="administration">
                    <div id="titro_admin">Mon profil</div>
                    <h1>Mot de passe</h1>
                    <?php
					$sql2 = 'SELECT COUNT(*) as nbr FROM privilege';
					$reponse2=$bdd->query($sql2) or die('Erreur SQL !<br />'.$sql2.'<br />'.mysql_error());
					$data2 =$reponse2->fetch();
					$nbr_selection=$data2[0];
					?>
					<div align="center">
						<?php
						if (isset($_POST['valider']))
						{
							$mdp3=addslashes(($_POST['mdp3']));
							$mdp1=addslashes(($_POST['mdp1']));
							$mdp2=addslashes(($_POST['mdp2']));
							$date_mise=date('Y-m-d H:i:s');

							$sql = 'SELECT count(*) FROM user WHERE mdp_user="'.$mdp3.'" AND id_user AND id_user="'.$_SESSION['celest'].'"';
							$reponse=$bdd-> query($sql) or die('Erreur SQL !<br />'.$sql.'<br />'.mysql_error());
							$data =$reponse->fetch();
							$nbr_mdp=$data[0];
							
							if (empty($mdp3))
							{
								echo '<div class="alert alert-danger">Veuillez renseigner l\'ancien mot de passe</div>';
							}
							elseif (empty($mdp1))
							{
								echo '<div class="alert alert-danger">Veuillez renseigner le nouveau mot de passe</div>';
							}
							elseif (empty($mdp2))
							{
								echo '<div class="alert alert-danger">Veuillez confirmer le nouveau mot de passe</div>';
							}
							elseif($nbr_mdp==0)
							{
								echo '
								<div class="alert alert-danger">
									<strong>Erreur!</strong> Ancien mot de Passe incorrect
								</div>';
							}
							elseif ($mdp1!=$mdp2)
							{
								echo '
								<div class="alert alert-danger">
									<strong>Erreur!</strong> Les deux nouveaux mots de passe ne correspondent pas
								</div>';
							}
							else
							{
								$req='UPDATE user SET  mdp_user="'.$mdp1.'" WHERE id_user='.$_SESSION['celest'].'';
								$reponse=$bdd-> query($req) or die('Erreur SQL !<br />'.$req.'<br />'.mysql_error());								
								echo '<div class="alert alert-success"> Modification effectuée avec succès</div>';
								?> 
								<script type="text/javascript">
								window.setTimeout("location=('mon-profil.php');",2500);
								</script>
								<?php
							}
						}			
						?>
					</div>
					<form role="form" method="post" action="" enctype="multipart/form-data" name="form2">
                        <h5>Ancien mot de passe</h5>
                        <input class="form-control" type="password" name="mdp3">
						<h5>Mot de passe</h5>
						<input type="password" class="form-control" name="mdp1">
						<h5>Confirmation Mot de passe</h5>
						<input type="password" class="form-control" name="mdp2">
						<div id="valider">
							<input type="submit" class="btn btn-primary" value="Valider" name="valider">
							&nbsp; <a href="administration.php"><i class="fa fa-reply"></i> Retour</a>
						</div>
					</form>

                    <hr><h1>Login</h1>
                    <div align="center">
                        <?php
                        if (isset($_POST['valider2']))
                        {
                            $nom=addslashes(($_POST['nom']));
                            if (empty($nom))
                            {
                                echo '<div class="alert alert-danger">Veuillez renseigner le login</div>';
                            }
                            else
                            {
                                $req='UPDATE user SET lib_user="'.$nom.'" WHERE id_user='.$_SESSION['celest'].'';
                                $reponse=$bdd-> query($req) or die('Erreur SQL !<br />'.$req.'<br />'.mysql_error());
                                echo '<div class="alert alert-success"> Profil modifié avec succès</div>';
                                ?> 
                                <script type="text/javascript">
                                window.setTimeout("location=('mon-profil.php');",2500);
                                </script>
                                <?php
                            }
                        }			
                        ?>
                    </div>
                    <form role="form" method="post" action="" enctype="multipart/form-data" name="form2">
                        <input type="text" class="form-control" name="nom" value="<?php echo stripslashes($data['lib_user']) ?>">
                        <div id="valider">
                            <input type="submit" class="btn btn-primary" value="Valider" name="valider2">
                            &nbsp; <a href="administration.php"><i class="fa fa-reply"></i> Retour</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>