<template>
    <Form
        ref="theForm"
        v-slot="{ meta }"
        @submit="onSubmit"
        @invalidSubmit="onInvalidSubmit">
        <div class="fade-on-route-transition">
            <div class="justify-content-center">
                <siteHeader
                    ref="siteHeader"
                    cmsWidgetName="SiteHeaderWidget" />
            </div>
            <div class="iss-heritage-container-width">
                <div class="tpa-confirmation-container iss-heritage-content-container-width">
                    <div class="header">
                        <img :src="tpaConfirmationImage" />
                        <span v-html="tpaConfirmationHeaderText"></span>
                    </div>
                    <div class="subheader">
                        <span v-html="tpaConfirmationSubheaderText"></span>
                    </div>
                    <textBlock
                        ref="tpaConfirmationBodyOne"
                        class="tpa-body-one"
                        :customText="tpaConfirmationBodyOne" />
                    <textBlock
                        ref="tpaConfirmationBodyTwo"
                        class="tpa-body-one"
                        :customText="tpaConfirmationBodyTwo" />
                    <siteFooter
                        v-if="carrierUrl"
                        ref="siteFooter"
                        cmsWidgetName="SiteFooterWidget"
                        :isStackedVertically="true"
                        :isForwardActionDisabled="!meta.valid"
                        @ForwardClicked="forwardButtonAction"
                        @backClicked="navigateBack" />
                </div>
            </div>
        </div>
    </Form>
</template>
<script>
// Components
import siteHeader from '@/iss-components/site-header/site-header.vue';
import siteFooter from '@/iss-components/site-footer/site-footer.vue';
import textBlock from '@/digital-components/text-block/text-block.vue';

// Supporting files
import {
    fetchCmsContentForPage,
    getPhoneLinkHtmlStringFromPhoneNumber,
    processIfStatements
} from '@/helpers/cms-content-helper';
import settleAllPromises from '@/helpers/layout-helper';
import { Form } from 'vee-validate';
import BaseFormMixin from '@/mixins/base-form-mixin.js';
import { useMainStore } from '@/store';
import {
    toTitleCase,
    toDisplayPhoneNumber
} from '@/helpers/text-helper.js';

const VERIFYING_COVERAGE = 'Verifying coverage';

export default {
    name: 'tpa-confirmation',
    components: {
        siteHeader,
        siteFooter,
        textBlock,
        // eslint-disable-next-line vue/no-reserved-component-names
        Form
    },
    mixins: [BaseFormMixin],
    async beforeRouteEnter(to, from, next) {
        // Call APIs
        const cmsContentPromise = fetchCmsContentForPage(to.query.issPage);
        // Settle promises and get results
        const promiseResultMap = [
            {
                resultKey: 'cmsContent',
                promise: cmsContentPromise
            }
        ];
        // use resultMap to populate layout content.
        const resultMap = await settleAllPromises(promiseResultMap);
        next((vm) => {
            vm.setCmsContent(resultMap.cmsContent);
        });
    },
    setup() {
        const mainStore = useMainStore();
        const submittedOrder = mainStore.getSubmittedOrder();
        return { mainStore, submittedOrder };
    },
    computed: {
        tpaConfirmationHeaderText() {
            return this.getCmsContent('TPAConfirmationContent', 'HeaderText');
        },
        tpaConfirmationImage() {
            return this.getCmsContent('TPAConfirmationContent', 'Image');
        },
        tpaConfirmationSubheaderText() {
            return this.getCmsContent(
                'TPAConfirmationContent',
                'SubheaderText'
            )?.replaceAll('{custom:glassShop}', this.preferredShopName);
        },
        tpaConfirmationBodyOne() {
            return this.getCmsContent('TPAConfirmationContent', 'BodyText')
                ?.replaceAll(
                    '{custom:phoneNumber}',
                    getPhoneLinkHtmlStringFromPhoneNumber(this.preferredShopPhoneNumber)
                )
                ?.replaceAll('{custom:glassShop}', this.preferredShopName);
        },
        tpaConfirmationBodyTwo() {
            return this.getCmsContent('TPAConfirmationContent', 'BodyText2')
                ?.replaceAll(
                    '{custom:carrierPhone}',
                    getPhoneLinkHtmlStringFromPhoneNumber(this.carrierPhoneNumber)
                )
                ?.replaceAll('{custom:carrierName}', this.carrierName);
        },
        deductibleBoxValue() {
            return this.isVerified
                ? this.formatAmountInDollars(this.currentDeductible)
                : VERIFYING_COVERAGE;
        },
        currentDeductible() {
            return this.submittedOrder.currentDeductible;
        },
        preferredShopName() {
            return toTitleCase(this.submittedOrder.serviceLocation.provider.companyName);
        },
        preferredShopPhoneNumber() {
            return this.toDisplayPhoneNumber(this.submittedOrder.serviceLocation.provider.phoneNumber);
        },
        isVerified() {
            return this.submittedOrder.isVerified;
        },
        carrierName() {
            return this.mainStore.issConfig.clientFullName;
        },
        carrierUrl() {
            return this.mainStore.issConfig.successReturnURL;
        },
        carrierPhoneNumber() {
            return this.toDisplayPhoneNumber(this.submittedOrder.carrierPhoneNumber);
        }
    },
    mounted() {
        if (this.carrierUrl) {
            this.$refs.siteFooter.updateButtonText(`Go back to ${this.carrierName}`);
        }
    },
    methods: {
        async forwardButtonAction() {
            this.$router.navigateToExternalUrl(this.carrierUrl);
        },
        toDisplayPhoneNumber,
        processIfStatements
    }
};
</script>
<style lang="scss" scoped>
.tpa-confirmation-container {
    position: relative;
    min-height: 1px;
    padding-left: .9375rem;
    padding-right: .9375rem;

    .header {
        margin-top: 1.25rem;
        margin-bottom: .625rem;
        font-size: 1.25rem;
        color: $black;
        img {
            height: 2.25rem;
            width: 2.25rem;
            margin-right: .5rem;
        }
    }

    .subheader {
        margin-bottom: 1.25rem;
        color: $black;
        font-weight: $font-weight-bold;
    }

    :deep(p) {
        margin-bottom: .625rem;
    }

    :deep(strong) {
        color: $black;
        font-weight: 600;
    }
}
</style>
