<template>
  <div class="ranking">
    <div class="ranking-header">
      Hill's Treasure Hunt
    </div>
    <div class="ranking-content">
      <div class="ranking-content-left">
        <div class="ranking-content-left-points">
          <div class="list">
            <div class="my-points">
              <p>My Points</p>
              <div class="list-btm">
                <div class="points" v-if="token">
                  <span v-if="usesScore">{{ usesScore }}</span>
                  <img src="./../../assets/common/Icon_Points.png" alt="">
                </div>
                <div class="points" v-else>
                  <span>0</span>
                  <img src="./../../assets/common/Icon_Points.png" alt="">
                </div>
              </div>
            </div>
            <div class="my-ranking">
              <div class="my-ranking-history">
                <p>My Ranking</p>
                <div @click="historyDialogVisible = true">
                  <img src="@/assets/box/Icon_history.png" alt="">
                  <span>History</span>
                </div>
              </div>
              <div class="list-btm" v-if="token">
                <div class="ranking" v-if="usesRank == '-'">{{ usesRank }}</div>
                <div class="ranking" v-else-if="usesRank >= 100">Outside the Top 100</div>
                <div class="ranking" v-else>{{ usesRank+1 }}</div>
              </div>
              <div class="list-btm" v-else>
                <div class="ranking">-</div>
              </div>
            </div>
          </div>
        </div>
        <div v-if="rankingTableData.length == 0" class="ranking-content-left-forward-no">
          <div>
            <p>None</p>
          </div>
        </div>
        <div v-else class="ranking-content-left-forward">
          <div class="forward-list">
            <li>
              <div class="forward-list-item-star">
                <img src="@/assets/box/Icon_Star_01.png" alt="">
              </div>
              <div class="forward-list-item-top">
                <div class="user-img">
                  <img v-if="!rankingTableData[1]" src="@/assets/common/head_default.jpg" alt="">
                  <img v-else-if="rankingTableData[1].avatar == ''" src="@/assets/common/head_default.jpg" alt="">
                  <img v-else :src="rankingTableData[1].avatar" alt="">
                </div>
              </div>
              <div class="forward-list-item-btm" v-if="!rankingTableData[1]">
                <div class="name">none</div>
                <div class="points">
                  <span>0</span>
                  <img src="@/assets/common/Icon_Points.png" alt="">
                </div>
              </div>
              <div class="forward-list-item-btm" v-else>
                <div class="name">{{ rankingTableData[1].nickname }}</div>
                <div class="points">
                  <span>{{ rankingTableData[1].score }}</span>
                  <img src="@/assets/common/Icon_Points.png" alt="">
                </div>
              </div>
            </li>
            <li>
              <div class="forward-list-item-star">
                <img src="@/assets/box/Icon_Star_01.png" alt="">
              </div>
              <div class="forward-list-item-top">
                <div class="user-img">
                  <img v-if="!rankingTableData[0]" src="@/assets/common/head_default.jpg" alt="">
                  <img v-else-if="rankingTableData[0].avatar == ''" src="@/assets/common/head_default.jpg" alt="">
                  <img v-else :src="rankingTableData[0].avatar" alt="">
                </div>
                <!-- <div>{{ rankingTableData[0].nickname.slice(0,4) }}{{ '…'+rankingTableData[0].nickname.slice(-4) }}</div> -->
              </div>
              <div class="forward-list-item-btm" v-if="!rankingTableData[0]">
                <div class="name">none</div>
                <div class="points">
                  <span>0</span>
                  <img src="@/assets/common/Icon_Points.png" alt="">
                </div>
              </div>
              <div class="forward-list-item-btm" v-else>
                <div class="name">{{ rankingTableData[0].nickname }}</div>
                <div class="points">
                  <span>{{ rankingTableData[0].score }}</span>
                  <img src="@/assets/common/Icon_Points.png" alt="">
                </div>
              </div>
            </li>
            <li>
              <div class="forward-list-item-star">
                <img src="@/assets/box/Icon_Star_01.png" alt="">
              </div>
              <div class="forward-list-item-top">
                <div class="user-img">
                  <img v-if="!rankingTableData[2]" src="@/assets/common/head_default.jpg" alt="">
                  <img v-else-if="rankingTableData[2].avatar == ''" src="@/assets/common/head_default.jpg" alt="">
                  <img v-else :src="rankingTableData[2].avatar" alt="">
                </div>
              </div>
              <div class="forward-list-item-btm" v-if="!rankingTableData[2]">
                <div class="name">none</div>
                <div class="points">
                  <span>0</span>
                  <img src="@/assets/common/Icon_Points.png" alt="">
                </div>
              </div>
              <div class="forward-list-item-btm" v-else>
                <div class="name">{{ rankingTableData[2].nickname }}</div>
                <div class="points">
                  <span>{{ rankingTableData[2].score }}</span>
                  <img src="@/assets/common/Icon_Points.png" alt="">
                </div>
              </div>
            </li>
          </div>
        </div>
      </div>
      <div class="ranking-content-right">
        <div v-if="rankingTableData.length == 0" class="ranking-content-right-list-no">
          <div>
            <p>None</p>
          </div>
        </div>
        <div v-else class="ranking-content-right-list">
          <li v-for="(item, index) in rankingTableData" :key="index">
            <div class="left">
              <div class="left-left">
                <div class="rank">
                  <img v-if="item.rank == 1" src="./../../assets/ranking/Ranking1.png" alt="">
                  <img v-else-if="item.rank == 2" src="./../../assets/ranking/Ranking2.png" alt="">
                  <img v-else-if="item.rank == 3" src="./../../assets/ranking/Ranking3.png" alt="">
                  <span v-else>{{ item.rank }}</span>
                </div>
              </div>
              <div class="left-right">
                <div class="user-img">
                  <UserImg :imgSrc="item.avatar" />
                </div>
                <div class="user-name">
                  <div class="label">Name</div>
                  <div class="name">{{ item.nickname }}</div>
                </div>
              </div>
            </div>
            <div class="right">
              <div class="label">Points</div>
              <div class="records">{{ item.score }}</div>
            </div>
          </li>
        </div>
      </div>
    </div>

    <!-- 我的积分记录 -->
    <el-dialog class="records-dialog" :visible="historyDialogVisible" :modal="false" :before-close="historyHandleClose" :show-close="false">
      <div class="top">
        <div class="top-title">Historical Records</div>
        <div class="top-close" @click="historyDialogVisible = false">
          <img src="./../../assets/common/CloseButton.png" alt="">
        </div>
      </div>
      <div class="content">
        <el-table :data="historyCurrentPageItems">
          <template slot="empty">
            <div>No Data</div>
          </template>
          <el-table-column label="Action">
              <template slot-scope="{ row }">
                {{ typeName(row.type) }}
            </template>
          </el-table-column>
          <el-table-column label="Time">
            <template
              slot-scope="scope"
            >{{ scope.row.time | formatDate }}</template>
          </el-table-column>
          <!-- <el-table-column label="Username" :width="180">
            <template
              slot-scope="scope"
            >{{ scope.row.address.slice(0,4) }}{{ '……'+scope.row.username }}</template>
          </el-table-column> -->
          <el-table-column label="Points">
            <template slot-scope="scope">
              <div class="points">
                <span>{{ scope.row.score }}</span>
                <img src="./../../assets/common/Icon_Points.png" alt="">
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="btm">
        <PaginationDialog  @onChangePage="onHistoryChangePage" :pageSize="logPageSize" :total="historyList.length" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from './../../utils/cookies.js'
