<?php
require_once('template_header.php');
?>

<main role="main" class="container starter-template">
 
    <div class="row">
        <div class="col">
 
            <!-- where prompt / messages will appear -->
            <div id="response"></div>
 
            <div>
            <form id="login_form" method="POST">
                <table>
                <tr>
                    <th>Login :</th>
                    <td><input type="text" id ='login' name="login"></td>
                </tr>
                <tr>
                    <th>Mot de passe :</th>
                    <td><input type="password" id='password' name="password"></td>
                </tr>
                <tr>
                    <th></th>
                    <td><input type="submit" value="Se connecter..." /></td>
                </tr>
                <tr>
                    <a href="signup.php">S'inscrire</a>
                </tr>
                </table>
             </form>
             </div>

        </div>
    </div>
</main>

<script
    src="https://code.jquery.com/jquery-3.4.1.min.js"
    integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
    crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-serialize-object/2.5.0/jquery.serialize-object.min.js" ></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>

<!-- jquery scripts will be here -->


<script type="text/javascript">
$(document).ready(function() {
    // will run if create identite form was submitted
    $(document).on('submit', '#login_form', function(){
        // get form data
        var form_data=JSON.stringify($(this).serializeObject());

        // submit form data to api
        $.ajax({
            url: "https://eden.imt-lille-douai.fr/~liang.wu/API_LOGIN/identite/login.php",
            type : "POST",
            contentType : 'application/json',
            data : form_data,
            success : function(result){
                var ac_log=JSON.parse(form_data);
                document.cookie ="IdentifiantActuel="+ac_log.login;
                window.location="https://eden.imt-lille-douai.fr/~liang.wu/projet_idaw/home.php";
                
            },
            error: function(xhr, resp, text){
            // on error, tell the user login has failed & empty the input boxes
            $('#response').html("<div class='alert alert-danger'>Login failed. login or password is incorrect.</div>");
            login_form.find('input').val('');
            }
        });
    });
});
</script>

            
<?php
    require_once('template_footer.php');
?>