<template>
    <!-- Modal -->
    <div
        class="modal fade modal-component"
        :class="modalId"
        v-on="{ 'hidden.bs.modal': onModalClosed, 'shown.bs.modal': onModalOpened }"
        :data-bs-backdrop="backdropSetting"
        :id="modalId"
        tabindex="-1"
        aria-labelledby="ModalComponentLabel"
        @keypress.enter="onEnter"
        aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header mb-0 mt-6">
                    <label
                        v-if="headerText"
                        class="modal-title d-flex justify-content-center pb-0 w-100">
                        {{ headerText }}
                    </label>
                    <button
                        ref="closeButton"
                        type="button"
                        class="btn-close"
                        @mousedown="closeModal"
                        aria-label="Close"></button>
                </div>
                <div class="modal-body ps-5 pe-5 pb-4 pt-1">
                    <slot></slot>
                </div>
                <div class="modal-footer">
                    <slot name="modal-footer-slot"></slot>
                    <modalButtonMain
                        isPrimary
                        class="w-100 modal-footer-button"
                        :id="modalId + '-modalbtn'"
                        ref="modalButtonMain"
                        loaderColor="white"
                        :buttonText="footerButtonText"
                        @click-event="validateAndEmit"
                        :class="isFooterButtonDisabled && 'form-test-invalid'" />
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { fmgPageValues } from "@/router/router-constants/fmgPage-values";
import analyticsMixin from "@/mixins/analytics-mixin";
import router from "@/router/index.js";
import modalButtonMain from "@/digital-components/modal/ux-components/modal-button-main/modal-button-main";
import { Modal } from "bootstrap";
import { useForm } from "vee-validate";
import { v4 as uuidv4 } from "uuid";

export default {
    name: "modal",
    props: {
        headerText: String,
        footerButtonText: String,
        suppressPageScroll: Boolean,
        staticBackdrop: Boolean,
        onModalOpenedCallback: {
            type: Function,
        },
        onModalClosedCallback: {
            type: Function,
        },
    },
    setup() {
        const uuid = uuidv4();
        const modalId = `modal-${uuid}`;

        const { meta, validate, resetForm } = useForm();

        return {
            modalId,
            meta,
            validate,
            resetForm,
        };
    },
    data() {
        return {
            isEnterClicked: false,
        };
    },
    methods: {
        async validateAndEmit() {
            const validationResult = await this.validate();
            if (validationResult.valid) {
                if (analyticsMixin.methods.sessionExpired()) {
                    analyticsMixin.methods.initSession();
                    router.push({
                        path: "/",
                        query: { fmgPage: fmgPageValues.RETURN_USER },
                    });
                } else {
                    this.$emit("footer-button-event");
                }
            } else {
                this.resetButtonStyle();
            }
        },
        async onEnter(e) {
            e.preventDefault();
            if (this.isEnterClicked) return;
            this.isEnterClicked = true;
            document.getElementById(this.modalId + "-modalbtn")?.focus();
            this.$refs.modalButtonMain.clicked();
            document.getElementById(this.modalId)?.focus();
            setTimeout(() => {
                this.isEnterClicked = false;
            }, 2000);
        },
        resetButtonStyle() {
            this.$refs.modalButtonMain?.resetButtonStyle();
        },
        onModalOpened() {
            this.onModalOpenedCallback?.();
        },
        onModalClosed() {
            this.resetButtonStyle();
            this.onModalClosedCallback?.();
            if (this.suppressPageScroll)
                document.querySelector("body").classList.remove("prevent-modal-scroll");
        },
        openModal() {
            if (this.suppressPageScroll)
                document.querySelector("body").classList.add("prevent-modal-scroll");
            const modal = Modal.getOrCreateInstance(document.getElementById(this.modalId));
            modal.show();
            this.$emit("isModalOpened", true);
        },
        closeModal() {
            const modal = Modal.getInstance(document.getElementById(this.modalId));
            modal.hide();
            this.$emit("isModalOpened", false);
        },
    },
    computed: {
        isFooterButtonDisabled() {
            if (!this.meta.touched) {
                return !this.meta.valid;
            }
            return !this.meta.dirty || !this.meta.valid;
        },
        backdropSetting() {
            return this.staticBackdrop ? "static" : "true";
        },
    },
    components: {
        modalButtonMain,
    },
};
</script>

