// 实时数据
package controllers

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

	"github.com/astaxie/beego"
)

type RollAwardController struct {
	beego.Controller
}

func (c *RollAwardController) 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.Data["ItemList"] = models.GetItemInfoList()
	c.Data["infoList"] = models.GetRollAwardInfo()
	c.TplName = "rollaward.tpl"
}

func (c *RollAwardController) Post() {
	userinfo := c.GetSession("user")
	log.Printf("RollAwardController Post userinfo[%v]", userinfo)
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	items := c.GetString("items")
	var eItems []int
	err := json.Unmarshal([]byte(items), &eItems)
	if err != nil {
		log.Printf("RollAwardController items[%s] err[%v]", items, err)
		c.Data["json"] = &map[string]interface{}{"status": false, "info": err.Error()}
		c.ServeJSON()
		return
	}

	u := new(models.RollAwardInfo)
	u.Id = models.CreateRollAwardId()
	u.ItemIdList = eItems

	err = models.AddRollAwardInfo(u)
	if err != nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": err.Error()}
		c.ServeJSON()
		return
	}

	c.Data["json"] = &map[string]interface{}{"status": true, "info": "新增roll房奖池方案成功"}
	c.ServeJSON()
}

func (c *RollAwardController) Put() {
	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	id, _ := c.GetInt("id")
	log.Printf("RollAwardController Put id[%d]", id)
	hf := models.GetRollAwardInfoById(id)
	if hf == nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "修改失败:奖池方案不存在"}
		c.ServeJSON()
		return
	}
	items := c.GetString("items")
	var eItems []int
	err := json.Unmarshal([]byte(items), &eItems)
	if err != nil {
		log.Printf("RollAwardController items[%s] err[%v]", items, err)
		c.Data["json"] = &map[string]interface{}{"status": false, "info": err.Error()}
		c.ServeJSON()
		return
	}

	hf.ItemIdList = eItems

	err = models.UpdateRollAwardInfo(hf)
	if err != nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": err}
		c.ServeJSON()
		return
	}
	c.Data["json"] = &map[string]interface{}{"status": true, "info": fmt.Sprintf("修改奖池方案[%d]成功", id)}
	c.ServeJSON()
}

func (c *RollAwardController) Delete() {
	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}

	id, _ := c.GetInt("id")
	log.Printf("RollAwardController Post Delete id[%d]", id)
	hf := models.GetRollAwardInfoById(id)
	if hf == nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "修改失败:兑换码配置不存在"}
		c.ServeJSON()
		return
	}

	models.DelRollAwardInfoById(id)

	c.Data["json"] = &map[string]interface{}{"status": true, "info": "删除兑换码成功"}
	c.ServeJSON()
}
