<template>
  <section class="personal-rank-wrap rank-wrap">
    <h5header
      v-if="!isWeb"
      title="个人勤奋榜单"
      :stage3BackBuried="'493#407002'"
      :stage3ShareBuried="'493#407001'"
      isGoHistory="1"
      :isOpacity="false"
      :shareObj="shareObj"/>
    <div
      class="rank-list"
      :class="{'padBott60': isWeb}"
      :style="{ marginTop: isWeb?0:`${44+statusBarHeight}Px`,marginBottom: isWeb?0:`${50+homeIndicatorHeight}` + 'Px'}">
        <div class="rank-item" v-for="(item, index) in personList" :key="index">
          <img v-if="index<=2" :src="rankIcon[index]" alt="" class="rank-icon">
          <p v-else class="rank-num">{{index+1}}</p>
          <div class="info">
            <img :src="item.avatarImageUrl" alt="" class="photo">
            <div class="details">
              <p class="name">{{item.name}}</p>
              <p class="awards">奖励: <span v-if="index==0">iPhone11、</span>{{item.bonus}}云鹊豆</p>
            </div>
          </div>
          <div class="score">
            <p class="num">{{item.diligence}}</p>
            <p class="name">勤奋分</p>
          </div>
        </div>
        <div class="no-more" v-if="personList.length>=10">
          <span class="line"></span>
          <span class="text">到底啦</span>
          <span class="line"></span>
        </div>
    </div>
    <div class="footer" v-if="!isWeb&&isLogin" :style="{height: `${50+homeIndicatorHeight}` + 'Px'}">
      <div class="content">
        <div class="left">
          <p class="score">我的勤奋分<span>{{personalData.diligence}}</span></p>
          <!--刘蓓测试需要改成8条，没那么多数据，测完需要改成100-->
          <p class="rank" v-if="personalData.personRank<=8&&personalData.personRank">恭喜您上榜排名<span>第{{personalData.personRank}}</span>
          </p>
          <p class="rank" v-else>很遗憾，您未上榜</p>
        </div>
        <div class="right" @click="share" v-bp="{ component_tag: '493#407003'}">分享排名</div>
      </div>
    </div>
    <no-ssr v-if="isWeb">
      <pica-call-app
        :text="'高效学习 轻松行医'"
        :path="appCallUrl"
      />
    </no-ssr>
    <finish-toast v-if="isGameOver"></finish-toast>
  </section>
</template>

<script>
  import {getPersonScore, getPersonList, isGameOver} from '@/service/stage3Api';
  import h5header from '@/components/h5header';
  import finishToast from '@/components/finish-toast';
  import {setCookie, getCookie, delCookie} from '@/utils/cookieFun';

  export default {
    name: "personal-rank",
    components: {
      h5header,
      finishToast
    },
    data() {
      return {
        personalData: {},
        isWeb: false,
        shareObj: {},
        rankIcon: {
          0: require('../../assets/img/stage3/rank-icon1.png'),
          1: require('../../assets/img/stage3/rank-icon2.png'),
          2: require('../../assets/img/stage3/rank-icon3.png'),
        },
        appCallUrl: '',
        isLogin: true
      }
    },
    async asyncData({query}) {
      const res = await isGameOver();
      const {data} = await getPersonList();
      return {
        isGameOver: res.data,
        personList: data,
        appCallUrl: ''
      }
    },
    created() {
    },
    mounted() {
      this.$rocNative.appInit();
      this.isWeb = this.$rocNative.isWeb;
      this.shareObj = {
        title1: '云鹊奖个人榜单公布',
        title2: '全中国最杰出的医生个人在这里',
        shareImageUrl: 'https://file.yunqueyi.com/h5/images/hd/icon-share.png',
        shareUrl: location.href, //微信分享链接
      };
      this.appCallUrl = location.href; //唤起app页面
      //微信分享初始化--start
      this.$picaWxShare({
        type: (process.env.NUXT_ENV_APP != 'prod') ? 15 : '',
        authUrl: window.location.href,
      }, (wx) => {
        const title = this.shareObj.title1;
        const desc = this.shareObj.title2;
        const link = window.location.href;
        const imgUrl = this.shareObj.shareImageUrl;
        // 分享给朋友
        wx.onMenuShareAppMessage({
          title, desc, link, imgUrl
        });
        // 分享到朋友圈
        wx.onMenuShareTimeline({
          title, desc, link, imgUrl
        });
      });
      //微信分享初始化--end
      if (!this.isWeb) {
        this.getUserInfo();
      }
      ;
      window.__refresh = function (params) {
        _this.getUserInfo();   // 判断是否登录
      }
    },
    methods: {
      getUserInfo() { // 判断是否登录
        this.$rocNative.getUserInfo().then(res => {
          if (res.userMobile) {
            setCookie('token', res.userToken)
            this.getPersonScore();
            this.isLogin = true;
          } else {
            this.isLogin = false;
          }
        })
      },
      getPersonScore() {
        getPersonScore().then(res => {
          this.personalData = res.data
        })
      },
      share() {
        this.$rocNative.shareWechat(this.shareObj);
      }
    }
  }
</script>
<style>
  @import '../../assets/style/pica-call-app.less';
</style>
<style scoped lang="less">
  @import "../../assets/style/awards-list/rank.less";
</style>