<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: 0;
  margin: 0;
  background: transparent;
}

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

.user-avatar {
  width: 65px;
  height: 65px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.8);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.15);
  object-fit: cover;
}

.user-name {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 6px;
  color: #fff;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.user-score {
  background: rgba(255, 255, 255, 0.25);
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 13px;
  color: #fff;
  font-weight: 500;
  backdrop-filter: blur(5px);
}
</style>
