<template>
  <div class="container">
    <div class="tabs">
      <div :class="{ tabItem: true, active: curTab === 0 }" @click="tabClick(0)">
        总积分
      </div>
      <div :class="{ tabItem: true, active: curTab === 1 }" @click="tabClick(1)">
        年度积分
      </div>
    </div>
    
    <div class="dataList">
      <div class="dataItem">
        <p class="dataItem-score">{{ provinceRank }}</p>
        <p class="dataItem-label">全省的排名</p>
      </div>
      <div class="dataItem">
        <p class="dataItem-score">{{ totalScore }}</p>
        <p class="dataItem-label">服务总积分</p>
      </div>
      <div class="dataItem">
        <p class="dataItem-score">{{ grandeRank }}</p>
        <p class="dataItem-label">本校本年级排名</p>
      </div>
    </div>
    
    <div class="rank">
      <div class="rank-hd">
        <p class="rank-title">本校本年级服务标兵</p>
        <p class="rank-range">1200人参与排名</p>
      </div>
      <ul class="rank-list">
        <li v-for="(item, idx) in dataList" :key="item.id">
          <div class="rank-student">
            <span class="rank-idx">{{ idx + 1 }}</span>
            <span class="rank-avatar">
              <img :src='item.avatar' />
            </span>
            <span class="rank-name">{{ item.name }}</span>
          </div>
          <div class="rank-score">{{ item.score }}分</div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      curTab: 0,
      dataList: [],
      provinceRank: "",
      totalScore: "",
      grandeRank: "",
    };
  },
  methods: {
    tabClick(value) {
      this.curTab = Number(value);
      this.fetchOverview(this.curTab);
      this.fetchRankList(this.curTab);
    },
    fetchOverview(type) {
      const that = this;
      axios
        .get("/api/report/myOverview", {
          params: { type },
        })
        .then(function (response) {
          const { error, data = {} } = response.data;
          if (error === 0) {
            that.provinceRank = data.provinceRank;
            that.totalScore = data.totalScore;
            that.grandeRank = data.grandeRank;
          }
        });
    },
    fetchRankList(type) {
      const that = this;
      const payload = {
        currentPage: 1,
        pageSize: 10,
        type,
      };
      axios
        .get("/api/report/rankList", { params: payload })
        .then(function (response) {
          const { error, data = {} } = response.data;
          if (error === 0) {
            that.dataList = data.list || [];
          }
        });
    },
  },
  mounted() {
    this.fetchOverview(this.curTab);
    this.fetchRankList(this.curTab);
  },
};
</script>

<style scoped>
.container {
  background-color: #f7f8fa;
  padding: 10px;
  min-height: 100vh;
}

.tabs, .dataList, .rank {
  background: white;
  border-radius: 8px;
  margin-bottom: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.tabs {
  display: flex;
  padding: 10px;
}

.tabItem {
  flex: 1;
  text-align: center;
  padding: 10px;
  cursor: pointer;
  color: #666;
}

.tabItem.active {
  color: #ff5d23;
  border-bottom: 2px solid #ff5d23;
  font-weight: bold;
}

.dataList {
  display: flex;
  justify-content: space-around;
  padding: 20px;
}

.dataItem {
  text-align: center;
}

.dataItem-score {
  font-size: 24px;
  font-weight: bold;
  color: #ff5d23;
  margin-bottom: 5px;
}

.dataItem-label {
  font-size: 12px;
  color: #333;
}

.rank {
  padding: 15px;
}

.rank-hd {
  margin-bottom: 15px;
}

.rank-title {
  font-weight: bold;
  font-size: 16px;
}

.rank-range {
  font-size: 12px;
  color: #333;
  margin-top: 5px;
}

.rank-list {
  list-style: none;
  padding: 0;
}

.rank-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #eee;
}

.rank-list li:last-child {
  border-bottom: none;
}

.rank-student {
  display: flex;
  align-items: center;
  gap: 10px;
}

.rank-idx {
  width: 20px;
  text-align: center;
  font-weight: bold;
}

.rank-avatar img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.rank-score {
  color: #ff5d23;
  font-weight: bold;
}
</style>
