<template>
  <Form
    @submit="onSubmit"
    @invalid-submit="onInvalidSubmit"
    ref="theForm"
    v-slot="{ meta }"
  >
    <div class="container-fluid shadow rounded-3 p-2 position-relative make-tall px-5">
      <funnelHeader cmsWidgetName="FunnelHeaderWidget" />
      <vehicleBanner cmsWidgetName="VehicleBannerWidget" />
      <funnelSubHeader cmsWidgetName="FunnelSubHeaderWidget" />
      <div class="fade-on-route-transition sub-container make-tall">
        <buttonQuestion
          cmsWidgetName="VinLookupMethod"
          :questionText="questionText"
          :answers="answersFromCms"
          groupName="vinLookupMethodOption"
          buttonType="listButton"
          v-model="selectedValues"
          isRequired
          validationRules="option-required"
        />
        <funnel-footer
          cmsWidgetName="FunnelFooterWidget"
          :isForwardActionDisabled="!meta.valid"
          @isDisabled="!meta.valid"
          @back-clicked="backButtonAction"
          @ForwardClicked="forwardButtonAction"
        />
      </div>
    </div>
  </Form>
</template>

<script>
// Components
import funnelHeader from "@/common-components/funnel-header/funnel-header";
import funnelFooter from "@/common-components/funnel-footer/funnel-footer";
import vehicleBanner from "@/common-components/vehicle-banner/vehicle-banner";
import funnelSubHeader from "@/common-components/funnel-sub-header/funnel-sub-header";
import buttonQuestion from "@/common-components/button-question/button-question";
//Supporting Files
import { fetchCmsContentForPage } from "@/helpers/cms-content-helper";
import { settleAllPromises } from "@/helpers/layout-helper";
import { required } from "@/helpers/validation-rules";
import { errorMessages } from "@/constants/error-messages";
import { Form, defineRule } from "vee-validate";
import store from "@/store";
import { vinLookupMethodSelections } from "@/constants/vin-lookup-method-selections.js";
// Define Validation Rules
defineRule("option-required", required(errorMessages.OPTION_REQUIRED));
export default {
  name: "estimate",
  data() {
    return {
      selectedValues: [],
    };
  },
  
  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);
    next((vm) => {
      vm.setCmsContent(resultMap.cmsContent);
    });
  },
  methods: {
    arePagePrerequisitesValid() {
      if(store.getters.damage.isRepair!=null){
        return true;
      }
      return false; 
    },
    resetDependentState() {},
    backButtonAction() {
      // route to move backwards
      this.$router.navigate(
        this.navigationScenarios.CLICKED_BACK, 
        this.$route
      );
    },
    forwardButtonAction() {
      if (this.selectedValues[0]===vinLookupMethodSelections.MANUALVIN) {
        this.$router.navigate(
          this.navigationScenarios.SELECTED_MANUAL_VIN,
          this.$route
        );
        return; 
      }
      if (this.selectedValues[0]===vinLookupMethodSelections.LICENSEPLATE) {
        this.$router.navigate(
          this.navigationScenarios.SELECTED_LICENSE_PLATE,
          this.$route
        );
        return; 
      }
      if (this.selectedValues[0]===vinLookupMethodSelections.HOMEADDRESS) {
        this.$router.navigate(
          this.navigationScenarios.SELECTED_HOME_ADDRESS,
          this.$route
        );
        return; 
      }
    },
  },
  computed: {
    questionText() {
      return this.getCmsContent("VinLookupMethod", "QuestionText");
    },
    answersFromCms() {
      return this.getCmsContent("VinLookupMethod", "Answers");
    },
  },
  components: {
    funnelHeader,
    vehicleBanner,
    funnelSubHeader,
    funnelFooter,
    buttonQuestion,
    Form,
  },
};
</script>