<template>
    <Form @submit="onSubmit" @invalid-submit="onInvalidSubmit" ref="theForm" v-slot="{ meta }">
        <funnelHeader
            cmsWidgetName="FunnelHeaderWidget"
            ref="funnelHeader"
            :overrideImageSrc="clientLogoImageSrc" />
        <div class="page-gradient"></div>
        <div class="container">
            <div class="row">
                <div class="col-12 col-md-10 col-lg-8 col-xl-7">
                    <funnelSubHeader class="pb-4" cmsWidgetName="FunnelSubHeaderWidget" />
                    <!--
                            ... Page code goes here.
                    -->
                    <insuranceNavBar
                        cmsWidgetName="FunnelFooterWidget"
                        ref="navbar"
                        :isForwardActionDisabled="!meta.valid"
                        @cancel-clicked="cancelButtonAction"
                        @back-clicked="backButtonAction"
                        @ForwardClicked="forwardButtonAction" />
                </div>
            </div>
        </div>
    </Form>
</template>

<script>
import funnelHeader from "@/fmg-components/funnel-header/funnel-header";
import insuranceNavBar from "@/fmg-components/insurance-nav-bar/insurance-nav-bar";
import funnelSubHeader from "@/fmg-components/funnel-sub-header/funnel-sub-header";
import { Form } from "vee-validate";

import { fetchCmsContentForPage } from "@/helpers/cms-content-helper";
import { settleAllPromises } from "@/helpers/layout-helper";
import store from "@/store";
import { debugLog } from "@/helpers/debug-log-helper";

export default {
    name: "policy-driver",
    mixins: [],
    data() {
        return {
            clientConfig: {},
        };
    },
    components: {
        Form,
        funnelHeader,
        insuranceNavBar,
        funnelSubHeader,
    },
    // Ensure CMS content is fetched during route navigation without depending on `to`/`from`
    async beforeRouteEnter(to, from, next) {
        const pageName = to.name;
        const cmsContentPromise = fetchCmsContentForPage(pageName);
        const clientConfigPageName = `client_${store.getters.order.payment.parentAccountNumber}`;
        const clientConfigPromise = fetchCmsContentForPage(clientConfigPageName);
        const promiseResultMap = [
            {
                resultKey: "cmsContent",
                promise: cmsContentPromise,
            },
            {
                resultKey: "clientConfig",
                promise: clientConfigPromise,
            },
        ];
        const resultMap = await settleAllPromises(promiseResultMap);
        next((vm) => {
            vm.setCmsContent(resultMap.cmsContent);
            vm.clientConfig = resultMap.clientConfig;
            debugLog(
                `policy-driver clientConfig::${clientConfigPageName}`,
                JSON.stringify(vm.clientConfig)
            );
        });
    },

    computed: {
        clientLogoImageSrc() {
            return this.clientConfig?.ClientLogoWidget?.Image || "";
        },
    },

    methods: {
        cancelButtonAction() {
            this.$router.navigateWithoutSaving(
                this.navigationScenarios.CLICKED_CANCEL_VERIFICATION,
                this.pageName
            );
        },
        backButtonAction() {
            this.$router.navigateWithoutSaving(
                this.navigationScenarios.CLICKED_BACK,
                this.pageName
            );
        },
        async forwardButtonAction() {
            this.$router.navigateWithSaving(
                this.navigationScenarios.CLICKED_FORWARD,
                this.pageName
            );
        },
        arePagePrerequisitesValid() {
            return true;
        },
    },
};
</script>
