<template>
    <!-- Modal -->
    <div
        :id="modalId"
        class="modal fade modal-component"
        tabindex="-1"
        aria-labelledby="ModalComponentLabel"
        aria-hidden="true"
        @keydown.enter="onKeyDown"
        v-on="{
            'hidden.bs.modal': onModalClosed,
            'shown.bs.modal': onModalOpened
        }">
        <div class="modal-dialog modal-dialog-centered">
            <div
                class="modal-content"
                role="dialog"
                aria-modal="true">
                <div class="modal-header mb-2 mt-2">
                    <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"
                        aria-label="Close"
                        @mousedown="closeModal"
                        @keypress.space="closeModal"></button>
                </div>
                <div class="modal-body ps-5 pe-5 pb-5 pt-1">
                    <slot></slot>
                </div>
                <div class="modal-footer px-5 py-4">
                    <modalButtonMain
                        id="modalbtn"
                        ref="modalButtonMain"
                        isPrimary
                        class="w-100"
                        loaderColor="white"
                        :buttonText="footerButtonText"
                        :class="
                            (isButtonDisabled || isFooterButtonDisabled) &&
                            'form-test-invalid'
                        "
                        @clickEvent="validateAndEmit" />
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import modalButtonMain from '@/ux-components/modal-button-main/modal-button-main.vue';
import { Modal } from 'bootstrap';
import { useForm } from 'vee-validate';

export default {
    // eslint-disable-next-line vue/multi-word-component-names
    name: 'modal',
    components: {
        modalButtonMain
    },
    props: {
        modalId: String,
        headerText: String,
        footerButtonText: String,
        onModalOpenedCallback: {
            type: Function
        },
        onModalClosedCallback: {
            type: Function
        },
        isButtonDisabled: Boolean
    },
    emits: ['footer-button-event', 'isModalOpened'],
    setup(props) {
        const modalId = props.modalId
            ? props.modalId
            : `modal-${crypto.randomUUID()}`;

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

        // TODO: Correct Duplicate key 'modalId' issue.
        // Probably just rename the prop. -br
        return {
            // eslint-disable-next-line vue/no-dupe-keys
            modalId,
            meta,
            validate,
            resetForm
        };
    },
    computed: {
        isFooterButtonDisabled() {
            if (!this.meta.touched) {
                return !this.meta.valid;
            }
            return !this.meta.dirty || !this.meta.valid;
        }
    },
    watch: {
        $route(newVal, oldVal) {
            this.closeModal();
        }
    },
    methods: {
        async validateAndEmit() {
            const validationResult = await this.validate();
            if (validationResult.valid) {
                this.$emit('footer-button-event');
            } else {
                this.resetButtonStyle();
            }
        },
        async onKeyDown(e) {
            e.preventDefault();
            document.getElementById('modalbtn')?.focus();
            this.$refs.modalButtonMain.clicked();
            document.getElementById(this.modalId)?.focus();
        },
        resetButtonStyle() {
            this.$refs.modalButtonMain?.resetButtonStyle();
        },
        onModalOpened() {
            this.onModalOpenedCallback?.();
        },
        onModalClosed() {
            this.resetButtonStyle();
            this.onModalClosedCallback?.();
        },
        openModal() {
            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);
        }
    }
};
</script>

<style lang="scss">
@import "@/styles/ux-variables-svg-strings.scss";

.modal {
    top: auto;
    bottom: 0;
    h5,
    strong,
    .subheader-text {
        color: $black;
    }
    .modal-header {
        padding: 1.5rem 0 0.5rem 0;
        border-bottom: none;
        .btn-close {
            position: absolute;
            right: 1rem;
            top: 1rem;
            background-image: url($svg-modal-header-button-close);
            opacity: 1;
        }

        .modal-title {
            font-weight: 500;
            font-size: 1.25rem;
            line-height: 2rem;
            color: $black;
        }
    }
    .modal-footer {
        position: sticky;
        width: 100%;
        bottom: 0;
        z-index: 5;
        border-radius: 0;
        background-color: $gray-100;
    }
    &.modal-component {
        .modal-dialog {
            max-width: 47.9375rem;
            margin: 0 auto;
            @include media-breakpoint-up(md) {
                width: 23.5rem;
                position: absolute;
                top: 0;
                right: 0;
                transform: translate(100%, 0);
            }
            .modal-content {
                margin: 1.5rem auto 0 auto;
                box-shadow: 0px 1rem 3rem -1rem 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 {
                    .col {
                        max-width: 100%;
                    }
                    .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;
                    }
                    h5 {
                        text-align: center;
                        line-height: 2rem;
                    }
                }
            }
            &.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>
