/* =========================================================
   COMPLETE BLISS - REGISTRATION
========================================================= */

document.addEventListener(
    "DOMContentLoaded",
    function () {


        /* =====================================================
           PASSWORD TOGGLES
        ====================================================== */

        const passwordToggles =
            document.querySelectorAll(
                ".cbx-password-toggle"
            );


        passwordToggles.forEach(
            function (button) {

                button.addEventListener(
                    "click",
                    function () {

                        const targetId =
                            this.dataset.target;


                        const input =
                            document.getElementById(
                                targetId
                            );


                        if (!input) {
                            return;
                        }


                        const showPassword =
                            input.type === "password";


                        input.type =
                            showPassword
                                ? "text"
                                : "password";


                        const icon =
                            this.querySelector("i");


                        if (icon) {

                            icon.classList.toggle(
                                "fa-eye",
                                !showPassword
                            );


                            icon.classList.toggle(
                                "fa-eye-slash",
                                showPassword
                            );

                        }


                        this.setAttribute(
                            "aria-label",
                            showPassword
                                ? "Hide password"
                                : "Show password"
                        );

                    }
                );

            }
        );



        /* =====================================================
           PAYMENT RECEIPT FILE NAME
        ====================================================== */

        const receiptInput =
            document.getElementById(
                "receipt"
            );


        const receiptFileName =
            document.getElementById(
                "receiptFileName"
            );


        if (
            receiptInput &&
            receiptFileName
        ) {

            receiptInput.addEventListener(
                "change",
                function () {

                    if (
                        this.files &&
                        this.files.length > 0
                    ) {

                        receiptFileName.textContent =
                            this.files[0].name;

                    } else {

                        receiptFileName.textContent =
                            "JPG, PNG or PDF";

                    }

                }
            );

        }



        /* =====================================================
           COPY ACCOUNT NUMBER
        ====================================================== */

        const copyButton =
            document.getElementById(
                "copyAccount"
            );


        const accountNumber =
            document.getElementById(
                "accountNumber"
            );


        if (
            copyButton &&
            accountNumber
        ) {

            copyButton.addEventListener(
                "click",
                async function () {

                    const account =
                        accountNumber.textContent.trim();


                    if (!account) {
                        return;
                    }


                    try {

                        await navigator.clipboard.writeText(
                            account
                        );


                        showCopiedMessage();

                    } catch (error) {

                        fallbackCopy(
                            account
                        );

                    }

                }
            );

        }



        function fallbackCopy(text) {

            const temporaryInput =
                document.createElement(
                    "input"
                );


            temporaryInput.value =
                text;


            document.body.appendChild(
                temporaryInput
            );


            temporaryInput.select();


            temporaryInput.setSelectionRange(
                0,
                99999
            );


            document.execCommand(
                "copy"
            );


            document.body.removeChild(
                temporaryInput
            );


            showCopiedMessage();

        }



        function showCopiedMessage() {

            if (
                typeof Swal !==
                "undefined"
            ) {

                Swal.fire({

                    icon:
                        "success",

                    title:
                        "Copied!",

                    text:
                        "Account number copied successfully.",

                    timer:
                        1400,

                    showConfirmButton:
                        false

                });

            }

        }

    }
);