import PaginationDialog from '@/components/paginationDialog.vue'
import UserImg from '@/components/userImg.vue'
export default  {
  components: {
    PaginationDialog,
    UserImg,
  },
  props: {
    activityName: String,
  },
  data() {
    return {
      token: getToken(),
      detailsDialog: false,
      historyList: [],
      rankingTableData: [],
      myScoreTotal: 0,
      myRankTotal: '-',
      historyDialogVisible: false,
      logPageSize: 8,
      logCurrentPage: 1,
    }
  },
  computed: {
    // 计算我的助力分页
    historyCurrentPageItems() {
      if(this.historyList.length == 0){
        return this.historyList
      }
      let start = (this.logCurrentPage - 1) * this.logPageSize;
      let end = start + this.logPageSize;
      return this.historyList.slice(start, end)
    },
    usesScore() {
      return this.$store.state.user.userData?.scoreTotal || 0
    },
    usesRank() {
      return this.$store.state.user.userData?.rankTotal || 0
    }
  },
  created() {
    this.getLeaderBoard()
  },
  mounted() {
    if(this.token) {
      this.userData = JSON.parse(localStorage.getItem("userData"));
      this.getMyIntegralList()
      this.initData()
    }
  },
  methods: {
    async initData() {
      this.$store.dispatch('wallet/restore')
      this.$store.dispatch('user/restore')
      if (this.token) {
        this.myScoreTotal = this.userData.scoreTotal
        this.myRankTotal = this.userData.rankTotal
      }
    },
    // 我的积分记录
    async getMyIntegralList() {
      if(this.token) {
        let res = await this.$axios.get(process.env.VUE_APP_API_URL+`/api/activity/score_list`,{ params: '',
        headers: { Authorization: `Bearer ${this.token}` },
      })
      this.historyList = res.data.data
      }
    },

    // 获取总积分榜
    async getLeaderBoard () {
      let res = await this.$axios.get(process.env.VUE_APP_API_URL+`/api/activity/leaderboard/uaw_activity/0`)
      this.rankingTableData = res.data.data
    },
    // 获取用户状态
    getUserState() {
      this.$axios.get(process.env.VUE_APP_API_URL+'/api/user/state',{ params: '',
          headers: { Authorization: `Bearer ${this.token}` },
      }).then(res => {
        this.userData = res.data.data
        localStorage.setItem('userData', JSON.stringify(res.data.data))
      })
    },
    async renewData() {
      await this.$store.dispatch('user/fetchUserState')
      this.getLeaderBoard()
      this.getMyIntegralList()
    },

    // 关闭弹窗
    historyHandleClose() {
      this.historyDialogVisible = false;
    },

    // 分页 翻页
    onHistoryChangePage(val) {
      this.logCurrentPage = val;
    },

    // 处理任务类型名字
    typeName(type) {
      if(type.includes('Social Tasks')) {
      // // 任务
        return type = `Task`
      } else if(type.includes('game_step')) {
        // 探索
        return type = `Exploration`
      } else if(type.includes('open_chest')) {
        // 开宝箱
        return type = `Open the Treasure`
      } else if(type.includes('enhance_chest_gift')) {
        // 助力
        return type = `Boosting`
      } else if(type.includes('invite_rebate')) {
        // 邀请收益
        return type = `Invitation Earning`
      } else if(type.includes('')) {
        // 任务
        return type = `Task`
      } else {
        return type = 'Task'
      }
    },
  },
}

