<template>
  <div>
    <cii-detail v-if="detailType === 'cii'" />
    <mi-detail v-if="detailType === 'mi'" />
    <ai-detail v-if="detailType === 'ai'" />
    <li-detail v-if="detailType === 'li'" />
    <advisor-rec-dialog v-model="showRec" @close="pageBack" />
  </div>
</template>

<script>
import ciiDetail from "./CIIDetail";
import miDetail from "./MIDetail";
import liDetail from "./LIDetail";
import aiDetail from "./AIDetail";
import AdvisorRecDialog from "./modules/AdvisorRecDialog";
import localStorage from "@/service/localStorage";
import { getCulOrder } from "@/api/consultant";

export default {
  name: "GoodsDetail",
  components: {
    ciiDetail,
    miDetail,
    aiDetail,
    liDetail,
    AdvisorRecDialog
  },
  data() {
    return {
      detailType: "",
      showRecFlag: "",
      showRec: false,
      noOrder: false,
      isLogin: ""
    };
  },
  beforeRouteLeave(to, from, next) {
    const { showRecFlag, noOrder, isLogin } = this;
    if (showRecFlag && !noOrder && isLogin) {
      next();
    } else {
      next(false);
      this.showRec = true;
    }
  },
  mounted() {
    this.getOrderInfo();
    this.showRecFlag = localStorage.get("recFlag");
    this.detailType = this.$route.query.type;
  },
  methods: {
    async getOrderInfo() {
      this.isLogin = localStorage.get("mongoToken");
      if (!this.isLogin) {
        return;
      }
      const res = await getCulOrder();
      this.noOrder = res[0].state && res[0].state <= 2;
    },
    pageBack(e) {
      this.showRecFlag = e;
      localStorage.set("recFlag", e);
      if (e === 1) {
        this.$router.back();
      } else {
        this.$router.push({ path: "/consultant" });
      }
    }
  }
};
</script>
