<?php
    include('model.php');
        $sql = new model;
    if (empty($_SESSION['admin_id'])) {
        header('location:login.php');
    }

    $_SESSION['mod_profil']="";

    // modification profil
    if(isset($_POST['update1'])){
      
        //Vérification username
        if (!empty($_POST['username'])) {
            if (trim($_POST['username']=="")) {
                $error_username= "Caractères alpha numérique uniquement";
            }else{
                $username=htmlspecialchars($_POST['username']);
            
            }
        }else{
            $error_username= "Ce champs est obligatoire";
        }

    
//Vérifier avant exécution
    if (empty($username) ) {
        
    } else {
        $profil_modifier = $sql->profil_modifier($username, $_SESSION['admin_id']);
        if($profil_modifier){
           
            $_SESSION['admin_username']=$username;

            $_SESSION['mod_profil']="profil";
        //echo "<script>alert(\"Profil modifié.\")</script>";
        //echo'<script type="text/javascript">window.location.href="profile"</script>';


        }
    }
    


}


    if(isset($_POST['update2'])){

    //Vérification ancien mot de passe
        if (!empty($_POST['password'])) {
            if (trim($_POST['password']=="")) {
            $error_password="Caractères alpha numérique uniquement";
            }else{
            $password=htmlspecialchars($_POST['password']);
            
            }
            
            }else{
                $error_password= "Champs obligatoire";
            }   
    
             //Vérification nouveau mot de passe
        if (!empty($_POST['new_password'])) {
            if (trim($_POST['new_password']=="")) {
            $error_new_password="Caractères alpha numérique uniquement";
            }else{
            $new_password=htmlspecialchars($_POST['new_password']);
            
            }
            
            }else{
                $error_new_password= "Champs obligatoire";
            } 
            
            //Vérification confirm nouveau mot de passe
        if (!empty($_POST['confirm_new'])) {
            if (trim($_POST['confirm_new']=="")) {
            $error_confirm_new="Caractères alpha numérique uniquement";
            }else{
            $confirm_new=htmlspecialchars($_POST['confirm_new']);
            
            }
            
            }else{
                $error_confirm_new= "Champs obligatoire";
            } 
    
            if (empty($password) || empty($new_password) || empty($confirm_new)) {
                //echo "<script>alert(\"Veuillez remplir tous les champs.\")</script>";
               
            }elseif ($new_password <> $confirm_new){
                //echo "<script>alert(\"Mots de passes non identiques.\")</script>";
                //echo '<script>window.location.href="profile"</script>';
                $_SESSION['mod_profil']="non_identique";
            }else {
                $access= password_verify($_POST['password'], $_SESSION['admin_password']);
                if ($access>0) {
                    $new_pass_hash=password_hash($new_password, 1);
                    $password_modifier = $sql->password_modifier($new_pass_hash,$_SESSION['admin_id']);
                    if($password_modifier){
                        $_SESSION['admin_password']=password_verify($new_pass_hash, $new_password);
                        //echo "<script>alert(\"Mot de passe modifié .\")</script>"; 
                        //echo '<script>window.location.href="profile"</script>';  
                        $_SESSION['mod_profil']="mdp";
                 
                    }else{
                 
                    }
                }else {
                    $_SESSION['mod_profil']="old_incorrect";
                    //echo "<script>alert(\"Ancien mot de passe incorrect  .\")</script>";
                    //echo '<script>window.location.href="profile"</script>'; 
                }
            }
        }
?>
<!doctype html>
<html lang="en" data-layout="horizontal" data-topbar="dark" data-sidebar-size="lg" data-sidebar="light" data-sidebar-image="none" data-preloader="disable">

<head>

    <meta charset="utf-8" />
    <title>Vision million</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta content="Vision million" name="description" />
    <meta content="Vision million" name="Vision million" />
    <!-- App favicon -->
    <link rel="shortcut icon" href="assets/images/LOG1_dark.png">

    <!-- Layout config Js -->
    <script src="assets/js/layout.js"></script>
    <!-- Bootstrap Css -->
    <link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <!-- Icons Css -->
    <link href="assets/css/icons.min.css" rel="stylesheet" type="text/css" />
    <!-- App Css-->
    <link href="assets/css/app.min.css" rel="stylesheet" type="text/css" />
    <!-- custom Css-->
    <link href="assets/css/custom.min.css" rel="stylesheet" type="text/css" />
    <style>
        .animation-nav li a::before {
            background-color: #1E2762;
        }
    </style>
 
        

