<template>
  <div class="layout" v-if="isMounted">
    <div class="header">
      <div class="navbar flex justify-between items-center">
        <div class="logo flex items-center" @click="backHome">
          <img
            src="https://static.caimei365.com/www/authentic/h5/icon-logo.png"
          />
          <span>认证通</span>
        </div>
        <div class="user-info">
          <template v-if="accessToken">
            <span v-text="userInfo.mobile"></span>
            <span class="underline logout" @click="logout">退出登录</span>
          </template>
          <template v-else>
            <div
              class="login pr-3 pl-3 border rounded-sm border-white leading-6"
              @click="$store.commit('app/SHOW_LOGIN')"
            >
              登录
            </div>
          </template>
        </div>
      </div>
    </div>
    <div class="content">
      <nuxt />
    </div>
    <div class="footer flex justify-center items-center">
      - 由采美网提供技术支持 -
    </div>
    <SimpleLogin></SimpleLogin>
  </div>
</template>

<script>
import { getCookies } from '@/utils/auth'
import { mapGetters } from 'vuex'
export default {
  computed: {
    ...mapGetters(['userInfo', 'type', 'accessToken', 'authUserId']),
  },
  data() {
    return {
      isMounted: false,
    }
  },
  mounted() {
    this.init()
  },
  beforeDestroy() {
    window.removeEventListener('resize', () => {})
  },
  methods: {
    init() {
      this.responseWidth()
      this.initPageData()
    },
    // 初始化数据页面公共数据
    initPageData() {
      const id = this.$route.params.template
      // 保存页面入口
      this.$store.commit('user/SET_TYPE', `${id}/app`)
      // 保存用户AppId
      this.$store.commit('user/SET_AUTHUSERID', id)

      // 获取用户信息
      const userInfo = getCookies('userInfo')
      if (userInfo && userInfo.authUserId === id) {
        this.$store.commit('user/SET_USERINFO', JSON.parse(userInfo))
      }
      // 初始化供应商信息
      this.fetchSupplierInfo()
    },

    // 获取供应商信息
    async fetchSupplierInfo() {
      try {
        const res = await this.$http.api.fetchSupplierInfo({
          authUserId: this.authUserId,
        })
        this.$store.commit('supplier/SET_SUPPLIER_INFO', res.data)
        this.$store.commit('user/SET_APPID', res.data.appId)
      } catch (error) {
        console.log(error)
      } finally {
        this.isMounted = true
      }
    },

    // 退出登录
    logout() {
      this.$store.dispatch('user/logout')
    },

    // 回到首页
    backHome() {
      const url = '/' + this.type
      if (this.$route.path === url) return
      this.$router.replace(url)
    },

    // 响应页面宽度变化
    responseWidth() {
      this.$store.commit('app/SET_SCREEN', window.innerWidth)
      window.addEventListener('resize', (e) => {
        this.$store.commit('app/SET_SCREEN', e.target.innerWidth)
      })
    },
  },
}
</script>

<style scoped lang="scss">
// PC端
@media screen and (min-width: 768px) {
  .layout {
    padding-top: 80px;
    user-select: none;
    .header {
      position: fixed;
      top: 0;
      left: 0;
      z-index: 999;
      width: 100%;
      height: 80px;
      box-sizing: border-box;
      background: linear-gradient(90deg, #101010 0%, #404040 100%);

      .navbar {
        width: 1200px;
        margin: 0 auto;
        height: 100%;
      }

      .logo {
        cursor: pointer;
        img {
          display: block;
          width: 44px;
          height: 44px;
        }
        span {
          font-size: 24px;
          color: #fff;
        }
      }

      .user-info {
        color: #fff;
        font-size: 16px;

        .login,
        .logout {
          cursor: pointer;
        }
      }
    }
    .content {
      min-height: calc(100vh - 80px - 80px);
      background-color: #f7f7f7;
      overflow: hidden;
    }

    .footer {
      height: 80px;
      background-color: #2c3038;
      color: #fff;
      font-size: 14px;
    }
  }
}

// 移动端
@media screen and (max-width: 768px) {
  .layout {
    padding-top: 12.8vw;
    .header {
      position: fixed;
      top: 0;
      left: 0;
      z-index: 999;
      width: 100%;
      padding: 0 2.4vw;
      height: 12.8vw;
      box-sizing: border-box;
      background: linear-gradient(90deg, #101010 0%, #404040 100%);

      .navbar {
        height: 100%;
      }

      .logo {
        img {
          display: block;
          width: 8vw;
          height: 8vw;
        }
        span {
          font-size: 4vw;
          color: #fff;
        }
      }

      .user-info {
        color: #fff;
        font-size: 3vw;
      }
    }
    .content {
      min-height: calc(100vh - 12.8vw - 12.4vw);
    }

    .footer {
      height: 12.4vw;
      background-color: #2c3038;
      color: #fff;
      font-size: 3vw;
    }
  }
}
</style>
