<template>
  <div class="ranking">
    <div class="list-wrapper">
      <div class="item my" @click="linkTo">
        <span>{{ info['积分排行'] }}</span>
        <div class="userinfo">
          <image :src="user.avatar || 'https://cdn.cunwuyun.cn/wxmp/tianfuxing/avatar.png'" />
          <h3>{{ user.name }}</h3>
        </div>
        <i>{{ info['累计获取积分'] }}</i>
        <image class="right" src="../img/right.png" />
      </div>
      <div class="item" v-for="(item, index) in list" :key="index">
        <span>{{ index + 1 }}</span>
        <div class="userinfo">
          <image :src="item.avatar || 'https://cdn.cunwuyun.cn/wxmp/tianfuxing/avatar.png'" />
          <h3>{{ item.name }}</h3>
        </div>
        <i>{{ item.score }}</i>
      </div>
      <AiEmpty v-if="!list.length && isMore"></AiEmpty>
    </div>
  </div>
</template>

<script>
  import { mapState } from 'vuex'
  export default {
    data () {
      return {
        search: {
          current: 1,
          title: '',
          size: 100
        },
        isMore: false,
        info: {},
        list: []
      }
    },

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

    mounted () {
      this.$loading()
      this.getList()
      this.getInfo()
    },

    methods: {
      linkTo () {
        uni.navigateTo({
          url: './IntegralDetail'
        })
      },

      getInfo () {
        this.$http.post(`/app/applearningquestion/myScore`).then(res => {
          if (res.code === 0) {
            this.info = res.data
          }
        })
      },

      getList () {
        if (this.isMore) return
        this.$http.post(`/app/applearningquestion/scoreRanking`, null, {
          params: {
            ...this.search
          }
        }).then(res => {
          if (res.code == 0) {
            if (this.search.current > 1) {
              this.list = [...this.list, ...res.data.records]
            } else {
              this.list = res.data.records
            }

            uni.hideLoading()

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

              return false
            }

            this.search.current = this.search.current + 1
          } else {
            uni.hideLoading()
          }
        }).catch(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>

<style scoped lang="scss">
  .ranking {
    padding-bottom: 20px;

    .list-wrapper {
      margin: 0 32px;

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

        span {
          width: 130px;
          font-size: 34px;
          color: #DDDDDD;
          font-style: oblique;
          font-weight: 600;
        }

        &:nth-of-type(1) span {
          color: #2D7DFF;
        }

        &:nth-of-type(2) span {
          color: #3BBC37;
        }

        &:nth-of-type(3) span {
          color: #FF883C;
        }

        image {
          width: 48px;
          height: 48px;
        }

        .userinfo {
          display: flex;
          align-items: center;
          flex: 1;

          h3 {
            margin-left: 16px;
            font-size: 28px;
            color: #333333;
          }
        }

        i {
          width: 100px;
          font-size: 32px;
          color: #FFB94C;
          font-style: normal;
          font-weight: 600;
          text-align: center;
        }
      }
    }

    .my {
      padding-right: 16px!important;

      i {
        width: auto!important;
      }

      .right {
        width: 64px;
        height: 64px;
      }
    }
  }
</style>
