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

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

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

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

            $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'];

                        header('location:index.php');
                            

                        

                    } else { 

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

                    } 

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

            }

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

    }else $error_email="Entrez l'adresse email"; 

}
?>
<!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>ADMIN Agritraders</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta content="Agritraders" name="description" />
    <meta content="Agritraders" name="Agritraders" />
    <!-- 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" />


</head>

<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>
                                        </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>
                                            <p class="text-muted">Connectez-vous pour accéder à Agritraders.</p>
                                        </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="email" placeholder="Username" required>
                                                    <?php
                                                        if(!empty($error_email)){
                                                            echo '<h6 style="color:red;">'.$error_email.'</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> Agritraders. Conçu avec <i class="mdi mdi-heart text-danger"></i> par </span> <a style="color:#1E2762;" href="https://isaac-digital.fr" target="_blank">Isaac Digital</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>