<template>
  <div class="page">
    <div class="page-top">
      <SimpleCenterCover />
      <div class="club-logo">
        <img :src="clubInfo.logo" alt="" v-if="isAuth" />
        <img
          src="~/assets/theme-images/normal/pc/icon-club-logo-default.png"
          v-else-if="isPc"
        />
        <img
          src="~/assets/theme-images/normal/h5/icon-club-logo-default.png"
          v-else
        />
        <div class="club-mobile">{{ userInfo.mobile }}</div>
        <div class="club-name" v-if="isAuth">
          机构：{{ clubInfo.authParty }}
        </div>
      </div>
    </div>
    <div class="page-content">
      <div class="section-title">我的认证</div>
      <div class="section-content">
        <div class="item club" @click="onToClubDetail">
          <div class="auth-icon" :class="isAuth ? 'auth' : 'un-auth'">
            {{ isAuth ? '已认证' : '未认证' }}
          </div>
          <div class="tip">机构认证</div>
          <div class="btn">
            点击查看<span class="icon el-icon-arrow-right"></span>
          </div>
        </div>
        <div class="item device" @click="onToDeviceList">
          <div class="auth-icon" :class="isAuthDevice ? 'auth' : 'un-auth'">
            {{ isAuthDevice ? '已认证' : '未认证' }}
          </div>
          <div class="tip">设备认证</div>
          <div class="btn">
            点击查看<span class="icon el-icon-arrow-right"></span>
          </div>
        </div>
      </div>
      <div class="line"></div>
      <template v-if="isPc">
        <div class="section-title">账户设置</div>
        <div class="section-content">
          <div class="reset-pwd" @click="onResetPassword">修改密码</div>
        </div>
      </template>
      <template v-else>
        <div class="menu-list">
          <div class="item" @click="toAccountSubNav">
            <span>账户设置</span>
            <span class="el-icon-arrow-right"></span>
          </div>
        </div>
      </template>
      <div v-if="!isPc" class="logout" @click="logout">退出登录</div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  layout: 'app-ph',
  data() {
    return {
      clubInfo: {},
      list: [],
    }
  },
  computed: {
    ...mapGetters(['routePrefix', 'isPc', 'userInfo', 'authUserId']),
    isAuth() {
      return this.clubInfo.auditStatus === 1
    },
    isAuthDevice() {
      return this.isAuth && this.list.length > 0
    },
  },
  created() {
    this.initUserInfo()
  },
  methods: {
    // 初始化用户信息
    async initUserInfo() {
      try {
        const res = await this.$http.api.checkTokenResult()
        this.$store.dispatch('user/login', res.data)
        this.$setStorage(this.routePrefix, 'userInfo', res.data)
        this.fetchAuthDetail()
        this.fetchProductList()
      } catch (error) {
        console.log(error)
      }
    },
    // 获取机构信息
    async fetchAuthDetail() {
      try {
        const authId = this.userInfo.authId
        if (!authId) return
        const res = await this.$http.api.fetchClubAuthInfoData({
          authId,
        })
        this.clubInfo = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 获取已认证设备列表
    async fetchProductList() {
      try {
        const authId = this.userInfo.authId
        if (!authId) return
        const res = await this.$http.api.fetchClubAuthProductList({
          authId,
          authUserId: this.authUserId,
        })
        if (res.data) {
          this.list = res.data
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 去账户设置菜单列表
    toAccountSubNav() {
      this.$router.push(`${this.routePrefix}/center/subnav/account`)
    },
    // 机构详情
    onToClubDetail() {
      this.$router.push(`${this.routePrefix}/center/club-detail`)
    },
    // 设备认证
    onToDeviceList() {
      this.$router.push(`${this.routePrefix}/center/device`)
    },
    // 修改密码
    onResetPassword() {
      this.$router.push(`${this.routePrefix}/center/settings/password`)
    },
    // 退出登录
    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)
    },
  },
}
</script>

<style lang="scss" scoped>
@media screen and (min-width: 768px) {
  .page {
    position: relative;
    min-height: calc(100vh - 80px - 80px);
    background-color: #fff;
    overflow: hidden;
  }
  .page-top {
    position: relative;
    height: 360px;
    // background-image: url(~assets/theme-images/normal/pc/banner-doc.png);

    .club-logo {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      text-align: center;

      img {
        display: inline-block;
        width: 90px;
        height: 90px;
        border-radius: 50%;
      }

      .club-mobile {
        margin: 8px 0;
      }

      .club-mobile,
      .club-name {
        font-size: 18px;
        color: #fff;
      }
    }
  }

  .page-content {
    width: 704px;
    margin: 8px auto 60px;

    .section-title {
      font-size: 24px;
      color: #666666;
      padding: 16px 0;
    }
    .section-content {
      display: flex;
      justify-content: space-between;
      align-items: flex-start;
      .item {
        position: relative;
        width: 340px;
        height: 230px;
        background: #eee;
        box-sizing: border-box;
        padding-left: 24px;
        cursor: pointer;

        display: flex;
        flex-direction: column;
        align-items: flex-start;
        justify-content: center;
        background-size: 340px 230px;
        background-position: center center;
        background-repeat: no-repeat;

        &.club {
          background-image: url(~assets/theme-images/normal/pc/icon-center-item-auth-club.png);
          .icon {
            color: #bc1724;
          }
        }

        &.device {
          background-image: url(~assets/theme-images/normal/pc/icon-center-item-device.png);

          .icon {
            color: #0a6eb1;
          }
        }

        .auth-icon {
          position: absolute;
          right: 16px;
          top: 16px;
          width: 72px;
          height: 32px;
          line-height: 32px;
          text-align: center;
          font-size: 16px;
          color: #fff;
          border-radius: 4px;

          &.auth {
            background: #1890ff;
          }

          &.un-auth {
            background: #f94b4b;
          }
        }
        .tip {
          font-size: 24px;
          color: #fff;
          margin-bottom: 8px;
        }
        .btn {
          font-size: 16px;
          color: #fff;

          .icon {
            display: inline-block;
            width: 18px;
            height: 18px;
            border-radius: 50%;
            background: #fff;
            // background: #0A6EB1;
            font-size: 12px;
            text-align: center;
            line-height: 18px;
            margin-left: 9px;
            font-weight: bold;
            vertical-align: 1px;
          }
        }
      }

      .reset-pwd {
        width: 98px;
        height: 36px;
        background: #bc1724;
        opacity: 1;
        border-radius: 4px;
        font-size: 16px;
        font-weight: 400;
        line-height: 36px;
        color: #ffffff;
        text-align: center;
        cursor: pointer;
      }
    }

    .line {
      height: 1px;
      background: #c2c2c2;
      margin: 32px 0;
    }
  }
}

@media screen and (max-width: 768px) {
  .page-top {
    height: 36vw;
    position: relative;

    .club-logo {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      text-align: center;

      img {
        display: inline-block;
        width: 12.8vw;
        height: 12.8vw;
        border-radius: 50%;
      }

      .club-mobile {
        margin: 0.8vw 0;
      }

      .club-mobile,
      .club-name {
        font-size: 3.4vw;
        color: #fff;
      }
    }
  }

  .page-content {
    padding-bottom: 26vw;
    .section-title {
      font-size: 4.2vw;
      color: #666666;
      padding: 6.4vw 0 3.2vw;
      padding-left: 4vw;
    }

    .logout {
      width: 85.2vw;
      height: 12vw;
      background: #bc1724;
      border-radius: 0.2vw;
      text-align: center;
      line-height: 12vw;
      color: #ffffff;
      font-size: 3.6vw;
      position: fixed;
      bottom: 24vw;
      left: 50%;
      transform: translateX(-50%);
    }

    .section-content {
      display: flex;
      justify-content: space-between;
      align-items: flex-start;
      padding: 0 4vw;
      .item {
        position: relative;
        width: 44.4vw;
        height: 30vw;
        background: #eee;
        box-sizing: border-box;
        padding-left: 3.6vw;
        cursor: pointer;

        display: flex;
        flex-direction: column;
        align-items: flex-start;
        justify-content: center;
        background-size: 44.4vw 30vw;
        background-position: center center;
        background-repeat: no-repeat;

        &.club {
          background-image: url(~assets/theme-images/normal/pc/icon-center-item-auth-club.png);
          .icon {
            color: #bc1724;
          }
        }

        &.device {
          background-image: url(~assets/theme-images/normal/pc/icon-center-item-device.png);
          .icon {
            color: #0a6eb1;
          }
        }

        .auth-icon {
          position: absolute;
          right: 1.6vw;
          top: 1.6vw;
          width: 12.8vw;
          height: 5.6vw;
          line-height: 5.6vw;
          text-align: center;
          font-size: 3vw;
          color: #fff;
          border-radius: 0.4vw;

          &.auth {
            background: #1890ff;
          }

          &.un-auth {
            background: #f94b4b;
          }
        }
        .tip {
          font-size: 4vw;
          color: #fff;
          margin-bottom: 1.2vw;
        }
        .btn {
          font-size: 3vw;
          color: #fff;

          .icon {
            display: inline-block;
            width: 3.4vw;
            height: 3.4vw;
            border-radius: 50%;
            background: #fff;
            font-size: 2.8vw;
            text-align: center;
            line-height: 3.4vw;
            margin-left: 0.8vw;
            font-weight: bold;
            vertical-align: 1px;
          }
        }
      }
    }

    .menu-list {
      padding: 0 4vw;
      padding-top: 2.4vw;
      .item {
        width: 100%;
        padding: 3vw 0;
        display: flex;
        justify-content: space-between;
        font-size: 3.4vw;
        border-bottom: 0.1vw solid #c2c2c2;

        .el-icon-arrow-right {
          font-size: 4vw;
        }
      }
    }

    .line {
      height: 1.6vw;
      background: #f7f7f7;
      margin-top: 6.4vw;
    }
  }
}
</style>
