<%- include('./partials/header'); %>
<script src="https://hcaptcha.com/1/api.js" defer></script>
<div class="row mt-5">
    <div class="col-md-6 m-auto">
        <div class="card card-body" style="background-color: #b1cbe6;">
            <h1 class="text-center">Sign Up</h1>
            <br>
            <form action="/signup" id="signup-form" method="POST">
                <div id="signup-error" class="alert alert-danger alert-dismissible fade show" role="alert">
                    <span id="signup-error-message"></span>
                </div>
                <div class="form group">
                    <label for="username">Username</label>
                    <input type="username" name="signup-username" class="form-control" placeholder="Username"
                        autocomplete="username" required>
                    <br>
                    <label for="email">Email</label>
                    <input type="email" name="signup-email" class="form-control" placeholder="Email"
                        autocomplete="email" required>
                    <br>
                    <label for="password">Password</label>
                    <input type="password" name="signup-password" class="form-control" placeholder="Password"
                        autocomplete="new-password" required>
                    <br>
                    <label for="password">Confirm Password</label>
                    <input type="password" name="signup-password-confirm" class="form-control"
                        placeholder="Confirm Password" autocomplete="new-password" required>
                    <br>
                    <div class="h-captcha" data-sitekey="b5bed0d5-a077-493d-9ae0-3a66e4d05a6c"></div>
                    <br>
                    <input type="submit" id="signup-button" value="Sign Up" class="btn btn-primary btn-block">
                </div>
            </form>
        </div>
    </div>
</div>

<%- include('./partials/footer'); %>

<script>
    $(document).ready(() => $(`#signup-error`).hide());

    $(`#signup-button`).on(`click`, e => {
        e.preventDefault();

        $(`#signup-button`).attr(`disabled`, true);
        $(`#signup-error`).hide();

        $.ajax({
            type: `post`,
            url: `/signup`,
            data: $(`#signup-form`).serialize()
        }).then(res => {
            if (res.errors) {
                $(`#signup-button`).attr(`disabled`, false);
                $(`#signup-error`).show();
                hcaptcha.reset();

                $(`#signup-error-message`).text(res.errors);
                console.error(`[ACCOUNT SERVER]: ${JSON.stringify(res.errors)}`);
            } else if (res.success) {
                $(`#signup-button`).attr(`disabled`, true);
                $(`#signup-error`).show();

                $(`#signup-error-message`).text(res.success);
                console.log(`[ACCOUNT SERVER]: ${JSON.stringify(res.success)}`);

                setTimeout(() => window.location.href = `${window.location.protocol}//${window.location.host}/login`, 1e4);
            }
        });
    });
</script>