<template>
    <Form @submit="onSubmit" @invalid-submit="onInvalidSubmit" ref="theForm">
        <div class="return-user small-question-text">
            <loadingModal ref="loadingModal" />
            <div class="container-fluid page-container-grouped-styles">
                <div class="row justify-content-center">
                    <div class="col-md-6">
                        <funnelHeader cmsWidgetName="FunnelHeaderWidget" ref="funnelHeader" />
                    </div>
                </div>
                <div class="row justify-content-center return-user-spacing">
                    <div class="col-md-6 col-xl-4 mt-4">
                        <funnelSubHeader
                            ref="funnelSubHeader"
                            cmsWidgetName="FunnelSubHeaderWidget" />
                    </div>
                </div>
                <div class="row justify-content-center">
                    <div class="col-md-6 col-xl-4">
                        <navbar
                            cmsWidgetName="FunnelFooterWidget"
                            ref="navbar"
                            buttonSize
                            :isBackButtonHidden="true"
                            @ForwardClicked="forwardButtonAction" />
                    </div>
                </div>
                <div class="row justify-content-center">
                    <div class="col-md-6 col-xl-4 start-over">
                        <a @click="startOver" alt="Start Over Button">
                            <img v-bind:src="startOverImage" />
                            {{ startOverText }}
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </Form>
</template>

<script>
// Components
import funnelHeader from "@/fmg-components/funnel-header/funnel-header";
import funnelSubHeader from "@/fmg-components/funnel-sub-header/funnel-sub-header";
import navbar from "@/fmg-components/nav-bar/nav-bar";
// Supporting Files
import { fetchCmsContentForPage } from "@/helpers/cms-content-helper";
import { settleAllPromises } from "@/helpers/layout-helper";
import { Form } from "vee-validate";
import { getFunnelCookie } from "@/helpers/heritage-integration/cookie-helper";
import { deleteFunnelCookie } from "@/helpers/heritage-integration/cookie-helper";
import { storeActions } from "@/constants/store-actions";

export default {
    name: "return-user",
    async beforeRouteEnter(to, from, next) {
        // Call APIs
        const cmsContentPromise = fetchCmsContentForPage(to.query.fmgPage);
        // Settle promises and get results
        const promiseResultMap = [
            {
                resultKey: "cmsContent",
                promise: cmsContentPromise,
            },
        ];
        const resultMap = await settleAllPromises(promiseResultMap);
        // Call the "next" function to complete the transition to this page.
        next((vm) => {
            vm.setCmsContent(resultMap.cmsContent);
            vm.startOverText = resultMap.cmsContent.StartOverWidget.HeaderText;
            vm.startOverImage = resultMap.cmsContent.StartOverWidget.Image;
        });
    },
    data() {
        return {
            startOverText: "",
            startOverImage: null,
        };
    },
    methods: {
        arePagePrerequisitesValid() {
            return getFunnelCookie() !== null;
        },

        async forwardButtonAction() {
            this.$router.navigateWithoutSaving(
                this.navigationScenarios.CLICKED_FORWARD,
                this.$route
            );
        },

        async startOver() {
            this.pushEventToGA(
                this.$route.query[this.queryStrings.FMG_PAGE],
                this.GaActions.CLICKED,
                "Start over",
                true
            );
            deleteFunnelCookie();
            await this.dispatchStoreAction(storeActions.RESET_STATE);
            this.$router.navigateWithoutSaving(
                this.navigationScenarios.CLICKED_FORWARD,
                this.$route
            );
        },
    },
    components: {
        Form,
        funnelHeader,
        funnelSubHeader,
        navbar,
    },
};
</script>
<style lang="scss" scoped>
.start-over {
    text-align: center;
    a {
        font-weight: 600;
        color: $black;
        cursor: pointer;
        text-decoration: underline;
    }
}
.return-user {
    .return-user-spacing {
        margin-top: 14rem;
    }
}
</style>
