<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>LittlePrynter</title>
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
        <link rel="stylesheet" href="{{ url_for('static',filename='css/style.css') }}">
    </head>
    <body class="container">
        <div class="col-md-6 offset-md-3">
            <h1 class="font-weight-bold">Little Prynter</h1>
            <hr>
            <br>
            {% with messages = get_flashed_messages() %}
              {% if messages %}
                {% for message in messages %}
                  <div class="alert alert-danger" role="alert">{{ message }}</div>
                {% endfor %}
              {% endif %}
            {% endwith %}
            <br>
            <h3>Imprimer une image aléatoire</h3>
            <a class="btn btn-primary btn-lg btn-block" href="/print/image"> Imprimer</a>
            <hr>
            <br>
            <h3>Imprimer ton propre texte</h3>
            <form class="form-group" action="/print/text" method="post">
                <input class="form-control" type="text" name="message" placeholder="200 caractères ou moins " maxlength="200"><br>
                <input class="btn btn-primary float-right" type="submit" value="Imprimer" name="imprimer">
            </form>

            <div id="pbar" style="display:none">
                <img src="{{ url_for('static',filename='load.gif') }}" alt="loading wheel">
            </div>
            <br>
            <br>
            <footer><a href="/logout">Se déconnecter</a></footer>
        </div>
    </body>

    <script>

    function printImage() {
        // Make an ajax call to the flask server on the /print/image route to trigger an image to be printer.
        var xhttp = new XMLHttpRequest();
         xhttp.onreadystatechange = function() {
             var x = document.getElementById("pbar");
                if (x.style.display === "none") {
                    x.style.display = "inline";
                }
           if (this.readyState == 4 && this.status == 429) {
               x.style.display = "none";
               alert("Hé, attends un peu avant d'imprimer une autre image, je me repose...");
           } else if (this.readyState == 4 && this.status == 200) {
               x.style.display = "none";
           }
         };
         xhttp.open("GET", "/print/image", true);
         xhttp.send();
    }

    function printText(){
        var xhttp = new XMLHttpRequest();
         xhttp.onreadystatechange = function() {
             if (this.readyState == 4 && this.status == 400) {
               alert("Le text est trop long !");
           }
         };
         xhttp.open("GET", "/print/text", true);
         xhttp.send();
    }

    </script>
</html>