// 聊天查询
package controllers

import (
	"box-gm/events"
	"box-gm/utils"
	"time"
	"box-gm/models"

	"github.com/astaxie/beego"
)

type ClientController struct {
	beego.Controller
}

// 主界面
// 通过c.Data[key]返回数据
// ace模板文件通过{{.key}}获取数据
func (c *ClientController) Get() {
	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	userPermission := models.GetPermission(userinfo.(*LoginInfo).Username)
	if (userPermission & (1 << uint(models.ModelStatment))) == 0 {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	c.Data["username"] = userinfo.(*LoginInfo).Username
	c.Data["token"] = models.GetToken()

	currTime := time.Now()

	scene := c.GetString("scene")
	eventid := c.GetString("eventid")
	startTime := c.GetString("start_time")
	endTime := c.GetString("end_time")
	if startTime == "" {
		startTime = currTime.Format("2006-01-02")
	}
	if endTime == "" {
		endTime = currTime.Format("2006-01-02")
	}
	if scene == "ZgotmplZ" {
		scene = ""
	}
	if eventid == "ZgotmplZ" {
		eventid = ""
	}
	// log.Printf("查询聊天记录 uid[%s] name[%s] startTime[%s] endTime[%s] ty_chat[%s]", uid, name, startTime, endTime, ty_chat)

	_startTime := utils.GetTime(startTime + " 00:00:00")
	_endTime := utils.GetTime(endTime + " 23:59:59")
	eventList := events.Client_get_records(scene, eventid, _startTime, _endTime)

	c.Data["Events"] = eventList
	c.Data["Scene"] = scene
	c.Data["EventId"] = eventid
	c.Data["Start_time"] = startTime
	c.Data["End_time"] = endTime

	c.TplName = "client.tpl"
}