<style lang="scss">
.modal {
    top: auto;
    bottom: 0;
    h5,
    strong,
    .subheader-text {
        color: $black;
    }
    .modal-header {
        padding: 1rem 0 0.5rem 0;
        border-bottom: none;
        .btn-close {
            position: absolute;
            right: 1rem;
            top: 1rem;
            background-image: url("data:image/svg+xml,%3Csvg width='16' height='16' viewBox='0 0 16 16' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M15.6874 1.82179L9.50889 8L15.6874 14.1782C16.1042 14.595 16.1042 15.2707 15.6874 15.6875C15.4843 15.8907 15.2146 16 14.9328 16C14.6514 16 14.3818 15.8906 14.1787 15.6875L8.00017 9.50934L1.82171 15.6875C1.6182 15.8907 1.34876 16 1.06708 16C0.785733 16 0.516056 15.8906 0.312965 15.6875C-0.10429 15.2706 -0.10429 14.5952 0.312797 14.1784L6.03276 8.45867L6.49146 8L0.312945 1.82177C-0.10429 1.40483 -0.10429 0.729418 0.312797 0.31262C0.728936 -0.104145 1.40489 -0.104051 1.82185 0.31262L7.54152 6.03202L8.00017 6.49065L14.1786 0.312472C14.5955 -0.104107 15.2707 -0.104201 15.6874 0.312452C16.1042 0.729289 16.1042 1.40496 15.6874 1.82179Z' fill='%231574A1'/%3E%3C/svg%3E%0A");
            opacity: 1;
            margin: 0.3rem -0.5rem -0.5rem auto;
        }

        .modal-title {
            font-weight: 500;
            font-size: 1.25rem;
            line-height: 32px;
            color: rgb(0, 0, 0);
        }
    }
    .modal-footer {
        padding: 1rem 1.5rem;
        position: sticky;
        width: 100%;
        bottom: 0;
        z-index: 5;
        background-color: $white;
    }
    &.modal-component {
        .modal-dialog {
            max-width: 767px;
            margin: 0 auto;
            @include media-breakpoint-up(md) {
                width: 376px;
                position: absolute;
                top: 0;
                right: 0;
                transform: translate(100%, 0);
            }
            .modal-content {
                margin: 1.5rem auto 0 auto;
                box-shadow: 0px 16px 48px -16px rgba(0, 0, 0, 0.25);
                border-radius: 1.5rem 1.5rem 0 0;
                @include media-breakpoint-up(md) {
                    box-shadow: none;
                    border-radius: 0;
                    margin: 0 auto;
                }
                .modal-body {
                    .modal-sub-body {
                        color: $gray-600;
                    }
                    ul {
                        margin-bottom: 0;
                    }
                    p {
                        &:last-child {
                            margin-bottom: 0;
                        }
                    }
                    @include media-breakpoint-up(md) {
                        overflow: auto;
                        flex: none;
                    }
                    .textbox-question {
                        padding: 0.25rem 0;
                    }
                }
            }

            &.modal-dialog-centered {
                align-items: flex-end;
                min-height: 100%;
                @include media-breakpoint-up(md) {
                    align-items: inherit;
                }
            }
        }
        .modal-footer {
            border-top: none;

            button {
                margin: 0;
            }
        }
    }
    &.show .modal-dialog {
        transform: translateY(0);
    }
    @include media-breakpoint-up(md) {
        &.fade {
            transition: opacity 0.25s linear;
        }
    }
}

body {
    .modal-backdrop {
        height: 100%;
        &.show {
            opacity: 0.4;
        }
    }
}
</style>
