<template>
    <Form @submit="onSubmit" @invalid-submit="onInvalidSubmit" ref="theForm" v-slot="{ meta }">
        <funnelHeader
            cmsWidgetName="FunnelHeaderWidget"
            ref="funnelHeader"
            :overrideImageSrc="clientLogoImageSrc" />
        <div class="container">
            <div class="row">
                <div class="col-12 col-md-10 col-lg-8 col-xl-7">
                    <funnelSubHeader
                        cmsWidgetName="FunnelSubHeaderWidget"
                        class="siteSubHeader"
                        :alignLeft="true" />

                    <buttonQuestion
                        :questionText="existingClaimsQuestionText"
                        :answers="existingClaimsAnswers"
                        class="mb-4"
                        groupName="existingClaimsQuestion"
                        buttonTypeString="listButton"
                        isRequired
                        v-model="existingClaimsSelectedValue" />

                    <div class="or-divider mb-4" role="separator" aria-label="or">
                        <span class="or-divider-text">or</span>
                    </div>

                    <buttonQuestion
                        :answers="startNewClaimAnswers"
                        class="mb-4"
                        groupName="existingClaimsQuestion"
                        buttonTypeString="listButton"
                        isRequired
                        v-model="startNewClaimSelectedValue" />

                    <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 buttonQuestion from "@/digital-components/button-question/button-question";
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: "duplicate-check",
    mixins: [],
    data() {
        return {
            clientConfig: {},
        };
    },
    components: {
        Form,
        funnelHeader,
        insuranceNavBar,
        funnelSubHeader,
        buttonQuestion,
    },
    // Ensure CMS content is fetched during route navigation without depending on `to`/`from`
    async beforeRouteEnter(to, from, next) {
        const clientConfigPageName = `client_${store.getters.order.payment.parentAccountNumber}`;
        const clientConfigPromise = fetchCmsContentForPage(clientConfigPageName);

        const pageName = to.name;
        const cmsContentPromise = fetchCmsContentForPage(pageName);
        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(
                `duplicate-check clientConfig::${clientConfigPageName}`,
                JSON.stringify(vm.clientConfig)
            );
        });
    },
    computed: {
        clientLogoImageSrc() {
            return this.clientConfig?.ClientLogoWidget?.Image || "";
        },
        existingClaimsQuestionText() {
            return this.getCmsContent("ExistingClaimsListWidget", "QuestionText");
        },
        existingClaimsAnswers() {
            return this.getCmsContent("ExistingClaimsListWidget", "Answers");
        },
        startNewClaimQuestionText() {
            return this.getCmsContent("StartNewClaimWidget", "QuestionText");
        },
        startNewClaimAnswers() {
            const answers = this.getCmsContent("StartNewClaimWidget", "Answers");
            if (!Array.isArray(answers)) return [];
            return answers.map((answer) => ({
                buttonLabel: answer.Text,
                value: answer.Name,
            }));
        },
    },

    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>
<style lang="scss">
.cc-page-title {
    font-size: 20px !important;
    line-height: 1.325;
    font-weight: 300;
    color: $black;
}
.or-divider {
    display: flex;
    align-items: center;
    text-align: center;
    color: $gray-600;

    &::before,
    &::after {
        content: "";
        flex: 1;
        border-bottom: 1px solid $gray-500;
    }

    .or-divider-text {
        padding: 0 0.75rem;
        font-size: 0.875rem;
        text-transform: lowercase;
    }
}
</style>
