<template>
  <div id="app">
    <cr-nav-bar v-if="header" :title="title" left-text="" @click-left="backFun" />
    <div class="app">
      <Weapp v-if="isWeixinBrowser" jump-url="pages/groupbuy/webview" />
      <keep-alive>
        <router-view v-if="$route.meta.keepLive" />
      </keep-alive>
      <router-view v-if="!$route.meta.keepLive" />
    </div>
    <div v-if="loading || loadingPic" class="loading-container">
      <cr-loading class="loading" size="24px">加载中...</cr-loading>
    </div>
    <net-error />
    <goods-share ref="gdShare" />
    <div v-if="groupLoading" class="group-list-loading" />
  </div>
</template>
<script>
import { mapState } from 'vuex';
import NetError from '@/components/NetError';
import { isApp, isWxMp, isWechat } from '@/service/validation.service';
import store from '@/store';
import Weapp from '@/components/weapp';
import goodsShare from '@/components/groupShare';
export default {
  name: 'App',
  components: {
    NetError,
    Weapp,
    goodsShare
  },
  data() {
    return {};
  },
  computed: {
    ...mapState({
      title: state => state.pay.title,
      header: state => state.pay.header,
      loading: state => state.pay.loading,
      loadingPic: state => state.pay.loadingPic,
      groupLoading: state => state.pay.groupLoading,
      isPrimordialBrowser: state => state.pay.isPrimordialBrowser,
      isWeixinBrowser: state => state.pay.isWeixinBrowser
    })
  },
  watch: {
    header: {
      immediate: true,
      handler(val, oldVal) {
        if (val !== oldVal) {
          document.body.className = val ? 'has-header' : '';
        }
      }
    },
    $route(v) {
      this.$nextTick(() => {
        this.$refs.gdShare.shareCloseChange(); //切换页面隐藏分享弹窗
        document.querySelectorAll('.cr-overlay').forEach(e => {
          !e.parentNode.className.includes('share') &&
            !v.name == 'payWaiting' &&
            e.parentNode.removeChild(e);
        });
        store.dispatch('change_show_mini_app_guide', {
          bool: !isApp && !isWxMp && !isWechat ? true : false,
          pointer: this
        });
        store.dispatch('change_is_weixin_browser', !isApp && !isWxMp && isWechat ? true : false);
      });
    }
  },
  methods: {
    backFun() {
      this.$router.go(-1);
    }
  }
};
</script>
<style lang="less">
.app {
  user-select: none;
  height: 100%;
  min-height: 100%;
  position: relative;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}

.loading-container {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  z-index: 999;
  display: flex;
  justify-content: center;
  align-items: center;
  .loading {
    padding: 6px;
    border-radius: 6px;
    background-color: rgba(0, 0, 0, 0.5);
    color: #fff;
    @{deep} .cr-loading--text {
      color: #fff;
    }
  }
}
.group-list-loading {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url(https://img.lkbang.net/activity/group-list.png) no-repeat center;
  background-size: 100%;
  z-index: 10;
}
</style>
