import { defineStore } from "pinia";
import { ref } from "vue";
import { getUserLevelInfo } from "@/api/user";

// 定义并导出 store
export const useStoreRights = defineStore("rights", () => {
  // 用户权益
  const userBenefits = ref({});

  const getUserBenefits = async (id) => {
    try {
      const res = await getUserLevelInfo(id);
      userBenefits.value = {
        ...res.data,
        sold: res.data.sold || 0,
        buy: res.data.buy || 0,
        silverSold: res.data.silverSold || 0,
      };
    } catch (error) {
      console.error("获取用户权益失败:", error);
      userBenefits.value = { sold: 0, buy: 0, silverSold: 0 }; // 出错时默认权益为0
    }
  };

  return {
    userBenefits,
    getUserBenefits,
  };
});
