<template>
  <div class="container">
    <div class="user">
      <img class="user-avatar" :src="userData.avatar" />
      <div>
        <p class="user-name">{{ userData.name }}</p>
        <span class="user-score">{{ userData.totalScore }}积分</span>
      </div>
    </div>
    
    <van-button 
      round 
      hairline 
      size="mini" 
      icon="arrow-down" 
      color="#ff5d23"
      @click="showPicker = true"
    >
      {{ date.text }}
    </van-button>
    
    <van-popup v-model:show="showPicker" round position="bottom">
      <van-picker 
        :columns="dateList" 
        @cancel="cancel" 
        @confirm="confirm" 
      />
    </van-popup>
  </div>
</template>

<script>
import axios from "axios";

export default {
  props: ["dateList", "date"],
  data() {
    return {
      showPicker: false,
      userData: {},
    };
  },
  methods: {
    fetchUserData() {
      const that = this;
      axios.get("/api/service/userScore").then(function (response) {
        const { error, data = {} } = response.data;
        if (error === 0) {
          that.userData = data || {};
        }
      });
    },
    cancel() {
      this.showPicker = false;
    },
    confirm({ selectedOptions }) {
      const data = selectedOptions[0];
      this.showPicker = false;
      this.$emit("updateYear", data);
    },
  },
  mounted() {
    this.fetchUserData();
  },
};
</script>

<style scoped>
.container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
}

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

.user-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
}

.user-name {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 5px;
}

.user-score {
  color: #ff5d23;
  font-size: 14px;
}
</style>
