<template>
  <div id="app">
    <loading
      :show="this.$store.state.loading"
      v-if="this.$store.state.loading"
    ></loading>
    <transition :name="transitionName">
      <keep-alive :include="keepAlive">
        <router-view
          class="Router"
          :class="{ pb_body: isShowNav || isShowNav2 || isShowNav3 }"
        ></router-view>
      </keep-alive>
    </transition>
  </div>
</template>

<script>
import FooterNav from "./components/tools/bottomNav";
import FooterNav2 from "./components/tools/bottomNav2";
import FooterNav3 from "./components/tools/bottomNav3";
import loading from "./components/tools/loading";
export default {
  name: "App",
  data() {
    return {
      isShowNav: true, // 是否显示底部导航 Tab
      isShowNav2: false,
      isShowNav3: false,
      navTabs: ["/", "/learning", "/find", "/my"], // 底部导航
      navTabs2: ["/volunteer", "/volunteerExamine"], // 底部导航
      navTabs3: [
        "/home2",
        "/learning2",
        // "/innovate",
        "/my2",
      ], // 底部导航
      keepAlive: "main-keep-alive", //需要缓存的页面 例如首页
      transitionName: "slide-right", //初始过渡动画方向
      time1: null,
      time2: null,
      time3: null,
      time4: null,
    };
  },
  created: function () {
    // this.routerP();
    // this.getnCount();
    // if (!this.time1) {
    //   this.time1 = setInterval(() => {
    //     this.getnCount();
    //   }, 5000); // 60000
    // }
    // this.getDoi1();
    // this.getDoi2();
    // this.getDoi3();
    // if (!this.time2) {
    //   this.time2 = setInterval(() => {
    //     this.getDoi2();
    //   }, 60000);
    // }
    // if (!this.time3) {
    //   this.time3 = setInterval(() => {
    //     this.getDoi1();
    //   }, 60000);
    // }
    // if (!this.time4) {
    //   this.time4 = setInterval(() => {
    //     this.getDoi3();
    //   }, 60000);
    // }
    //60000
  },
  components: {
    "footer-nav": FooterNav,
    "footer-nav2": FooterNav2,
    "footer-nav3": FooterNav3,
    loading,
  },
  methods: {
    isLogin() {
      let router_path = this.$route.path;
      var _isLogin = this.$cookies.get("login");
      var userInfo = this.$cookies.get("userInfo");
      if (_isLogin == "1" && userInfo) {
        this.$store.commit("update", ["isLogin", true]);
        // var info = JSON.parse(window.sessionStorage.getItem("userInfo"));
        this.$store.commit("update", ["userInfo", userInfo]);
      } else {
        if (router_path != "/") {
          this.$store.commit("update", ["isLogin", false]);
          this.$toast.fail("用户未登录");
          setTimeout(() => {
            this.$router.push("/login");
          }, 2000);
        }
      }
    },
    getnCount() {
      let params = {
        uid: this.$store.state.userInfo.userid,
      };
      this.ajax
        .get(this.$store.state.api + "getNewsCount", params)
        .then((res) => {
          console.log(res);
          if (res.data[0].length > 0) {
            this.$store.commit("update", ["nCount", res.data[0][0].num]);
          } else {
            this.$store.commit("update", ["nCount", 0]);
          }
        })
        .catch((err) => {
          console.error(err);
        });
    },
    getDoi2() {
      let params = {
        uid: this.$store.state.userInfo.userid,
        type: 2,
      };
      this.ajax
        .get(this.$store.state.api + "getDoi", params)
        .then((res) => {
          console.log(res);
          if (res.data[0].length > 0) {
            this.$store.commit("update", ["doi2", res.data[0][0].num]);
          } else {
            this.$store.commit("update", ["doi2", 0]);
          }
        })
        .catch((err) => {
          console.error(err);
        });
    },
    getDoi1() {
      let params = {
        uid: this.$store.state.userInfo.userid,
        type: 1,
      };
      this.ajax
        .get(this.$store.state.api + "getDoi", params)
        .then((res) => {
          console.log(res);
          if (res.data[0].length > 0) {
            this.$store.commit("update", ["doi1", res.data[0][0].num]);
          } else {
            this.$store.commit("update", ["doi1", 0]);
          }
        })
        .catch((err) => {
          console.error(err);
        });
    },
    getDoi3() {
      let params = {
        uid: this.$store.state.userInfo.userid,
        type: 3,
      };
      this.ajax
        .get(this.$store.state.api + "getDoi", params)
        .then((res) => {
          console.log(res);
          if (res.data[0].length > 0) {
            this.$store.commit("update", ["doi3", res.data[0][0].num]);
          } else {
            this.$store.commit("update", ["doi3", 0]);
          }
        })
        .catch((err) => {
          console.error(err);
        });
    },
    routerP() {
      const { navTabs, navTabs2, navTabs3 } = this.$data;
      let router_path = this.$route.path;
      console.log(router_path);
      if (router_path != "/login") {
        this.isLogin();
      }
      var a = 0;
      var b = 0;
      for (var i = 0; i < navTabs.length; i++) {
        if (router_path == navTabs[i]) {
          this.isShowNav = true;
          this.isShowNav2 = false;
          a = 1;
          break;
        }
      }
      if (navTabs2.indexOf(router_path) != -1) {
        this.isShowNav2 = true;
      } else {
        this.isShowNav2 = false;
      }

      if (navTabs3.indexOf(router_path) != -1) {
        this.isShowNav3 = true;
      } else {
        this.isShowNav3 = false;
      }
      if (a == 0) {
        this.isShowNav = false;
      }
    },
  },
  watch: {
    $route(to, from) {
      const { navTabs, navTabs2, navTabs3 } = this.$data;
      const toPath = to.path;
      const fromName = from.name;
      var a = 0;
      for (var i = 0; i < navTabs.length; i++) {
        if (toPath == navTabs[i]) {
          this.isShowNav = true;
          a = 1;
          break;
        }
      }
      if (navTabs2.indexOf(toPath) != -1) {
        this.isShowNav2 = true;
      } else {
        this.isShowNav2 = false;
      }
      if (navTabs3.indexOf(toPath) != -1) {
        this.isShowNav3 = true;
      } else {
        this.isShowNav3 = false;
      }
      if (a == 0) {
        this.isShowNav = false;
      }
      // 切换动画
      let isBack = this.$router.isBack; // 监听路由变化时的状态为前进还是后退
      if (isBack) {
        this.transitionName = "slide-left";
      } else {
        this.transitionName = "slide-right";
      }
      this.$router.isBack = false;
    },
  },
  destroyed() {
    clearInterval(this.time1);
    clearInterval(this.time2);
    clearInterval(this.time3);
    clearInterval(this.time4);
  },
};
</script>

