<template>
    <div class="avue-contail" :class="{ 'avue--collapse': isCollapse }" v-if="isRouterAlive">
      <div class="avue-header">
        <!-- 顶部导航栏 -->
        <top ref="top" />
        <div class="avue-helpText" v-if="helpShow" :class="{'avue-helpText-active':helpTag}" @click="jlHelpChange">
          <div class="text">
            <img src="../../../public/img/help/icon.png" height="20" width="20"/>

            <span>帮助中心</span>
          </div>
          <img v-if="!helpTag" src="../../../public/img/help/zuo_jiantou.png" height="16" width="16"/>
          <img v-else src="../../../public/img/help/right_jiantou.png" height="16" width="16"/>
        </div>
      </div>
      <div class="avue-layout">
        <div class="avue-left">
          <!-- 左侧导航栏 -->
          <sidebar />
        </div>
        <div class="avue-main" :class="{'avue-main-active':helpTag}">
          <!-- 顶部标签卡 -->
          <tags />
          <transition name="fade-scale">
            <search class="avue-view" v-show="isSearch"></search>
          </transition>
          <!-- 主体视图层 -->
          <div style="height: 100%; overflow-y: auto; overflow-x: hidden" id="avue-view" v-show="!isSearch">
            <keep-alive :include="include" max="10">
              <router-view class="avue-view" v-if="$route.meta.$keepAlive" />
            </keep-alive>
            <router-view class="avue-view" v-if="!$route.meta.$keepAlive" />
          </div>
        </div>

        <!-- 侧边栏 -->
        <jl-help></jl-help>


      </div>
      <div class="avue-shade" @click="showCollapse"></div>
      <set-sign-pwd ref="setSignPwd"></set-sign-pwd>
    </div>
</template>

<script>
  import {mapGetters, mapState} from "vuex";
  import tags from "./tags";
  import search from "./search";
  import top from "./top/";
  import sidebar from "./sidebar/";
  import admin from "@/util/admin";
  import { validatenull } from "@/util/validate";
  import { calcDate } from "@/util/date.js";
  import { getStore } from "@/util/store.js";
  import { resetRouter } from "@/router/router";
  import {whetherSetSignPwd } from '@/api/manage/econtract';
  import setSignPwd from './setSignPwd'
  import jlHelp from '@/components/jl-help'

  export default {
    components: {
      top,
      tags,
      search,
      sidebar,
      setSignPwd,
      jlHelp
    },
    name: "index",
    provide() {
      return {
        index: this,
      };
    },
    data() {
      return {
        //搜索控制
        isSearch: false,
        //刷新token锁
        refreshLock: false,
        //刷新token的时间
        refreshTime: "",
        isRouterAlive: true,
        hasPass:'',
      };
    },
    created() {
      //实时检测刷新token
      this.refreshToken();

    },
    mounted() {
      this.init();
      // this.getSignatureStatus();//获取企业是否设置了签章密码
    },
    activated() { },
    computed: {
      ...mapGetters([
        "isMenu",
        "isLock",
        "isCollapse",
        "website",
        "menu",
        "trial",
        "tagList",
      ]),
      ...mapState({
        helpTag: state => state.common.helpTag,
        helpShow: state => state.common.helpShow
      }),
      include() {
        const arr = [];
        Array.prototype.forEach.call(this.tagList, (item) => {
          arr.push(item.code);
        });
        return arr;
      },
    },
    props: [],
    methods: {
      jlHelpChange () {
        this.$store.dispatch('jlHelpTag', !this.helpTag)
      },
      showCollapse() {
        this.$store.commit("SET_COLLAPSE");
      },
      // 初始化
      init() {
        this.$store.commit("SET_SCREEN", admin.getScreen());
        window.onresize = () => {
          setTimeout(() => {
            this.$store.commit("SET_SCREEN", admin.getScreen());
          }, 0);
        };
        this.$store.dispatch("FlowRoutes").then(() => { });
        this.$store.dispatch("InitArea");
      },
      getSignatureStatus() {
        whetherSetSignPwd().then(res => {
          this.hasPass=res.data.data;
          if(!this.hasPass){
            this.$refs.setSignPwd.open();
          }

        })
      },
      //打开菜单
      openMenu(item = {}) {
        if (this.$store.state.user.auth === "2") {
          this.$store.dispatch("GetMenu", item.id).then((data) => {
            resetRouter();
            this.$router.$avueRouter.formatRoutes(data, true);
          });
        }
      },
      // 定时检测token
      refreshToken() {
        this.refreshTime = setInterval(() => {
          const token =
            getStore({
              name: "token",
              debug: true,
            }) || {};
          const date = calcDate(token.datetime, new Date().getTime());
          if (validatenull(date)) return;
          if (date.seconds >= this.website.tokenTime && !this.refreshLock) {
            this.refreshLock = true;
            this.$store
              .dispatch("refreshToken")
              .then(() => {
                this.refreshLock = false;
              })
              .catch(() => {
                this.refreshLock = false;
              });
          }
        }, 10000);
      },
    },
    watch: {
      trial() {
        this.$jlEvent("trialCheck").then(
          () => { },
          () => { }
        );
      },
      isSearch(val) {
        if (!val) {
          this.isRouterAlive = false;
          this.$nextTick(() => {
            this.isRouterAlive = true;
          });
        }
      },
    },
  };
</script>
