<template>
  <div class="my">
    <div class="user">
      <image :src="user.avatarUrl || 'https://cdn.cunwuyun.cn/wxmp/tianfuxing/avatar.png'" />
      <div class="right" v-if="isLogin">
        <h2>{{ user.nickName }}</h2>
        <p v-if="user.levelTitle">{{ user.levelTitle }}</p>
      </div>
      <div class="right" v-else @click="toLogin">
        <h2>登录</h2>
        <p>点击进行登录</p>
      </div>
    </div>
    <div class="info">
      <div class="info-item">
        <h3>积分总额</h3>
        <span>{{ user.integral || 0 }}</span>
        <image src="https://cdn.cunwuyun.cn/wxmp/tianfuxing/nav2.png" />
      </div>
      <div class="info-item">
        <h3>积分排名</h3>
        <span>{{ user.integralOrder || 0 }}</span>
        <image src="https://cdn.cunwuyun.cn/wxmp/tianfuxing/nav1.png" />
      </div>
    </div>
    <div class="my-group">
      <div class="group-item" hover-class="bg-hover" @click="linkTo('./UserInfo')">
        <image src="https://cdn.cunwuyun.cn/wxmp/tianfuxing/banshijindu@2x.png" />
        <span>个人中心</span>
      </div>
      <div class="group-item" hover-class="bg-hover" @click="linkTo('./Merchants')" v-if="user.merchantInfo && user.merchantInfo.status === '1'">
        <image src="https://cdn.cunwuyun.cn/wxmp/tianfuxing/chaoshidingdan@2x.png" />
        <span>商户中心</span>
      </div>
      <div class="group-item" hover-class="bg-hover" @click="linkTo('./Integral')">
        <image src="https://cdn.cunwuyun.cn/wxmp/tianfuxing/wodejifen@2x.png" />
        <span>积分明细</span>
      </div>
    </div>
  </div>
</template>

<script>
  import { mapActions, mapState } from 'vuex'
  export default {
    appName: '我的',
    name: 'AppMy',

    data () {
      return {
        list: [],
        current: 1,
        isMore: false
      }
    },

    computed: {
      ...mapState(['user', 'token']),

      isLogin () {
        return !!this.user.id
      }
    },

    onShow () {
      if (this.token) {
        this.getUserInfo()
      }
    },

    onLoad () {
      if (this.token) {
        this.getUserInfo()
      } else {
        this.autoLogin()
      }

      uni.$on('updateUserInfo', () => {
        this.getUserInfo()
      })
    },

    methods: {
      ...mapActions(['autoLogin', 'getUserInfo']),

      linkTo (url) {
        if (!this.token) {
          this.autoLogin()
        } else {
          this.$linkTo(url)
        }
      },

      toLogin () {
        if (!this.token) {
          this.autoLogin().then(() => {
            this.getUserInfo()
          })
        } else {
          this.getUserInfo()
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .my {
    padding-top: 32px;
    padding-bottom: 30px;

    div {
      box-sizing: border-box;
    }

    .my-group {
      margin: 0 32px;

      .group-item {
        display: flex;
        align-items: center;
        height: 100px;
        margin-bottom: 24px;
        padding: 0 16px;
        box-shadow: 0 0 8px 0 rgba(0,0,0,0.02);
        border-radius: 16px;
        background: #fff;

        image {
          width: 48px;
          height: 48px;
          margin-right: 18px;
        }

        span {
          color: #666666;
          font-size: 30px;
        }
      }
    }

    .user, .info {
      display: flex;
      align-items: center;
      padding: 0 48px;
    }

    .user {
      line-height: 1;
      margin-bottom: 48px;

      image {
        width: 112px;
        height: 112px;
        margin-right: 32px;
        border-radius: 50%;
        border: 4px solid #FFFFFF;
      }

      p {
        margin-top: 18px;
        color: #8891A1;
        font-size: 26px;
      }

      h2 {
        font-weight: 600;
        font-size: 34px;
        color: #1D2229;
      }
    }

    .info {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 58px;

      .info-item {
        display: flex;
        position: relative;
        align-items: center;
        justify-content: space-between;
        font-size: 28px;
        width: 318px;
        height: 112px;
        padding: 0 24px;
        color: #4D5596;
        box-sizing: border-box;

        &:last-child {
          color: #316568;
        }

        h3, span {
          position: relative;
          z-index: 2;
        }

        image {
          position: absolute;
          left: 0;
          top: 0;
          z-index: 1;
          width: 318px;
          height: 112px;
        }
      }
    }
  }
</style>
