<?php
ob_start();
require('model.php');
$sql = new model;

if (isset($_POST['login'])) { 
     
    if(!empty($_POST['username'])){

        if(!empty($_POST['password'])){

            $login = $sql->login($_POST['username']); 

            $count=$login->rowCount();

            while ($requestLogins = $login->fetch()) {

                if($count>0){

                    $access= password_verify($_POST['password'], $requestLogins['password']);
                    
                    if ($access == 1) {


                        $_SESSION['admin_id']=$requestLogins['id'];

                        $_SESSION['admin_username']=$requestLogins['username'];

                        $_SESSION['admin_password']=$requestLogins['password'];


                        if(!empty($requestLogins['username']) && $requestLogins['valid'] == 0){ 

                            $error_username="Votre compte a été desactivé. Veuillez conctater l'administrateur général"; 
            
                        }elseif(!empty($requestLogins['username']) && $requestLogins['valid'] == 1){ 

                            header('location:index.php');

                        }

                    } else { 

                        $error_password="Erreur, mot de passe incorrect"; 

                    } 

                }else $error_username="Ce compte n'existe pas"; 

            }

        }else $error_password="Entrez le mot de passe"; 

    }else $error_username="Entrez le nom d'utilisateur"; 

}
?>
<!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">
<?php include('head.php'); ?>

<body>

    <!-- auth-page wrapper -->
    <div class="auth-page-wrapper auth-bg-cover py-5 d-flex justify-content-center align-items-center min-vh-100">
        <div style="background:#FFF;" class="bg-overlay"></div>
        <!-- auth-page content -->
        <div class="auth-page-content overflow-hidden pt-lg-5">
            <div class="container">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="card overflow-hidden">
                            <div class="row g-0">
                                <div class="col-lg-6">
                                    <div class="p-lg-5 p-4 auth-one-bg h-100">
                                        <div style="background:gray; opacity:0.7;" class="bg-overlay"></div>
                                        <div class="position-relative h-100 d-flex flex-column">
                                            <div class="mb-4">
                                                <a href="" class="d-block">
                                                    <img src="assets/images/LOG1_dark.png" alt="" height="70">
                                                </a>
                                            </div>
                                            <div class="mt-auto">
                                                

                                                <!--<div id="qoutescarouselIndicators" class="carousel slide" data-bs-ride="carousel">
                                                    <div class="carousel-indicators">
                                                        <button type="button" data-bs-target="#qoutescarouselIndicators" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1"></button>
                                                        <button type="button" data-bs-target="#qoutescarouselIndicators" data-bs-slide-to="1" aria-label="Slide 2"></button>
                                                        <button type="button" data-bs-target="#qoutescarouselIndicators" data-bs-slide-to="2" aria-label="Slide 3"></button>
                                                    </div>
                                                    <div class="carousel-inner text-center text-white pb-5">
                                                        <div class="carousel-item active">
                                                            <p class="fs-15 fst-italic">"Numérisez vos archives pour une conservation indéfinie"</p>
                                                        </div>
                                                        <div class="carousel-item">
                                                            <p class="fs-15 fst-italic">"Edition automatique des bulletins"</p>
                                                        </div>
                                                        <div class="carousel-item">
                                                            <p class="fs-15 fst-italic">"Un logiciel tout en un"</p>
                                                        </div>
                                                    </div>
                                                </div>
                                                 end carousel -->
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <!-- end col -->

                                <div class="col-lg-6">
                                    <div class="p-lg-5 p-4">
                                        <div>
                                            <h5 style="color:#65214D;" class="text">Content de vous revoir !</h5>
                                        </div>

                                        <div class="mt-4">
                                            <form action="" method="post">

                                                <div class="mb-3">
                                                    <label for="username" class="form-label">Nom d'utilisateur</label>
                                                    <input type="text" class="form-control" id="username" name="username" required>
                                                    <?php
                                                        if(!empty($error_username)){
                                                            echo '<h6 style="color:red;">'.$error_username.'</h6>';
                                                        }
                                                    ?>
                                                </div>

                                                <div class="mb-3">
                                                    <!-- <div class="float-end">
                                                        <a href="auth-pass-reset-cover.html" class="text-muted">Forgot password?</a>
                                                    </div> -->
                                                    <label class="form-label" for="password-input">Mot de passe</label>
                                                    <div class="position-relative auth-pass-inputgroup mb-3">
                                                        <input type="password" class="form-control pe-5 password-input" name="password" placeholder="Mot de passe" id="password-input" required>
                                                        <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="form-check">
                                                    <input class="form-check-input" type="checkbox" value="" id="auth-remember-check">
                                                    <label class="form-check-label" for="auth-remember-check">Remember me</label>
                                                </div> -->

                                                <div class="mt-4">
                                                    <button style="background:#65214D;" class="btn btn-success w-100" name="login" type="submit">Connexion</button>
                                                </div>

                                                

                                            </form>
                                        </div>

                                        <!-- <div class="mt-5 text-center">
                                            <p class="mb-0">Don't have an account ? <a href="auth-signup-cover.html" class="fw-semibold text-primary text-decoration-underline"> Signup</a> </p>
                                        </div> -->
                                    </div>
                                </div>
                                <!-- end col -->
                            </div>
                            <!-- end row -->
                        </div>
                        <!-- end card -->
                    </div>
                    <!-- end col -->

                </div>
                <!-- end row -->
            </div>
            <!-- end container -->
        </div>
        <!-- end auth page content -->

        <!-- footer -->
        <footer class="footer">
            <div class="container">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="text-center">
                            <p class="mb-0">
                               <span style="color:#000000;"> &copy; <script>document.write(new Date().getFullYear())</script> Vision million | Développeé par</span> <a style="text-decoration:none; color:#1E2762;" target="_blank" href="https://african-technologies.com"><b>AFRICAN TECHNOLOGIES</b></a>
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </footer>
        <!-- end Footer -->
    </div>
    <!-- end auth-page-wrapper -->

    <!-- 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>

    <!-- password-addon init -->
    <script src="assets/js/pages/password-addon.init.js"></script>
</body>

</html>