<template>
  <div class="my">
    <div class="user" @click="toSetting">
      <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-list">
      <h2>积分明细</h2>
      <div class="list">
        <div class="item" v-for="(item, index) in list" :key="index">
          <div class="left">
            <h2>{{ item.detail }}</h2>
            <p>{{ item.createTime }}</p>
          </div>
          <div class="right">
            <i>{{ item.calcType === '0' ? '-' : '+' }}</i>
            <span>{{ item.integral || 0 }}</span>
            <em>积分</em>
          </div>
        </div>
        <AiEmpty v-if="!list.length"></AiEmpty>
      </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
      }
    },

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

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

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

      toSetting () {
        if (!this.token) {
          this.autoLogin()
        } else {
          this.$linkTo('./UserInfo')
        }
      },

      toLogin () {
        if (!this.token) {
          this.autoLogin().then(() => {
            this.getList()
            this.getUserInfo()
          })
        } else {
          this.getUserInfo()
        }
      },

      getList () {
        if (this.isMore) return

        this.$instance.post(`/api/appwechatintegraldetail/list`, null, {
          params: {
            current: this.current,
            size: 10,
            openId: this.user.openId
          }
        }).then(res => {
          if (res.code === 0) {
            this.$hideLoading()
            if (this.current > 1) {
              this.list = [...this.list, ...res.data.records]
            } else {
              this.list = res.data.records
            }

            if (res.data.records.length < 10) {
              this.isMore = true

              return false
            }

            this.current = this.current + 1
          } else {
            this.isMore = true
          }
        }).catch(() => {
          this.$hideLoading()
        })
      }
    },

      onReachBottom () {
        this.getList()
      }
  }
</script>

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

    div {
      box-sizing: border-box;
    }

    .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;
      }
    }

    .my-list {
      padding: 0 32px;

      & > h2 {
        margin-bottom: 42px;
        font-weight: 600;
        font-size: 34px;
        color: #333333;
      }

      .item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 24px;
        padding: 24px;
        background: #FFFFFF;
        box-shadow: 0 0 8px 0 rgba(0,0,0,0.02);
        border-radius: 16px;

        .right {
          display: flex;
          align-items: baseline;
          line-height: 1;

          i {
            color: #FFB94C;
            font-size: 32px;
          }

          span {
            color: #FFB94C;
            font-size: 40px;
          }

          em {
            font-size: 26px;
            color: #999999;
          }
        }

        .left {
          flex: 1;
          margin-right: 20px;

          h2 {
            line-height: 1.3;
            margin-bottom: 10px;
            font-size: 34px;
            color: #333333;
          }

          p {
            font-size: 26px;
            color: #999999;
          }
        }
      }
    }

    .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>
