<template>
  <div :class="themeClass" v-if="isMounted">
    <div class="layout" :style="{ paddingTop: showHeader ? '' : 0 }">
      <div class="header" v-show="showHeader">
        <div class="navbar flex justify-between items-center">
          <div class="logo flex items-center" @click="backHome">
            <img src="~/assets/theme-images/hyt/logo.png" />
            <span>认证通</span>
          </div>
          <div class="flex justify-center items-center">
            <div class="nav" v-if="isPc">
              <template v-for="item in list">
                <div class="link" :key="item.id" @click="onJumpTo(item)">
                  <span class="icon" :class="item.icon"></span>
                  <span class="text">{{ item.name }}</span>
                </div>
              </template>
            </div>
            <div class="user-info">
              <template v-if="accessToken">
                <div class="user-center">
                  <span class="icon el-icon-user-solid"></span>
                  <span class="icon el-icon-arrow-down"></span>
                  <div class="drop-down">
                    <ul class="nav">
                      <li @click.stop="onUserCenter">个人中心</li>
                      <li @click.stop="logout">退出登录</li>
                    </ul>
                  </div>
                </div>
              </template>
              <template v-else>
                <div class="login-btn" @click="onLogin">登录</div>
              </template>
            </div>
            <span class="collapse-icon" @click="drawer = true"></span>
          </div>
        </div>
      </div>
      <div class="content">
        <nuxt />
      </div>
      <!-- <div class="footer flex justify-center items-center" v-show="showFooter">
        - {{ supplierInfo.shopName }} | 由采美网提供技术支持 -
      </div> -->
      <SimpleLogin :type="formType" @click="onLoginClick"></SimpleLogin>
    </div>

    <template v-if="!isPc">
      <el-drawer :visible.sync="drawer" size="63%">
        <div class="nav">
          <template v-for="item in list">
            <div class="link" :key="item.id" @click="onJumpTo(item)">
              <span class="icon" :class="item.icon"></span>
              <span class="text">{{ item.name }}</span>
            </div>
          </template>
        </div>
      </el-drawer>
    </template>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  computed: {
    ...mapGetters([
      'userInfo',
      'accessToken',
      'authUserId',
      'appId',
      'routePrefix',
      'themeName',
      'isPc',
      'showHeader',
      'showFooter',
      'supplierInfo',
    ]),
    themeClass() {
      return `theme-${this.themeName}`
    },
  },
  head() {
    return {
      meta: [
        {
          name: 'viewport',
          content:
            'width=device-width,initial-scale=1.0,minimum-scale=1.0, maximum-scale=1.0, user-scalable=no',
        },
      ],
    }
  },
  data() {
    return {
      formType: 'login',
      drawer: false,
      isMounted: false,
      list: [
        {
          id: 1,
          name: '授权申请',
          path: '/form/club-register',
          icon: 'icon-register',
        },
        {
          id: 2,
          name: '云资料库',
          path: '/docs/0',
          icon: 'icon-doc',
        },
        {
          id: 3,
          name: '意见反馈',
          path: '/feedback',
          icon: 'icon-feedback',
        },
      ],
    }
  },
  created() {},
  mounted() {
    this.responseWidth()
    this.initPageData()
    // this.checkAccountType()
  },
  beforeDestroy() {
    window.removeEventListener('resize', () => {})
    this.refreshCacheData()
  },
  methods: {
    // 跳转
    onJumpTo(item) {
      this.drawer = false
      const hasLogin = this.$store.getters.accessToken
      // 保存登录重定向路由
      this.$setStorage(
        this.routePrefix,
        'login_redicret',
        this.routePrefix + item.path
      )
      if (item.id > 2 && !hasLogin) {
        this.$toast({ message: '请先登录', duration: 1000 })
        this.formType = 'login'
        this.$store.commit('app/SHOW_LOGIN')
        return
      }

      if (item.id === 0) {
        const url = this.routePrefix + item.path
        this.$router.push(url)
      } else {
        const url = this.routePrefix + item.path
        this.$router.push(url)
      }
    },

    // 点击登录
    onLoginClick(type) {
      this.formType = type
    },

    // 初始化数据页面公共数据
    async initPageData() {
      this.$store.commit('app/SET_PAGE_THEME', 'hyt')
      // 获取用户信息
      let userInfo = this.$getStorage(this.routePrefix, 'userInfo')
      if (userInfo && userInfo.authUserId === this.authUserId) {
        this.$store.commit('user/SET_USER_INFO', userInfo)
        const res = await this.$http.api.checkTokenResult()
        this.$store.commit('user/SET_USER_INFO', res.data)
      }
      this.isMounted = true
    },

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

    // 登录
    onLogin() {
      this.formType = 'login'
      this.$store.commit('app/SHOW_LOGIN')
    },

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

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

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

    // 个人中心
    onUserCenter() {
      // const path = `${this.routePrefix}/center`
      window.location.href =
        window.location.origin + `${this.routePrefix}/center`
    },

    // 响应页面宽度变化
    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">
@keyframes slide-down {
  0% {
    top: 48px;
    z-index: 9;
    opacity: 0;
  }
  100% {
    top: 32px;
    opacity: 1;
  }
}

// PC端
@media screen and (min-width: 768px) {
  .layout {
    @include useTheme() {
      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: 122px;
            height: 44px;

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

        .nav {
          .link {
            display: inline;
            margin-left: 32px;
            cursor: pointer;

            &:hover {
              .text {
                color: fetch('color');
              }

              .icon {
                &.icon-register {
                  background-image: url(~assets/theme-images/hyt/pc/link-entry-register-active.png);
                }
                &.icon-doc {
                  background-image: url(~assets/theme-images/hyt/pc/link-entry-doc-active.png);
                }
                &.icon-feedback {
                  background-image: url(~assets/theme-images/hyt/pc/link-entry-feedback-active.png);
                }
              }
            }
          }

          .icon {
            width: 20px;
            height: 20px;
            display: inline-block;
            vertical-align: -4px;
            margin-right: 4px;

            background-size: 20px;
            background-repeat: no-repeat;
            background-position: center;

            &.icon-register {
              background-image: url(~assets/theme-images/hyt/pc/link-entry-register.png);
            }
            &.icon-doc {
              background-image: url(~assets/theme-images/hyt/pc/link-entry-doc.png);
            }
            &.icon-feedback {
              background-image: url(~assets/theme-images/hyt/pc/link-entry-feedback.png);
            }
          }
          .text {
            font-size: 16px;
            color: #fff;
          }
        }

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

          .login-btn {
            width: 80px;
            height: 34px;
            color: #fff;
            background: rgba(255, 255, 255, 0.39);
            font-size: 14px;
            text-align: center;
            line-height: 34px;
            cursor: pointer;
          }
          .user-center {
            position: relative;

            .icon {
              width: 32px;
              height: 32px;
              text-align: center;
              line-height: 32px;

              &.el-icon-user-solid {
                font-size: 24px;
                cursor: pointer;
              }

              &.el-icon-arrow-down {
                font-size: 22px;
                transition: all 0.2s;
              }
            }

            &:hover {
              .drop-down {
                display: block;
                animation: slide-down 0.4s linear forwards;
              }

              .el-icon-arrow-down {
                transform: rotateZ(180deg);
              }
            }

            .drop-down {
              display: none;
              opacity: 0;
              // z-index: -1;
              right: 0;

              position: absolute;
              background: transparent;
              box-sizing: border-box;
              padding-top: 24px;

              .nav {
                width: 118px;
                padding: 8px 0;
                background: #fff;
                box-shadow: 0px 6px 16px rgba(40, 40, 40, 0.1);
                border-radius: 4px;
                li {
                  font-size: 14px;
                  color: #282828;
                  text-align: center;
                  line-height: 40px;
                  transition: all 0.4s;
                  cursor: pointer;

                  &:hover {
                    color: fetch('color');
                  }
                }
              }
            }
          }

          .login,
          .register,
          .logout {
            cursor: pointer;
            &:hover {
              color: fetch('color');
            }
          }
        }
      }
      .content {
        // min-height: calc(100vh - 80px - 80px);
        min-height: calc(100vh - 80px);
        background-color: #f7f7f7;
        overflow: hidden;
        // margin-bottom: 80px;
      }

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

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

        .navbar {
          height: 100%;
        }

        .logo {
          img {
            display: block;
            width: 22.5vw;
            height: 8vw;

            // &.ross {
            //   width: 12.2vw;
            //   height: 3.9vw;
            //   margin-right: 1.9vw;
            //   transform: translateY(-0.6vw);
            // }
          }
          span {
            font-size: 4vw;
            color: #fff;
            display: none;
          }
        }

        .user-info {
          color: #fff;
          font-size: 3vw;

          .logout {
            margin: 0 1.6vw;
          }

          .login-btn {
            width: 13.4vw;
            height: 6.4vw;
            color: #fff;
            background: rgba(255, 255, 255, 0.39);
            font-size: 3.4vw;
            text-align: center;
            line-height: 6.4vw;
            margin-right: 2.4vw;
          }

          .user-center {
            position: relative;

            .icon {
              width: 5.6vw;
              height: 5.6vw;
              text-align: center;
              line-height: 5.6vw;
              margin-right: 4vw;

              &.el-icon-user-solid {
                font-size: 24px;
                cursor: pointer;
              }

              &.el-icon-arrow-down {
                display: none;
              }
            }

            &:hover {
              .drop-down {
                display: block;
                animation: slide-down 0.4s linear forwards;
              }

              .el-icon-arrow-down {
                transform: rotateZ(180deg);
              }
            }

            .drop-down {
              display: none;
              opacity: 0;
              right: 0;

              position: absolute;
              background: transparent;
              box-sizing: border-box;
              padding-top: 1.2vw;

              .nav {
                width: 26vw;
                padding: 1vw 0;
                background: #fff;
                box-shadow: 0px 0.6vw 20vw rgba(40, 40, 40, 0.1);
                border-radius: 0.4vw;
                li {
                  font-size: 3.4vw;
                  color: #282828;
                  text-align: center;
                  line-height: 8.6vw;
                  transition: all 0.4s;
                }
              }
            }
          }
        }

        .collapse-icon {
          display: block;
          width: 5.6vw;
          height: 5.6vw;
          background: url(~assets/theme-images/common/h5-icon-collapse.png)
            no-repeat center;
          background-size: 5.6vw;
        }
      }
      .content {
        // min-height: calc(100vh - 12.8vw - 12.4vw);
        min-height: calc(100vh - 12.8vw);
      }

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

  .nav {
    width: 63vw;
    box-sizing: border-box;
    padding: 0 6.4vw;
    .link {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      border-bottom: 0.1vw solid #c2c2c2;
      padding-bottom: 3vw;
      padding-top: 6vw;
      .icon {
        width: 5.6vw;
        height: 5.6vw;
        vertical-align: -1.2vw;
        margin-right: 2.4vw;
        background-size: 5.6vw;
        background-repeat: no-repeat;
        background-position: center;

        &.icon-register {
          background-image: url(~assets/theme-images/hyt/h5/link-entry-register-active.png);
        }
        &.icon-doc {
          background-image: url(~assets/theme-images/hyt/h5/link-entry-doc-active.png);
        }
        &.icon-feedback {
          background-image: url(~assets/theme-images/hyt/h5/link-entry-feedback-active.png);
        }
      }
      .text {
        font-size: 3.4vw;
        color: #282828;
      }
    }
  }
}
</style>
