import { defineStore } from "pinia";

export const useAgentStore = defineStore("agent", {
    state: () => ({
        isLoaded: false,
        level: "",
        status: 3, // 0=待审核，1=审核通过，2=审核未通过，3=未申请
        isAgent: false,
        ancestorID: null,
        agentID: null,
        mobile: "",
    }),
    actions: {
        async fetchAgentStatus() {
            const { data, error } = await useApiFetch("/agent/info")
                .get()
                .json();

            if (data.value && !error.value) {
                if (data.value.code === 200) {
                    this.level = data.value.data.level;
                    this.isAgent = data.value.data.is_agent; // 判断是否是代理
                    this.status = data.value.data.status; // 获取代理状态  0=待审核，1=审核通过，2=审核未通过，3=未申请
                    this.agentID = data.value.data.agent_id;
                    this.mobile = data.value.data.mobile;
                } else {
                    console.log("Error fetching agent info", data.value);
                }
            }
            this.isLoaded = true;
        },
    },
});