</script>

<style lang="scss" scoped>
.ranking {
  position: relative;
  .ranking-header {
    height: 40px;
    line-height: 40px;
    text-align: left;
    position: absolute;
    top: -25px;
    left: 75px;
    font-size: 42px;
    font-weight: 700;
    font-family: "Anton-Regular";
  }
  .ranking-content {
    display: flex;
    justify-content: space-around;
    .ranking-content-left {
      width: 60%;
      position: relative;
      .ranking-content-left-points {
        width: 170px;
        padding: 20px;
        background: url('./../../assets/ranking/My points bg.png') no-repeat;
        background-size: 100% 100%;
        margin-top: 40px;
        margin-left: 30px;
        .list {
          .my-points {
            p {
              font-weight: 700;
              text-align: left;
              font-size: 14px;
            }
            .list-btm {
              position: relative;
              width: 170px;
              height: 40px;
              margin-bottom: 20px;
              .points {
                width: 280px;
                height: 140px;
                background: url('@/assets/ranking/Point bg.png') no-repeat;
                background-size: 100% 100%;
                display: flex;
                align-items: center;
                // justify-content: center;
                position: absolute;
                top: -88%;
                left: 43%;
                transform: translateX(-50%);
                padding-left: 75px;
                box-sizing: border-box;
                span {
                  font-size: 28px;
                  font-weight: 700;
                  font-family: "Anton-Regular";
                  color: #9950fd;
                }
                img {
                  width: 16px;
                  height: 22px;
                  margin-left: 20px;
                }
              }
            }
          }
          .my-ranking {
            .my-ranking-history {
              display: flex;
              justify-content: space-between;
              align-items: center;
              margin-top: 30px;
              position: relative;
              z-index: 99;
              cursor: pointer;
              p {
                font-weight: 700;
                font-size: 14px;
                text-align: left;
              }
              div {
                display: flex;
                align-items: center;
                justify-content: center;
                font-size: 14px;
                background: #2d2738;
                padding: 8px 15px;
                border-radius: 20px;
                img {
                  width: 15px;
                  height: 15px;
                  margin-right: 5px;
                }
                span {
                  font-size: 12px;
                }
              }
            }
            .list-btm {
              font-size: 28px;
              font-weight: 700;
              font-family: 'Anton-Regular';
              .ranking {
                margin-left: 10px;
                color: #FFC35B;
                text-align: left;
              }
            }
          }
        }
      }
      .ranking-content-left-forward {
        width: 70%;
        height: 500px;
        background: url('@/assets/ranking/Background_shine.png') no-repeat;
        background-size: 100% 100%;
        margin: 0 auto;
        padding: 10px;
        position: absolute;
        top: -15%;
        left: 55%;
        transform: translateX(-50%);
        .forward-list {
          width: 1000px;
          height: 100%;
          display: flex;
          justify-content: space-evenly;
          position: absolute;
          left: 50%;
          bottom: -38%;
          transform: translateX(-50%);
          position: relative;
          li {
            width: 300px;
            height: 300px;
            padding: 10px;
            margin-top: 50px;
            box-sizing: content-box;
            background: url('@/assets/ranking/Bg_chest.png') no-repeat;
            background-size: 100% 100%;
            position: relative;
            .forward-list-item-star {
              position: absolute;
              top: 0;
              left: 50%;
              transform: translateX(-50%);
              width: 40px;
              height: 40px;
              img {
                width: 100%;
                height: 100%;
              }
            }
            .forward-list-item-top {
              position: absolute;
              top: 40%;
              left: 50%;
              transform: translate(-50%, -50%);
              .user-img {
                width: 120px;
                height: 120px;
                border-radius: 50%;
                overflow: hidden;
                img {
                  width: 100%;
                  height: 100%;
                }
              }
            }
            .forward-list-item-btm {
              font-size: 14px;
              position: absolute;
              bottom: 20%;
              left: 50%;
              transform: translateX(-50%);
              .name {
                font-weight: 700;
                font-size: 14px;
              }
              .points {
                color: #9950fd;
                display: flex;
                align-items: center;
                justify-content: center;
                margin-top: 5px;
                img {
                  margin-left: 5px;
                }
              }
            }
            &:nth-child(3) {
              position: absolute;
              right: 8%;
              top: 30%;
            }
          }
          li:nth-child(1) {
            position: absolute;
            left: 10%;
            top: 30%;
          }
          li:nth-child(2) {
            position: absolute;
            top: -35%;
            left: 50%;
            transform: translateX(-50%);
            width: 420px;
            height: 420px;
            .forward-list-item-star {
              width: 60px;
              height: 60px;
            }
            .forward-list-item-top {
              .user-img {
                width: 180px;
                height: 180px;
              }
            }
          }
        }
      }
      .ranking-content-left-forward-no {
        height: 500px;
        position: relative;
        div {
          position: absolute;
          top: -8%;
          left: 50%;
          transform: translate(-0%, -50%);
          width: 200px;
          height: 140px;
          background: url('./../../assets/box/none01.png') no-repeat;
          background-size: 100% 100%;
          p {
            position: absolute;
            bottom: 8%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 14px;
          }
        }
      }
    }
    .ranking-content-right {
      width: 30%;
      height: 680px;
      padding: 0 30px;
      box-sizing: border-box;
      position: relative;
      overflow-y: scroll;
      .ranking-content-right-list {
        
        li {
          display: flex;
          align-items: center;
          justify-content: space-between;
          background: #2d2738;
          padding: 8px 50px;
          border-radius: 30px;
          margin-top: 15px;
          .left {
            display: flex;
            align-items: center;
            .left-left {
              width: 50px;
              margin-right: 30px;
              .rank {
                span {
                  font-size: 18px;
                  font-weight: 700;
                  // font-family: "Anton-Regular";
                  color: #9950fd;
                }
                img {
                  width: 40px;
                  height: 30px;
                }
              }
            }
            .left-right {
              display: flex;
              align-items: center;
              .user-name {
                font-size: 16px;
                margin-left: 20px;
                line-height: 1.4;
                .label {
                  text-align: left;
                  color: #979797;
                  font-size: 14px;
                }
              }
            }
          }
          .right {
            line-height: 1.4;
            .label {
              text-align: left;
              color: #979797;
              font-size: 14px;
            }
            .records {
              text-align: left;
            }
            .icon {
              width: 10px;
              height: 12px;
              margin-left: 5px;
              display: flex;
              align-items: center;
              img {
                width: 100%;
                height: 100%;
              }
            } 
          }
        }
      }
      .ranking-content-right-list-no {
        height: 500px;
        position: relative;
        div {
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          width: 200px;
          height: 140px;
          background: url('./../../assets/box/none01.png') no-repeat;
          background-size: 100% 100%;
          p {
            position: absolute;
            bottom: 8%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 14px;
          }
        }
      }
      &::-webkit-scrollbar {
        width: 5px;
      }
      &::-webkit-scrollbar-track{
        background: #171220;
        border-radius:2px;
      }
      &::-webkit-scrollbar-corner{
        display: block;
      }
      &::-webkit-scrollbar-thumb{
        height: 15px;
        background: #9950fd;
      }
    }
  }
  ::v-deep .el-dialog {
    border: 1px solid #924df2;
    // box-shadow: 0px 0px 20px #924df2;
    background: #1a1821;
    border-radius: 80px;
    padding: 0px 50px;
    .el-dialog__header {
      padding: 0;
    }
    .el-dialog__body {
      position: relative;
      color: #fff;
      .top {
        display: flex;

        .top-title {
          color: #fff;
          font-size: 28px;
          font-family: "Anton-Regular";
        }
        .top-close {
          position: absolute;
          top: -8%;
          right: -9%;
          width: 100px;
          height: 100px;
          cursor: pointer;
          img {
            width: 100%;
            height: 100%;
          }
        }
      }
      .content {
        margin: 20px 0;
        height: 420px;
        .el-table {
          background: #1a1821;
          .el-table__header-wrapper {
            border-radius: 20px;
            .el-table__header {
              tr {
                background: #1a1821;

                overflow: hidden;
                .el-table__cell {
                  border: 0px;
                  background: #2d2738;
                  color: #fff;
                }
                .el-table__cell:last-child {
                  border-radius: 0 20px 20px 0 !important;
                }
              }
            }
          }
          .el-table__body-wrapper {
            .el-table__body {
              .el-table__row {
                background: #1a1821;
                .el-table__cell {
                  color: #fff;
                  border: 0px;
                  .points {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    // width: 80px;
                    margin: 0 auto;
                    color: #9950fa;
                    font-family: "Poppins";
                    font-weight: 600;
                    font-size: 16px;
                    img {
                      width: 12px;
                      height: 16px;
                      margin-left: 20px;
                    }
                  }
                  .touxiang {
                    display: flex;
                    align-items: center;
                    justify-content: space-evenly;
                    img {
                      width: 40px;
                      height: 40px;
                    }
                  }
                }
              }
            }
            tr:hover {
              background: none !important;
              td {
                background: none;
              }
            }
          }
          &::before {
            background: #1a1821;
          }
        }
      }
      .btm {
      }
    }
  }
}
</style>