<template>
  <div id="app">
    <header-nav v-if="showTools" title="web端产品库">
      <template #right>
        <div @click="showTools=false">隐藏工具栏</div>
        <div @click="handleLogin">点此登录</div>
      </template>
    </header-nav>
    <el-row class="fill mar-t48" type="flex">
      <slider-nav v-if="showTools"/>
      <main-content class="fill"/>
    </el-row>
    <div v-if="dialog" class="sign-box">
      <ai-sign style="margin: auto" :instance="$request" :action="{login}"
               visible @login="getToken" :showScanLogin="false"/>
    </div>
    <el-button type="info" v-if="!showTools" class="fixedBtn" @click="showTools=true">显示工具栏</el-button>
  </div>
</template>

<script>
import SliderNav from "./components/sliderNav";
import MainContent from "./components/mainContent";
import HeaderNav from "./components/headerNav";
import {mapActions, mapMutations, mapState} from "vuex";

export default {
  name: 'app',
  components: {HeaderNav, MainContent, SliderNav},
  computed: {
    ...mapState(['user']),
    login() {
      let url = '/auth/oauth/token';
      /project\/sass/g.test(location.pathname) && (url += "?corpId=ww596787bb70f08288")
      return url
    },
  },
  data() {
    return {
      dialog: false,
      showTools: true,
    }
  },
  methods: {
    ...mapMutations(['setToken', 'setFinanceUser']),
    ...mapActions(['getUserInfo']),
    getToken(params) {
      if (params.access_token) {
        this.setToken([params.token_type, params.access_token].join(' '))
        this.dialog = false
        this.$message.success("登录成功,正在刷新页面...")
        location.reload()
      } else this.$message.error(params.msg || "登录失败!")

    },
    handleLogin() {
      this.$request.delete("/auth/token/logout").finally(() => {
        this.dialog = true
      })
    },
  },
  created() {
    wx = jWeixin
    if (this.user.token) this.getUserInfo().then(() => {
      if (/^\/project\/xiushan/.test(location.pathname)) {
        this.setFinanceUser()
      }
    })
  }
}
</script>

<style lang="scss">
html, body {
  width: 100%;
  height: 100%;
  margin: 0;
}

.mar-t48 {
  margin-top: 48px;
}

#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  overflow: hidden;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;

  .fixedBtn {
    position: fixed;
    top: 0;
    right: 60px;
    opacity: 0;

    &:hover {
      opacity: 1;
    }
  }

  li {
    list-style-type: none;
  }

  .sign-box {
    z-index: 99;
    margin: -10px;
    display: flex;
    position: fixed;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.2);
  }
}

</style>
