// 留存分布
package controllers

import (
	"github.com/astaxie/beego"
	"time"
	// "log"
	"box-gm/models"
)


type LogController struct {
	beego.Controller
}

// 主界面
// 通过c.Data[key]返回数据
// ace模板文件通过{{.key}}获取数据
func (c *LogController) 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.ModelStatment))) == 0 {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	

	filename := ""
	if c.GetString("action") == "query" {
		username := c.GetString("username")
		ts1 := c.GetString("start_time")
		ts2 := c.GetString("end_time")
		operate := c.GetString("operate")

		start_time := StrToTime(ts1)
		end_time := StrToTime(ts2)

		c.Data["logs"] = models.QueryLog(username, start_time, end_time, operate, 0)
		
		c.Data["Username"] = username
		c.Data["Start_time"] = ts1
		c.Data["End_time"] = ts2
		c.Data["Operate"] = operate
	}else {
		currTime := time.Now()
		c.Data["Start_time"] = currTime.Format("2006-01-02")
		c.Data["End_time"] = currTime.Format("2006-01-02")
	}
	
	if c.GetString("querydata") == "log" {
		c.Ctx.Output.Download(filename)
	}else {
		//界面模板文件
		c.TplName = "log.tpl"
	}
}

