<?php include($_SERVER['DOCUMENT_ROOT'] . "/COMPONENTS/root/path.php"); ?>
<div class="p-3">
    <div class="text-center">
        <a href=""> <img src="<?php echo $website_name; ?>/assets/images/<?php echo $image; ?>" width="350px" alt="wrapkit"></a>
    </div>
    <h2 class="mt-3 text-center">Sign Up</h2>
    <p class="text-center">Enter you signup details</p>
    <form method='POST' action='' id='form_signup' autocomplete='off' validate>
        <div class="row">
            <div class="col-lg-12">
                <div class="form-group">
                    <select class="form-control" name="unique_user_id" id="unique_user_id" required>
                        <option value="" selected disabled>Select your name</option>
                        <?php
                        $query = mysqli_query($conn, "SELECT * FROM users");
                        while ($spec = mysqli_fetch_assoc($query)) {
                            echo "<option value='" . $spec["unique_user_id"] . "'>" . $spec["fullname"] . "</option>";
                        }
                        ?>
                    </select>
                </div>
            </div>

            <div class="col-lg-12">
                <div class="form-group">
                    <input class="form-control" id="username" name="username" type="text" placeholder="Enter your preferred username" required>
                </div>
            </div>

            <div class="col-lg-12">
                <div class="form-group">
                    <input class="form-control" id="password" name="password" type="password" placeholder="Enter your preferred password" required>
                </div>
            </div>
            <div class="col-lg-12 text-center">
                <button type="submit" id="submit_signup" class="btn btn-block btn-dark"><span class='submitLoader'>Sign Up</span></button>
            </div>
            <div class="col-lg-12 text-center mt-5">
                Already have an account? <a href="#" class="text-danger get-inn" data-url="signin/form.php">Sign In</a>
            </div>
        </div>
    </form>
</div>

<script>
    $("#submit_signup").on("click", function() {
        $("#form_signup").submit(function(e) {
            $.ajax({
                url: domain_api + "/signup/signup.php",
                type: "POST",
                data: $(this).serialize(),
                beforeSend: function() {
                    $("#submit_signup").attr("disabled", "disabled");
                    $(".submitLoader").html("<i class='fa fa-spinner fa-spin'></i>");
                },
                success: function(data) {
                    $(".submitLoader").html("Sign Up");
                    if (data.code == 200) {
                        swal("Succes!", data.message, "success");
                        $(".signin-signup-form").load(domain + "/COMPONENTS/signin/form.php", function() {})
                    } else {
                        swal("Oops!", data.message, "error");
                    }
                    $("#submit_signup").attr("disabled", false);

                },
                error: function(request, error) {
                    swal("Oops!", "Saving was not successful, you should try again!", "error");
                    $("#submit_signup").attr("disabled", false);
                },
            });
            e.preventDefault();
        });
        $(this).off();
    });

    $(".get-inn").off().on("click", function() {
        var url = $(this).attr("data-url");
        $(".signin-signup-form").load(domain + "/COMPONENTS/" + url, function() {})
    });
</script>