</head>

<body>

    <!-- Begin page -->
    <div id="layout-wrapper">

       <?php include('header.php'); ?>
<br><br>

        <div class="app-menu navbar-menu">
            <!-- LOGO -->
            <!-- <div class="navbar-brand-box">
                
                <a href="index.html" class="logo logo-dark">
                    <span class="logo-sm">
                        <img src="assets/images/logo-sm.png" alt="" height="22">
                    </span>
                    <span class="logo-lg">
                        <img src="assets/images/logo-dark.png" alt="" height="17">
                    </span>
                </a>
                
                <a href="index.html" class="logo logo-light">
                    <span class="logo-sm">
                        <img src="assets/images/logo-sm.png" alt="" height="22">
                    </span>
                    <span class="logo-lg">
                        <img src="assets/images/logo-light.png" alt="" height="17">
                    </span>
                </a>
                <button type="button" class="btn btn-sm p-0 fs-20 header-item float-end btn-vertical-sm-hover" id="vertical-hover">
                    <i class="ri-record-circle-line"></i>
                </button>
            </div> -->

            <div id="scrollbar">
                <div class="container-fluid">

                    <div id="two-column-menu">
                    </div>
                    <ul class="navbar-nav" id="navbar-nav">
                        <?php include('nav.php'); ?>
                    </ul>
                </div>
                <!-- Sidebar -->
            </div>

            <div class="sidebar-background"></div>
        </div>
        <!-- Left Sidebar End -->
        <!-- Vertical Overlay-->
        <div class="vertical-overlay"></div>

        <div class="main-content">
                
                <div class="page-content">
                    <div class="container-fluid">


                  <!-- start page title -->
                  <div class="row">
                 
                        </div>
                        <!-- end page title -->
                        <div class="row">
                            <div class="col-lg-12">
                                <div class="card">
                                    <div class="card-body">
                                        <div class="row g-2">
                                            <div class="col-sm-9">
                                                <!-- Bouton retour -->
                                    <div class="row justify-content-beetwen g-2 mb-3">
                                        <!--end col-->
                                        <!--<div class="col-auto">
                                            <div class="d-flex gap-2 align-items-start">
                                                <h5 class="mb-sm-0">Affectation d'un enseignant dans une classe</h5>
                                            </div>
                                        </div>
                                        end col-->
                                    </div>
                                <!-- Bouton retour -->
                                            </div>
                                            <div class="col-sm-3">
                                                <div class="search-box">
                                                <h4 class="mb-sm-0">Modifier mon profil</h4>
                                                </div>
                                            </div>
                                            <div class="col-sm-12">
                                            <?php
                                                    if ($_SESSION['mod_profil']=="profil") {
                                                        # code...
                                                        echo('
                                                            <div class="alert alert-success mb-xl-0" role="alert">
                                                                <strong> Opération réussie. </strong> <b>Profil modifié.</b> 
                                                            </div>
                                                        ');
                                                    }
                                                    elseif ($_SESSION['mod_profil']=="non_identique") {
                                                        # code...
                                                        echo('
                                                            <div class="alert alert-danger mb-xl-0" role="alert">
                                                                <strong> Echec de l\'opération. </strong> <b>Mots de passes non identiques.</b> 
                                                            </div>
                                                        ');
                                                    }
                                                    elseif ($_SESSION['mod_profil']=="old_incorrect") {
                                                        # code...
                                                        echo('
                                                            <div class="alert alert-danger mb-xl-0" role="alert">
                                                                <strong> Echec de l\'opération. </strong> <b>Ancien mot de passe incorrect.</b> 
                                                            </div>
                                                        ');
                                                    }
                                                    elseif ($_SESSION['mod_profil']=="existe") {
                                                        # code...
                                                        echo('
                                                            <div class="alert alert-danger mb-xl-0" role="alert">
                                                                <strong> Echec de l\'opération. </strong> <b>Ce nom d\'utilisateur existe déjà.</b> 
                                                            </div>
                                                        ');
                                                    }
                                                    elseif ($_SESSION['mod_profil']=="mdp") {
                                                        # code...
                                                        echo('
                                                            <div class="alert alert-success mb-xl-0" role="alert">
                                                                <strong> Opération réussie. </strong> <b>Mot de passe modifié.</b> 
                                                            </div>
                                                        ');
                                                    }
                                                    elseif (empty($_SESSION['mod_profil'])) {
                                                        # code...
                                                    }
                                                ?>
                                            </div>
                                            <!--end col-->
                                            
                                        </div><!--end row-->
                                    </div>
                                </div>
                            </div><!--end col-->
                        </div><!--end row-->
                        <div class="row">
                        <div class="col-12">
                            <h5 class="mb-3"></h5>
                            <div class="card">
                                <div class="card-body">
                                    
                                    <!-- Nav tabs -->
                                    <ul class="nav nav-pills animation-nav gap-2 gap-lg-3 flex-grow-1" role="tablist">
                                        <li class="nav-item">
                                            <a class="nav-link fs-14 active" data-bs-toggle="tab" href="#pill-justified-home-1" role="tab">
                                                <i class="mdi mdi-account-circle d-inline-block d-md-none"></i> <span class="d-none d-md-inline-block"><i class=" mdi mdi-account-circle"></i> Profil</span>
                                                </a>
                                            </li>
                                        <li class="nav-item">
                                            <a class="nav-link fs-14" data-bs-toggle="tab" href="#pill-justified-profile-1" role="tab">
                                                <i class="mdi mdi-lock d-inline-block d-md-none"></i> <span class="d-none d-md-inline-block"><i class=" mdi mdi-lock"></i> Mot de passe</span>
                                            </a>
                                        </li> 
                                    </ul>
                                    <!-- Tab panes -->
                                    <div class="tab-content text-muted">
                                        <div class="tab-pane active" id="pill-justified-home-1" role="tabpanel">
                                            <form action="" enctype="multipart/form-data" method="post">
                                                <div class="row clearfix">
                                                    <div class="col-12">
                                                        <div class="card">
                                                            <div class="card-body">
                                                                <div class="row g-3 clearfix">
                                                                    <div class=" col-md-12 col-sm-12">
                                                                   
                                                                   Nom d'utilisateur <input type="text" name="username" class="form-control" value="<?php echo($_SESSION['admin_username']); ?>">
                                                                    <?php
                                                                     if(!empty($error_username)){
                                                                        echo '<h6 style="color:red;">'.$error_username.'</h6>';
                                                                     }
                                                                     ?>
                                                                    </div>
                                                                    
                                                                
                                                                    <div class="col-sm-12">
                                                                        <input style="background:#1E2762;" type="submit" name="update1" class="btn btn-primary" value="Modifier"/>
                                                                        
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </form>
                                        </div>
                                        <div class="tab-pane" id="pill-justified-profile-1" role="tabpanel">
                                            <form action="" enctype="multipart/form-data" method="post" class="needs-validation" novalidate>
                                                <div class="row clearfix">
                                                    <div class="col-12">
                                                        <div class="card">
                                                            <div class="card-body">
                                                                <div class="row g-3 clearfix">
                                                                    <div class=" col-md-4 col-sm-12">
                                                                       <label class="form-label" for="password-input">Ancien mot de passe</label>
                                                                       
                                                                       <div class="position-relative auth-pass-inputgroup mb-3">
                                                                       <input type="password" name="password" class="form-control pe-5 password-input" placeholder= "ancien mot de passe">
                                                                         <button class="btn btn-link position-absolute end-0 top-0 text-decoration-none text-muted password-addon" type="button" id="password-addon"><i class="ri-eye-fill align-middle"></i></button>
                                                                        </div>
                                                                        <?php
                                                                         if(!empty($error_password)){
                                                                            echo '<h6 style="color:red;">'.$error_password.'</h6>';
                                                                         }
                                                                         ?>
                                                                    </div>
                                                                    <div class="col-md-4 col-sm-12">
                                                                    <label class="form-label" for="password-input">Nouveau mot de passe</label>
                                                                    <div class="position-relative auth-pass-inputgroup mb-3">
                                                                    <input type="password" class="form-control pe-5 password-input" onpaste="return false" placeholder="Mot de passe" id="password-input" aria-describedby="passwordInput" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" name="new_password" >
                                                                         <button class="btn btn-link position-absolute end-0 top-0 text-decoration-none text-muted password-addon" type="button" id="password-addon"><i class="ri-eye-fill align-middle"></i></button>
                                                                        </div>
                                                                    <?php
                                                                     if(!empty($error_new_password)){
                                                                        echo '<h6 style="color:red;">'.$error_new_password.'</h6>';
                                                                     }
                                                                     ?>
                                                                    </div>
                                                                    
                                                                    <div class="col-md-4 col-sm-12">                                         
                                                                    <label class="form-label" for="password-input">Confirmation</label>
                                                                    <div class="position-relative auth-pass-inputgroup mb-3">
                                                                    <input type="password" class="form-control pe-5 password-input" onpaste="return false" placeholder="Confirmation" id="password-input" aria-describedby="passwordInput" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" name="confirm_new" >
                                                                         <button class="btn btn-link position-absolute end-0 top-0 text-decoration-none text-muted password-addon" type="button" id="password-addon"><i class="ri-eye-fill align-middle"></i></button>
                                                                        </div>
                                                                     <?php
                                                                     if(!empty($error_confirm_new)){
                                                                        echo '<h6 style="color:red;">'.$error_confirm_new.'</h6>';
                                                                     }
                                                                     ?>
                                                                    </div>
                                                                    <div id="password-contain" class="p-3 bg-light mb-2 rounded">
                                                                                    <h5 class="fs-13">Votre mot de passe doit contenir:</h5>
                                                                                    <p id="pass-length" class="invalid fs-12 mb-2">Au moins <b>8 caractères</b></p>
                                                                                    <p id="pass-lower" class="invalid fs-12 mb-2">Au moins une lettre <b>miniscule</b> (a-z)</p>
                                                                                    <p id="pass-upper" class="invalid fs-12 mb-2">Au moins un lettre <b>majuscule</b> (A-Z)</p>
                                                                                    <p id="pass-number" class="invalid fs-12 mb-0">Au moins un <b>nombre</b> (0-9)</p>
                                                                                </div>
                                                                    <div class="col-sm-12">
                                                                        <input style="background:#1E2762;" type="submit" name="update2" class="btn btn-primary" value="Modifier"/>
                                                                        
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </form>
                                        </div>
                                        
                                    </div>
                                </div><!-- end card-body -->
                            </div><!-- end card -->
                        </div>
                        <!--end col-->
                    </div>
                    <!--end row-->
                    <!-- container-fluid -->
                </div>
                <!-- End Page-content -->

                

                <?php include ('footer.php'); ?>
            </div>
            <!-- end main content-->

        </div>
        <!-- end main content-->

    </div>
    <!-- END layout-wrapper -->



    <!--start back-to-top-->
    <button onclick="topFunction()" class="btn btn-danger btn-icon" id="back-to-top">
        <i class="ri-arrow-up-line"></i>
    </button>
    <!--end back-to-top-->

    <!--preloader-->
    <div id="preloader">
        <div id="status">
            <div class="spinner-border text-primary avatar-sm" role="status">
                <span class="visually-hidden">Loading...</span>
            </div>
        </div>
    </div>

    

    <!-- JAVASCRIPT -->
     <script src="assets/libs/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="assets/libs/simplebar/simplebar.min.js"></script>
    <script src="assets/libs/node-waves/waves.min.js"></script>
    <script src="assets/libs/feather-icons/feather.min.js"></script>
    <script src="assets/js/pages/plugins/lord-icon-2.1.0.js"></script>
    <script src="assets/js/plugins.js"></script>
    <!-- prismjs plugin -->
    <script src="assets/libs/prismjs/prism.js"></script>
    <script src="assets/libs/list.js/list.min.js"></script>
    <script src="assets/libs/list.pagination.js/list.pagination.min.js"></script>

    <!-- listjs init -->
    <script src="assets/js/pages/listjs.init.js"></script>

    <!-- Sweet Alerts js -->
    <script src="assets/libs/sweetalert2/sweetalert2.min.js"></script>

    <!-- App js -->
    <script src="assets/js/app.js"></script>
    <script type="text/javascript">
        function rtn() {
            window.history.back();
        }
    </script>
</body>

</html>