// 注册分布
package controllers

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

	"github.com/astaxie/beego"
)

type RegisterController struct {
	beego.Controller
}

// 主界面
// 通过c.Data[key]返回数据
// ace模板文件通过{{.key}}获取数据
func (c *RegisterController) 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.ModelStatment))) == 0 {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	select_username := c.GetString("select_username")
	days, _ := c.GetInt("days")
	if days == 0 {
		days = 7
	}
	log.Printf("RegisterController days[%d]", days)

	users, _ := models.GetTypeUserList("主播")
	shareCode := ""
	if select_username != "" {
		shareCode = models.GetUserShareCode(select_username)
	}
	promoter := models.IsUserPromoter(username)
	if promoter == 1 {
		select_username = username
		shareCode = models.GetUserShareCode(username)
	}
	series, categories := events.Register_get_range_day_time_count(days, shareCode)
	filename := events.Save_register_records(series, username)
	c.Data["Series"], c.Data["Categories"] = series, categories
	c.Data["SelectUserName"] = select_username
	c.Data["Promoter"] = promoter
	c.Data["users"] = users
	c.Data["Days"] = days
	c.Data["file"] = filename
	if c.GetString("querydata") == "register" {
		c.Ctx.Output.Download(filename)
	} else {
		//界面模板文件
		c.TplName = "register.tpl"
	}
}
