<?php
    session_start();

    include 'db_conn.php';

    if (isset($_REQUEST['privilege_level']))
    {
        $pl = $_REQUEST['privilege_level'];
        $token = bin2hex(openssl_random_pseudo_bytes(32));

        $statement = $db->prepare("INSERT INTO Invites (token, privilege_level) VALUES (:token, :pl);");
        $statement->execute(array(
            "token" => $token,
            "pl" => $pl
        ));

        $url = "https://oggle.segfault.party/?invite_token=" . $token;

        echo "<h1>Invite link:</h1>";
        echo "<a href='$url'>$url</a><br/>";
        echo "<img src='https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=" . urlencode($url) . "' alt='' title='' />";
        die;
    }
?>
<script>
    function reloadGenInvite(e)
    {
        $.post('/generate_invite.php', $(e).serialize(), function(data)
        {
            $("#invite-content")[0].innerHTML = data;
        });
    }
    </script>
<div id="invite-content" class="pad-20 pad-20-around">
    <h1>Get invite token</h1>

    <form action="javascript:;" onsubmit="reloadGenInvite(this)" id="genInviteForm" method="post">
        <select name="privilege_level">
            <option value="1">Admin</option>
            <option value="2">Redacteur</option>
            <option value="4">Gebruiker</option>
        </select>
        <input type="submit" value="Get invite token"/>
    </form>
</div>
