<?php require_once '../db.php';
    session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="../css/login.css">
    <title>Modeles | Login</title>
</head>
<body>
    <div id="container">
        <form method="post" action="login.php" class="formulaire">
                <h1>Connexion</h1>
                <label for="txtlogin">Nom d'utilisateur : </label>
                <input type="text" name="login" class="zonetext" id="txtlogin"  required>
                <label for="pwd">Mot de passe : </label>
                <input type="password" name="pwd" id="txtpwd" class ="zonetext"  required>
                <input type="submit" name="btnlogin" class="submit" id="submit" value="Connecter">
                <?php 
                $sql="";
                 if (isset($_POST['btnlogin'])) {
                    if (!empty($_POST['login']) AND !empty($_POST['pwd'])) {
                        $sql ="SELECT * FROM users WHERE login= ? AND password =?";
                    } else {
                        echo "<font color ='#ff0000'>Veuillez remplir tous les champs ..</font>";
                    }

                    $req = $pdo->prepare($sql);
                    $req->execute([$_POST['login'],$_POST['pwd']]);

                    if ($req) {
                        $result = $req->fetch();
                        if($result !=0){
                            $_SESSION['profil'] = $_POST['login'];
                            header('Location:index.php');
                        }else {
                            echo "<div style='color: #ff0000; text-align: center; margin-top: 10px;'>Le pseudo ou le mot de passe est incorrect.</div>";

                        }
                    } 
                
                }
        ?>
        </form>  
    </div>
</body>
</html>