<template>
    <Form
        @submit="onSubmit"
        @invalidSubmit="onInvalidSubmit">
        <div class="fade-on-route-transition">
            <div class="justify-content-center">
                <siteHeader cmsWidgetName="SiteHeaderWidget" />
            </div>
            <div class="iss-heritage-container-width">
                <div class="vehicle-lookup-container iss-heritage-content-container-width">
                    <SiteSubHeader
                        cmsWidgetName="SiteSubHeaderWidget"
                        class="mt-4"
                        subHeaderMarginClasses="mt-1" />
                    <VinLookupMethods
                        ref="VinLookupMethods"
                        v-model="selectedVinLookupMethod"
                        cmsWidgetName="VINLookupMethod"
                        groupName="VinLookupMethods" />
                    <SiteFooter
                        cmsWidgetName="SiteFooterWidget"
                        :isForwardActionDisabled="isForwardActionDisabled"
                        class="mt-5"
                        @backClicked="navigateBack"
                        @forwardClicked="forwardButtonAction" />
                </div>
            </div>
        </div>
    </Form>
</template>

<script>
// Import Supporting Files
import { fetchCmsContentForPage } from '@/helpers/cms-content-helper';
import settleAllPromises from '@/helpers/layout-helper';
import { Form } from 'vee-validate';
import BaseFormMixin from '@/mixins/base-form-mixin';
import vinLookupMethodSelections from '@/constants/vin-lookup-methods';

// Import Component
import SiteFooter from '@/iss-components/site-footer/site-footer.vue';
import siteHeader from '@/iss-components/site-header/site-header.vue';
import SiteSubHeader from '@/iss-components/site-sub-header/site-sub-header.vue';
import VinLookupMethods from '@/layouts/vehicle-lookup/vin-lookup-methods/vin-lookup-methods.vue';

export default {
    name: 'vehicle-lookup',
    components: {
        // eslint-disable-next-line vue/no-reserved-component-names
        Form,
        SiteFooter,
        siteHeader,
        SiteSubHeader,
        VinLookupMethods
    },
    mixins: [BaseFormMixin],
    async beforeRouteEnter(to, from, next) {
        const cmsContentPromise = fetchCmsContentForPage(to.query.issPage);

        // Settle promises and get results
        const promiseResultMap = [
            {
                resultKey: 'cmsContent',
                promise: cmsContentPromise
            }
        ];

        const resultMap = await settleAllPromises(promiseResultMap);

        next((vm) => {
            vm.setCmsContent(resultMap.cmsContent);
            vm.$refs.VinLookupMethods.initializeComponent();
        });
    },
    data() {
        return {
            selectedVinLookupMethod: null
        };
    },
    computed: {
        isForwardActionDisabled() {
            return this.selectedVinLookupMethod === null;
        }
    },
    methods: {
        arePagePrerequisiteValid() {
            return true;
        },
        forwardButtonAction() {
            switch (this.selectedVinLookupMethod) {
                case vinLookupMethodSelections.MANUALVIN:
                    this.$router.navigate(
                        this.navigationScenarios.SELECTED_MANUAL_VIN,
                        this.$route
                    );
                    break;
                case vinLookupMethodSelections.LICENSEPLATE:
                    this.$router.navigate(
                        this.navigationScenarios.SELECTED_LICENSE_PLATE,
                        this.$route
                    );
                    break;
                case vinLookupMethodSelections.HOMEADDRESS:
                    this.$router.navigate(
                        this.navigationScenarios.SELECTED_HOME_ADDRESS,
                        this.$route
                    );
                    break;
                default:
                    break;
            }
        },
        resetDependentState() {}
    }
};
</script>

<style lang="scss" scoped>
.iss-heritage-container-width {
    .vehicle-lookup-container {
        position: relative;
        min-height: 1px;
        padding-left: .9375rem;
        padding-right: .9375rem;
    }
}

</style>
