import api from "@/api/index.js"
import utils from '@/utils/utils'
const hb = {
    state: () => ({
        userHbInfo: null, // 红包活动, 游戏角色信息
        taskInfo: [{
                amount: 500,
                end_time: "2021-08-20 20:02:00",
                reward_name: "5元现金",
                task_id: 43,
                task_name: "完成创建角色",
                task_status: "completed",
            }, {
                amount: 500,
                end_time: "2021-08-20 20:02:00",
                reward_name: "5元现金",
                task_id: 44,
                task_name: "达到30级",
                task_status: "uncomplete",
            },
            {
                amount: 1000,
                end_time: "2021-08-20 20:02:00",
                reward_name: "10元现金",
                task_id: 45,
                task_name: "达到50级",
                task_status: "uncomplete",
            },
            {
                amount: 1000,
                end_time: "2021-08-21 20:02:00",
                reward_name: "10元现金",
                task_id: 46,
                task_name: "达到70级",
                task_status: "uncomplete",
            }
        ], // 红包活动 任务列表
        myAwardList: [], // 提现失败 获取更多 -> 抽奖 抽中的奖品
    }),
    actions: {
        // 红包活动, 游戏角色信息
        userHbInfoAction(context, payload) {
            const userHbInfo = {
                amount: 500,
                red_point: 0,
                role_id: "1872_380003",
                role_level: "0",
                role_name: "冷月殇璃",
                server_id: "380003",
                server_name: "圣者3服",
                start_time: "2021-08-10 20:02:00",
            }
            context.commit("userHbInfoMutation", userHbInfo)
            return
            const { server_id, role_id } = payload.arg;
            const { appid } = payload
            api.hbInfo({ server_id, role_id, app_id: appid }).then((res) => {
                payload.checkCode(res, true)
                const { data, code, msg } = res.data;
                // 错误
                if (code) {
                    return;
                }
                // 成功
                data.amount = data.amount / 100;
                data.role_level = utils.getRoleLevel(data.role_level);

                context.commit("userHbInfoMutation", data)
            });
        },
        // 红包活动 任务列表
        taskInfoAction(context, payload) {
            const taskInfo = [{
                    amount: 500,
                    end_time: "2021-08-20 20:02:00",
                    reward_name: "5元现金",
                    task_id: 43,
                    task_name: "完成创建角色",
                    task_status: "completed",
                }, {
                    amount: 500,
                    end_time: "2021-08-20 20:02:00",
                    reward_name: "5元现金",
                    task_id: 44,
                    task_name: "达到30级",
                    task_status: "uncomplete",
                },
                {
                    amount: 1000,
                    end_time: "2021-08-20 20:02:00",
                    reward_name: "10元现金",
                    task_id: 45,
                    task_name: "达到50级",
                    task_status: "uncomplete",
                },
                {
                    amount: 1000,
                    end_time: "2021-08-21 20:02:00",
                    reward_name: "10元现金",
                    task_id: 46,
                    task_name: "达到70级",
                    task_status: "uncomplete",
                }
            ];
            context.commit("taskInfoMutation", taskInfo)
            return

            const { server_id, role_id } = payload.arg;
            const { appid } = payload
            api.hbTasks({ server_id, role_id, app_id: appid }).then((res) => {
                payload.checkCode(res, true);
                const { data, code } = res.data;
                // 错误
                if (code) {
                    return;
                }

                // 如果用户列表为空 不显示入口
                if (!data.length) {
                    context.commit('showHbStartPageMutation', false)
                    context.commit('showHbTaskMutation', false)
                    return
                }

                // 查询第一个 去完成 用来显示小手
                let isFirstCompleteable = false;
                data.map(ele => {
                    if (isFirstCompleteable) {
                        return
                    }

                    // 第一个去完成 并且 任务时间未到期
                    const endTime = new Date(ele.end_time.replace(/\-/g, "/")).getTime()
                    const nowTime = new Date().getTime()
                    if (ele.task_status === 'uncomplete' && endTime > nowTime) {
                        ele.is_first = true;
                        isFirstCompleteable = true
                        return
                    }
                })

                // 成功
                context.commit("taskInfoMutation", data)
            });
        },
        // 提现失败 获取更多 -> 抽奖 抽中的奖品
        myAwardListAction(context, payload) {
            context.commit("myAwardListMutation", payload)
        }
    },
    mutations: {
        // 红包活动, 游戏角色信息
        userHbInfoMutation(state, payload) {
            state.userHbInfo = payload
        },
        // 红包活动 任务列表
        taskInfoMutation(state, payload) {
            state.taskInfo = payload
        },
        // 提现失败 获取更多 -> 抽奖 抽中的奖品
        myAwardListMutation(state, payload) {
            state.myAwardList = payload
        },
    }

}

export default hb