<style>
body {
  font-family: "Microsoft Yahei" !important;
  background: #eee;
}
#app {
  height: 100%;
  width: 100%;
}

.pb_body {
  height: calc(100% - 50px);
  padding: 0 0 50px 0;
  overflow: auto;
}

.pb_head {
  height: 3rem;
  /* background-image: url("./assets/icon/zhuangtailan.png"); */
  background-size: 100% 100%;
  text-align: center;
  line-height: 3rem;
  /* color: #fff; */
  font-size: 0.9rem;
  position: sticky;
  z-index: 9999;
  width: 100%;
  top: 0;
  background: #ffbf43;
}

.pb_head .pb_back {
  position: absolute;
  left: 0.3rem;
  top: 50%;
  transform: translate(0, -50%);
}
.pb_head span {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  width: 14rem;
  display: block;
  margin: 0px auto;
}
.pb_head .pb_left {
  position: absolute;
  right: 1rem;
  font-size: 0.85rem;
  top: 50%;
  transform: translate(0, -50%);
  display: flex;
  align-items: center;
  z-index: 2;
}
.pb_left span {
  width: auto;
}
.pb_content {
  background: rgba(243, 243, 243);
  /* height: 100%; */
  /* width: 100%; */
}
.pb_content2 {
  background: rgba(243, 243, 243);
  height: 100%;
}
.pb_content_body {
  /* padding: 3rem 0 0 0; */
  /* min-height: calc(100% - 5.5rem); */
}

.Router {
  /* position: absolute; */
  /* height: 100%; */
  transition: all 0.377s ease;
  will-change: transform;
  top: 0;
  backface-visibility: hidden;
  perspective: 1000;
  overflow-x: hidden;
}
.slide-left-enter,
.slide-right-leave-active {
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}

.slide-left-leave-active,
.slide-right-enter {
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}
</style>
