<template>
  <div :class="themeClass">
    <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="~/assets/theme-images/ross/ross-logo-f.png"
              v-if="themeName === 'ross'"
              class="ross"
            />
            <img src="~/assets/theme-images/common/icon-logo.png" v-else />
            <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="flex justify-center">
                <div
                  class="login pr-3 pl-3 rounded-sm border-white leading-6"
                  @click="onLogin"
                >
                  登录
                </div>
                |
                <div
                  class="register pr-3 pl-3 rounded-sm border-white leading-6"
                  @click="onRegister"
                >
                  注册
                </div>
              </div>
            </template>
          </div>
        </div>
      </div>
      <div class="content">
        <nuxt />
      </div>
      <div class="footer flex justify-center items-center">
        - 由采美网提供技术支持 -
      </div>
      <SimpleLogin :type="formType" @click="onLoginClick"></SimpleLogin>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { isWeChat } from '~/utils/validator'
import { toAuthorization } from '~/utils'
export default {
  computed: {
    ...mapGetters([
      'userInfo',
      'accessToken',
      'authUserId',
      'appId',
      'accountType',
      'routePrefix',
      'themeName',
    ]),
    themeClass() {
      return `theme-${this.themeName}`
    },
  },
  // head(context) {
  //   return {
  //     link: [{ rel: 'icon', type: 'image/x-icon', href: context.themeName === 'ross' ? '' : '/favicon.ico' }],
  //   }
  // },
  data() {
    return {
      isMounted: false,
      formType: 'login',
    }
  },
  mounted() {
    this.init()
    console.log(1)
  },
  beforeDestroy() {
    window.removeEventListener('resize', () => {})
  },
  methods: {
    onLoginClick(type) {
      this.formType = type
    },

    init() {
      this.responseWidth()
      this.initPageData()
    },
    // 初始化数据页面公共数据
    initPageData() {
      // 获取供应商id
      const authUserId = parseInt(this.$route.params.template)
      const routePrefix = `/${authUserId}/app`
      // 保存页面路由前缀
      this.$store.commit('app/SET_ROUTE_PREFIX', routePrefix)
      // 保存用户AppId
      this.$store.commit('user/SET_AUTH_USER_ID', authUserId)

      // 设置页面主题
      // if (authUserId === parseInt(12)) {
      //   this.$store.commit('app/SET_PAGE_THEME', 'ross')
      // }

      // 获取用户信息
      let userInfo = this.$getStorage(routePrefix, 'userInfo')
      if (userInfo && userInfo.authUserId === authUserId) {
        this.$store.commit('user/SET_USER_INFO', 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)
        // 如果appId存在
        if (res.data.appId) {
          this.checkAccountType(res.data.appId)
        }
      } catch (error) {
        console.log(error)
      } finally {
        this.isMounted = true
        // 清除缓存
        this.refreshCacheData()
      }
    },

    // 校验公众号类型
    async checkAccountType(appId) {
      try {
        // 1订阅号，2服务号
        const res = await this.$http.api.checkAccountType({ appId })
        this.$store.commit('user/SET_ACCOUNT_TYPE', res.data)
      } catch (error) {
        console.log(error)
      }
    },

    onLogin() {
      // 在微信浏览器中使用微信授权登录
      if (isWeChat() && this.appId && this.accountType === 2) {
        const payload = {
          authUserId: this.authUserId,
          routePrefix: this.routePrefix,
        }
        return toAuthorization(this.appId, payload)
      }
      this.formType = 'login'
      this.$store.commit('app/SHOW_LOGIN')
    },

    onRegister() {
      this.formType = 'register'
      console.log(this.formType)
      this.$store.commit('app/SHOW_LOGIN')
    },

    // 退出登录
    logout() {
      this.$store.dispatch('user/logout')
      console.log(this.routePrefix)
      this.$removeStorage(this.routePrefix, 'userInfo')
      this.backHome()
    },

    // 回到首页
    backHome() {
      if (this.$route.path === this.routePrefix) return
      this.$router.replace(this.routePrefix)
    },

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

    // 数据初始化刷新浏览器
    refreshCacheData() {
      this.$removeStorage(this.routePrefix, 'club_list_data')
    },
  },
}
</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;

          &.ross {
            width: 85px;
            height: 27px;
            margin-right: 15px;
            transform: translateY(-2px);
          }
        }
        span {
          font-size: 24px;
          color: #fff;
        }
      }

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

        .login,
        .register,
        .logout {
          cursor: pointer;

          &:hover {
            @include themify($themes) {
              color: themed('color');
            }
          }
        }
      }
    }
    .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;

          &.ross {
            width: 12.2vw;
            height: 3.9vw;
            margin-right: 1.9vw;
            transform: translateY(-1vw);
          }
        }
        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>
