// 实时数据
package controllers

import (
	"box-gm/models"
	"fmt"
	"log"

	"github.com/astaxie/beego"
)

type GameConfigController struct {
	beego.Controller
}

func (c *GameConfigController) Get() {
	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	c.Data["username"] = userinfo.(*LoginInfo).Username
	c.Data["token"] = models.GetToken()
	userPermission := models.GetPermission(userinfo.(*LoginInfo).Username)
	if (userPermission & (1 << uint(models.ModelSystem))) == 0 {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}

	c.TplName = "gameconfig.tpl"
}

func (c *GameConfigController) Post() {
	userinfo := c.GetSession("user")
	log.Printf("GameConfigController Post userinfo[%v]", userinfo)
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	ty := c.GetString("ty")
	switch ty {
	case "items":
		models.UpdateItemConfig()
	case "goods":
		models.UpdateGoodsConfig()
	default:
		c.Data["json"] = &map[string]interface{}{"status": false, "info": fmt.Sprintf("更新失败:不支持类型%s", ty)}
		c.ServeJSON()
		return
	}

	c.Data["json"] = &map[string]interface{}{"status": true, "info": fmt.Sprintf("更新[%s]成功", ty)}
	c.ServeJSON()
}
