// 实时数据
package controllers

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

	"github.com/astaxie/beego"
)

type ExchangeCodeController struct {
	beego.Controller
}

func (c *ExchangeCodeController) Get() {
	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	username := userinfo.(*LoginInfo).Username
	c.Data["username"] = username
	c.Data["token"] = models.GetToken()
	userPermission := models.GetPermission(username)
	if (userPermission & (1 << uint(models.ModelSystem))) == 0 {
		c.Ctx.Redirect(302, "/accountlogin")
		log.Printf("ExchangeCodeController Get 用户[%s]无权限", username)
		return
	}

	c.Data["ItemList"] = models.GetItemInfoList()
	c.Data["infoList"] = models.GetExchangeCodeInfo()
	c.TplName = "exchangecode.tpl"
}

func (c *ExchangeCodeController) Post() {
	userinfo := c.GetSession("user")
	log.Printf("ExchangeCodeController Post userinfo[%v]", userinfo)
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	pcode := c.GetString("pcode")
	log.Printf("ExchangeCodeController Post pcode[%s]", pcode)
	hf := models.GetExchangeCodeInfoByCode(pcode)
	if hf != nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "新增失败:渠道已配置"}
		c.ServeJSON()
		return
	}
	awardTimes, _ := c.GetInt64("awardTimes")
	expireTime := c.GetString("expireTime")
	items := c.GetString("items")
	var eItems []*models.ExchangeItemsInfo
	err := json.Unmarshal([]byte(items), &eItems)
	if err != nil {
		log.Printf("ExchangeCodeController items[%s] err[%v]", items, err)
		c.Data["json"] = &map[string]interface{}{"status": false, "info": err.Error()}
		c.ServeJSON()
		return
	}

	u := new(models.ExchangeCodeInfo)
	u.Pcode = pcode
	u.AwardTimes = awardTimes
	u.ExpireTime = utils.GetTime64(expireTime)
	u.Items = eItems

	err = models.AddExchangeCodeInfo(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": "新增兑换码成功"}
	c.ServeJSON()
}

func (c *ExchangeCodeController) Put() {
	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	pcode := c.GetString("pcode")
	log.Printf("ExchangeCodeController Post pcode[%s]", pcode)
	hf := models.GetExchangeCodeInfoByCode(pcode)
	if hf == nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "修改失败:兑换码配置不存在"}
		c.ServeJSON()
		return
	}
	awardTimes, _ := c.GetInt64("awardTimes")
	expireTime := c.GetString("expireTime")
	items := c.GetString("items")
	var eItems []*models.ExchangeItemsInfo
	err := json.Unmarshal([]byte(items), &eItems)
	if err != nil {
		log.Printf("ExchangeCodeController items[%s] err[%v]", items, err)
		c.Data["json"] = &map[string]interface{}{"status": false, "info": err.Error()}
		c.ServeJSON()
		return
	}

	hf.AwardTimes = awardTimes
	hf.ExpireTime = utils.GetTime64(expireTime)
	hf.Items = eItems

	err = models.UpdateExchangeCodeInfo(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("修改兑换码[%s]成功", pcode)}
	c.ServeJSON()
}

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

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

	models.DelExchangeCodeInfoById(pcode)

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