<template>
  <div id="box" class="home">
    <div :is="this.$store.state.tempName"></div>
  </div>
</template>

<script>
import { strategyGetNew, detMainProduct, strategyGetID } from "../api";
import { getWxCode } from "../api/wx";

export default {
  name: "indexNew",
  data() {
    return {
      channelKey: "no",
      imgUrl: process.env.VUE_APP_IMGS_URL,
    };
  },
  created() {
      localStorage.setItem("beCome", "订购页");
    let channelKey = this.$route.params.key;
    this.onload(channelKey);

  },
  computed: {
    user() {
      return this.$store.state.user;
    },
  },
  methods: {
    onload(channelKey) {
      if (channelKey) {
        localStorage.setItem("key", channelKey);
      } else {
        this.$toast.fail("渠道报错，请刷新页面");
        return false;
      }
      let userid = this.user.userid;
      strategyGetNew({ channelKey ,userid}, true).then(async (res) => {
        if (res.data.length > 0) {
          if (res.data[0].strategyStatus === 1) {
            let data = res.data;
            if (data.id === null) {
              this.$toast.fail("当前页面无效");
              return false;
            }
            if (userid) {
              this.$sensors.login(userid);
            }
            let info = await this.getPorduct(data[0].primaryProductId);
            let recommend = [];
            if (data[0].recommendStrategyList !== null && data[0].recommendStrategyList.length>0) {
              for (let i = 0; i < data[0].recommendStrategyList.length; i++) {
                let list = await strategyGetID(
                  {
                    id: data[0].recommendStrategyList[i],
                  },
                  true
                );
                recommend.push(list.data);
              }
            }
            let pageData = {
              mainProduct: info.data,
              strategyInfo: data[0],
              remarks: JSON.parse(data[0].extJson),
              channl: {
                channelKey: channelKey,
              },
              recommend: recommend,
            };
            document.title = pageData.mainProduct.productName;
            if (
              window.navigator.userAgent
                .toLowerCase()
                .match(/MicroMessenger/i) == "micromessenger"
            ) {
              let banners = JSON.parse(pageData.mainProduct.bannerPics);
              let img = pageData.mainProduct.logoPic
                ? this.imgUrl + pageData.mainProduct.logoPic
                : this.imgUrl + banners[0];
              let url = location.href;
              let desc = pageData.mainProduct.remark3
                ? pageData.mainProduct.remark3
                : "联通畅视流量包";
              getWxCode(pageData.mainProduct.productName, img, url, desc);
            }
            if (navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
              const hackIframe = document.createElement("iframe");
              hackIframe.style.display = "none";
              hackIframe.src = "https://operation.mige.tv/a/mgTitle.html";
              document.body.appendChild(hackIframe);
              setTimeout((_) => {
                document.body.removeChild(hackIframe);
              }, 300);
            }
            this.$store.commit("setPageData", pageData);
            this.$store.commit("setTempName", data[0].templateCode);
          } else {
            this.$toast.fail("当前策略未生效");
          }
        } else {
          strategyGetID({ id: localStorage.getItem("strategyId") }).then(
            async (res) => {
              if (res.data.strategyStatus === 1) {
                let data = res.data;
                let info = await this.getPorduct(data.primaryProductId);
                let recommend = [];
                if (data.recommendStrategyList !== null && data.recommendStrategyList.length>0) {
                  for (let i = 0; i < data.recommendStrategyList.length; i++) {
                    let list = await strategyGetID({
                      id: data.recommendStrategyList[i],
                    });
                    recommend.push(list.data);
                  }
                }
                let pageData = {
                  mainProduct: info.data,
                  strategyInfo: data,
                  remarks: JSON.parse(data.extJson),
                  channl: {
                    channelKey: localStorage.getItem("key"),
                  },
                  recommend: recommend,
                };
                document.title = pageData.mainProduct.productName;
                this.$store.commit("setPageData", pageData);
                this.$store.commit("setTempName", data.templateCode);
              } else {
                this.$toast.fail("当前策略未生效");
              }
            }
          );
        }
      });
    },
    getPorduct(productId) {
      return detMainProduct({ productId: productId });
    },
  },
};
</script>

<style scoped>
</style>
