import {createSlice} from "@reduxjs/toolkit";
import {REQ_STATUS} from "@api/config";
import {buildReqReducer, reqAgentCheck, reqAgentGet, reqAgentHistory} from "@features/api";
import {getHttpMsg} from "@utils/helpers";
import {toast} from "react-toastify";
const i18n=global.i18n;
const parseLevels=(levels)=>{
    let list=[];
    for (const key in levels) {
        list.push(levels[key]);
    }
    list.sort((a,b)=>{
        return a.Level>b.Level;
    })
    return list;
}
const reducerAgentCheck=(state,action,arg)=>{
    let payload=action.payload;
    if (payload&&payload.code===200){
        let data=payload.data;
        if (data){
            if (data.info)state.info=data.info;
            if (data.subs)state.subs=data.subs;
            if (data.levels)state.levels=parseLevels(data.levels);
            if (data.history)state.bonusList=data.history;
            if (data.link)state.link=data.link;
            state.isInit=true;
            // state.subs=[
            //     {
            //         NickName:"xdh123dfasdfasdfa",
            //         SubTotal:10000,
            //         bonus:40
            //     },
            //     {
            //         NickName:"xdh123",
            //         SubTotal:10000,
            //         bonus:40
            //     },
            //     {
            //         NickName:"xdh123",
            //         SubTotal:10000,
            //         bonus:40
            //     }
            // ]
        }
    }
}
const agentCheckStatus=(state,status,arg)=>{
    state.status.AgentCheck=status;
}
const reducerAgentGet=(state,action,arg)=>{
    let payload=action.payload;
    let msg=getHttpMsg(payload);
    if (payload&&payload.code===200){
        let bonusList=state.bonusList;
        if (bonusList){
            for (let i = 0; i < bonusList.length; i++) {
                let item=bonusList[i];
                if (item.ID===arg){
                    bonusList.splice(i,1);
                    break;
                }
            }
        }
        state.bonusList=bonusList;
        // if(msg!=="")toast.success(msg);
        // else toast.success(i18n.t("agent.get_success"));
        toast.success(i18n.t("agent.get_success"),{toastId:"agent.get_success"});
    }
    else
    {
        if(msg!=="")toast.error(msg,{toastId:"agent.get_error"});
        else toast.error(i18n.t("agent.get_error"),{toastId:"agent.get_error"});
    }
}
const agentGetStatus=(state,status,arg)=>{
    state.status.AgentGet[arg]=status;
}
const reducerAgentHistory=(state,action,arg)=>{
    let payload=action.payload;
    if (payload&&payload.code===200){
        let data=payload.data;
        if (data&&data.history){
            state.history=data.history;
        }
    }
}
const agentHistoryStatus=(state,status,arg)=>{
    state.status.AgentHistory=status;
}
export const AgentSlice = createSlice({
    name: "agent",
    initialState: {
        status:{
            AgentCheck:REQ_STATUS.NONE,
            AgentGet:{

            },
            AgentHistory:REQ_STATUS.NONE
        },
        isInit:false,
        info: {},
        subs:[],
        levels:[],
        bonusList:[],
        history:[],
        link:""
    },
    reducers:{
        //如需切换用户刷新数据 slice 监听onSliceChangeUser
        onSliceChangeUser:((state, action) => {
            state.isInit=false;
        })
    },
    extraReducers: (builder) => {
        buildReqReducer(builder,reqAgentCheck,reducerAgentCheck,agentCheckStatus);
        buildReqReducer(builder,reqAgentGet,reducerAgentGet,agentGetStatus);
        buildReqReducer(builder,reqAgentHistory,reducerAgentHistory,agentHistoryStatus);
    }
})
export const {}=AgentSlice.actions;
export const getAgentInfo=(state)=>{
    return state.agent.info;
}
export const getAgentSubs=(state)=>{
    return state.agent.subs;
}
export const getAgentLevels=(state)=>{
    return state.agent.levels;
}
export const getBonusList=(state)=>{
    return state.agent.bonusList;
}
export const getAgentGetStatus=(state)=>{
    return state.agent.status.AgentGet;
}
export const getAgentHistoryStatus=(state)=>{
    return state.agent.status.AgentHistory;
}
export const getAgentHistory=(state)=>{
    return state.agent.history;
}
export const getAgentStatus=(state)=>{
    return state.agent.status.AgentCheck;
}
export const getIsInit=(state)=>{
    return state.agent.isInit;
}
export const getAgentLink=(state)=>{
    return state.agent.link;
}
export default AgentSlice.reducer;